Describe in detail the configuration of Nginx virtual host in Linux system
Nginx virtual host application Nginx supports three kinds of domain name-based virtual hosts. IP-based virtual host port-based virtual host is implemented through "server {}" configuration segment
This lab continues with the last one to build Nginx services. The compilation and installation of Nginx is not introduced in the previous part.
Domain name-based virtual host [root@localhost nginx-1.12.2] # mkdir-p / var/www/html/accp / / Recursive create accp web site directory [root@localhost nginx-1.12.2] # mkdir-p / var/www/html/kgc / / Recursively create kgc web site directory [root@localhost nginx-1.12.2] # cd / var/www/html/ enter site directory [root @ localhost html] # ls / / View accp kgc [root@localhost html] # echo "this is kgc web" > kgc/index.html / / create site file [root@localhost html] # echo "this is accp web" > accp/index.html / / create site file [root@localhost html] # ls accp/ View accp site directory index.html [root@localhost html] # ls kgc/ View kgc site directory Record index.html [root@localhost conf] # vim / etc/named.rfc1912.zones / / Edit DNS servant region configuration file. / / omit part of the content. Zone "kgc.com" IN {type master File "kgc.com.zone"; allow-update {none;};}; zone "accp.com" IN {type master; file "accp.com.zone"; / / add accp web name resolution allow-update {none;};} ...: wq [root@localhost conf] # cd / var/named/ [root@localhost named] # cp-p named.localhost kgc.com.zone / / copy the area data file [root@localhost named] # vim kgc.com.zone / / Edit the kgc area data file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.144.133:wq [root@localhost named] # cp-p kgc.com.zone accp.com.zone / / copy the kgc zone data file with the same permissions Copy to accp area data file [root@localhost named] # systemctl restart named / / restart DNS service [root@localhost init.d] # cd / usr/local/nginx/conf/ [root@localhost conf] # vim nginx.conf / / Edit Nginx main configuration file. / / omit some of the contents. Server {listen 80 Server_name www.kgc.com; / / set kgc domain name access entry charset utf-8; / / change string type access_log logs/www.kgc.com.access.log; / / change log file name location / {root / var/www/html/kgc; / / change site location index index.html index.htm } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}} server {listen 80; server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; location / {root / var/www/html/accp; index index.html index.htm } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html }}. / / omit some content.: wq [root@localhost conf] # nginx-t / / Detection Syntax nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf] # service nginx restart / / restart the service [root@localhost conf] # systemctl restart named / / restart the DNS service to test the web page in the client
Port-based virtual host [root@localhost conf] # vim nginx.conf / / Edit Nginx main configuration file. / / omit part of the content. # server {# listen 80th # server_name www.kgc.com;# charset utf-8;# access_log logs/www.kgc.com.access.log # location / {# root / var/www/html/kgc; / / comment out this section # index index.html index.htm;#} # error_page 500502503504 / 50x.htmlX # location = / 50x.html {# root html #} #} server {listen 192.168.144.133PV80; / / Port entry money add IP address server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; location / {root / var/www/html/accp; index index.html index.htm } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}} server {/ / copy the contents of the above part listen 192.168.144.133 8080 / / set the same IP to listen on different ports server_name www.accp.com; charset utf-8; access_log logs/www.accp8080.com.access.log; / / change the log file name location / {root / var/www/html/accp8080; / / correct the site file directory name index index.html index.htm } error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html }}. / / omit part of the content.: wq [root@localhost conf] # cd / var/www/html/ enter the site directory [root@localhost html] # mkdir accp8080/ / create directory [root@localhost html] # echo "this is accp8080 web" > accp8080/index.html / / Edit page content [root@localhost html] # cd / usr/local/nginx/conf / [root@localhost conf] # nginx-t / / Detection Syntax nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf] # service nginx restart / / restart the service [root@localhost conf] # netstat-ntap | grep 80 / / View port tcp 0 0192.168.144.133 nginx 80800 .0.0.0: * LISTEN 11967/nginx: master tcp 0 192.168.144.133 LISTEN 11967/nginx: access the test in the client
Virtual hosts based on different IP first add network cards to Linux virtual machines.
[root@localhost conf] # ifconfig / / View the newly added Nic information Record the IP address ens33: flags=4163 mtu 1500 inet 192.168.144.133 netmask 255.255.255.0 broadcast 192.168.144.255 inet6 fe80::a85a:c203:e2e:3f3c prefixlen 64 scopeid 0x20 ether 00:0c:29:5b:d3:a0 txqueuelen 1000 (Ethernet) RX packets 67362 bytes 72060261 (68.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 16836 bytes 1825469 (MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0ens36: flags=4163 mtu 1500 inet 192.168.144.145 netmask 255.255.255.0 broadcast 192.168.144.255 inet6 fe80::deb1:3cec:3e26:5ec2 prefixlen 64 scopeid 0x20 ether 00:0c:29:5b:d3:aa txqueuelen 1000 (Ethernet) RX packets 6 bytes 926 (926.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27 bytes 4513 (4. 4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost conf] # vim / var/named/kgc.com.zon / / change the IP address $TTL 1D @ IN SOA @ rname.invalid parsed by kgc pages in the DNS area data file. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.144.145 / change to the IP address of the newly added network card: wq [root@localhost conf] # vim nginx.conf / / Edit the main configuration file. / / omit some of the contents. Server {listen 192.168.144.145 server 80; / / remove the comments that were fully added when configuring port-based configuration above, and add the listening IP address server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log; location / {root / var/www/html/kgc Index index.html index.htm;} error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}} server {listen 192.168.144.133 location; server_name www.accp.com; charset utf-8 Access_log logs/www.accp.com.access.log; / / remain unchanged location / {root / var/www/html/accp; index index.html index.htm;} error_page 500 502 503 504 / 50x.html; location = / 50x.html {root html } # server {# listen 192.168.144.133server_name www.accp.com;# charset utf-8;# access_log logs/www.accp8080.com.access.log; / / comment out this section # location / {# root / var/www/html/accp8080 # index index.html index.htm;#} # error_page 500 502 503 504 / 50x.html political # location = / 50x.html {# root html #} #}. / / omit part of the content.: wq [root@localhost conf] # nginx-t / / check syntax nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@localhost conf] # service nginx restart / / restart service [root@localhost conf] # systemctl restart named / / restart DNS service Be sure to access the test in the client (here it is recommended to open a new client for access testing)