Nginx virtual host application-- virtual host based on domain name, IP and port
There are three types of virtual hosts supported by Nginx
● domain name-based virtual host
● virtual host based on IP
● port-based virtual host
Each virtual host can achieve its own functions through the "server {}" configuration segment.
Virtual host experimental environment based on domain name 1. Basic source package (no password): https://pan.baidu.com/s/14WvcmNMC6CFX1SnjHxE7JQ
2.CentOS version 7 Linux virtual machine
Host IP domain name 192.168.235.158www.kgc.comMagnewww.accp.com Experimental step 1, compile and install Nginx service
Step 1: remotely obtain the source code package on Windows and mount it to Linux
[root@localhost] # smbclient-L / / 192.168.235.1Enter SAMBA\ root's password: Sharename Type Comment- LNMP Disk [root@localhost ~] # mkdir / abc [root@localhost ~] # mount.cifs / / 192.168.235.1/LNMP / abcPassword for root@//192.168.235.1/LNMP: [root @ localhost ~] # ls / abcDiscuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.10.tar.bz2mysql-boost-5.7.20.tar.gz nginx-1.12.2.tar.gz php-7.1.20.tar.gz
Step 2: decompress the source package
[root@localhost ~] # cd / abc [root@localhost abc] # tar zxvf nginx-1.12.0.tar.gz-C / opt [root@localhost abc] # ls / optnginx-1.12.0 rh
Step 3: download and install the compiler package
[root@localhost abc] # cd / opt [root@localhost opt] # yum install-y\ > gcc\ / / C language > gcc-c++\ / / C++ language > pcre-devel\ / / pcre language tool > zlib-devel / / compressed function Library
Step 4: create program users and configure Nginx service-related components
[root@localhost opt] # useradd-M-s / sbin/nologin nginx// Creator user nginx And limit its non-login terminal [root@localhost opt] # cd nginx-1.12.0/ [root@localhost nginx-1.12.0] #. / configure\ / configure nginx >-- prefix=//usr/local/nginx\ / / specify installation path >-- user=nginx\ / / specify user name >-- group=nginx\ / / specify the group to which the user belongs >-- with- Http_stub_status_module// installation status Statistics Module
Step 5: compile and install Nginx
[root@localhost nginx-1.12.0] # make & & make install
Step 6: optimize the Nginx service startup script and establish a command soft connection
[root@localhost nginx-1.12.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ create nginx service command soft link to system command [root@localhost nginx-1.12.0] # systemctl stop firewalld.service / / turn off firewall [root@localhost nginx-1.12.0] # setenforce 0bat / turn off enhanced security function [root@localhost nginx-1.12.0] # nginx/ / input Nginx enables the service [root@localhost nginx-1.12.0] # netstat-ntap | grep 80 / / View port 80 of the service Show that tcp 0 0 0.0.0 master 80 0.0.0.0 master is enabled, configure DNS domain name resolution service [root@localhost] # yum-y install bind// install the bind package of DNS service [root@localhost] # vim / etc/named.conf / / edit the main configuration file options {listen-on port 53 {any }; # # replace the listening address 127.0.0.1 with any, listen-on-v6 port 53 {:: 1;}; directory "/ var/named"; dump-file "/ var/named/data/cache_dump.db"; statistics-file "/ var/named/data/named_stats.txt"; memstatistics-file "/ var/named/data/named_mem_stats.txt" Recursing-file "/ var/named/data/named.recursing"; secroots-file "/ var/named/data/named.secroots"; allow-query {any;}; # # replace authorization localhost with any [root@localhost ~] # vim / etc/named.rfc1912.zones / / Edit the regional configuration file zone "kgc.com" IN {type master for the two domain names File "kgc.com.zone"; allow-update {none;};} zone "accp.com" IN {type master; file "accp.com.zone"; allow-update {none;};} [root@localhost ~] # cd / var/named [root@localhost named] # cp-p named.localhost kgc.com.zone [root@localhost named] # cp-p named.localhost accp.com.zone [root@localhost named] # vim kgc.com.zone / / Edit the kgc domain name zone data configuration 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.235.158 IN SOA # delete the content of the last line and add the domain name resolution address to the local address [root@localhost named] # vim accp.com.zone / / Edit the accp domain name zone data configuration 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.235.158security # delete the content of the last line and add the domain name resolution address to the local address [root@localhost named] # systemctl start named / / enable dns service [root@localhost named] # systemctl stop firewalld.service / / turn off firewall [root@localhost named] # setenforce 0 / / turn off enhanced security features 3. Configure virtual host
Step 1: create a test page
[root@localhost named] # cd [root@localhost ~] # mkdir-p / var/www/html/kgc [root@localhost ~] # mkdir-p / var/www/html/accp [root@localhost ~] # ls / var/www/html/accp kgc [root@localhost ~] # cd / var/www/html/ [root@localhost html] # echo "this kgc web" > kgc/index.html [root@localhost html] # echo "this accp web" > accp/index.html
Step 2: edit the nginx.conf configuration file
[root@localhost html] # vim / usr/local/nginx/conf/nginx.confserver {listen 80; server_name www.kgc.com; charset utf-8; # # supports Chinese characters access_log logs/www.kgc.com.access.log; # # kgc site access log location / {root / var/www/html/kgc Index index.html index.htm;} error_page 500 502 503 504 / 50x.html; # # Server error related web page 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.html; location = / 50x.html {root html;}}
Step 3: reload the Nginx service
[root@localhost ~] # killall-s HUP nginx [root@localhost ~] # netstat-ntap | grep 80tcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
Step 4: test the web page and enter the www.kgc.com and www.accp.com domain names to access it.
Port-based virtual host configuration virtual host
Step 1: create a test page for another port
[root@localhost ~] # cd / var/www/html/ [root@localhost html] # echo "this is kgc 8080 web" > kgc/index.html
Step 2: edit the nginx.conf configuration file and modify only the listening address
[root@localhost html] # vim / usr/local/nginx/conf/nginx.confserver {listen 192.168.235.158 listen 80; # # listen on port 80 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 of the host } error_page 500502 503504 / 50x.htl; location = / 50x.html {root html;}} server {listen 192.168.235.158server 808080; # # listen on port 8080 server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log of the host Location / {root / var/www/html/kgc; index index.html index.htm;} error_page 500 502 503 504 / 50x.html; location = / 50x.html {root html;}}
Step 3: reload the Nginx service
[root@localhost html] # killall-s HUP nginx [root@localhost html] # netstat-ntap | grep 80tcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
Step 4: test the web page and visit the default web page at port 80 and the web page at port 8080 respectively
IP-based virtual host step 1: add a network card and plan the domain name IP host IP domain name 192.168.235.158www.kgc.com192.168.235.142www.accp.com step 2: modify the accp domain name zone data file configuration [root@localhost ~] # vim / var/named/accp.com.zone$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.235.142 minimum NS # change the IP address to 192.168.235.142 [root@localhost ~] # systemctl restart named## restart the domain name resolution service step 3: edit the nginx.conf configuration file [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf## this paragraph does not modify server {listen 192.168.235.158systemctl restart named## 80 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.html; location = / 50x.html {root html }} server {listen 192.168.235.142 root 80; # # modify the listening address of this section to 192.168.234.142 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 }} step 4: reload the Nginx service [root@localhost ~] # killall-s HUP nginx [root@localhost ~] # netstat-ntap | grep 80tcp 0 0192.168.235.142killall 80 0.0.0.0killall * LISTEN 7299/nginx: master tcp 00192.168.235.158killall 80 0.0.0.0killall * LISTEN 7299/nginx: master step 5: test the web page Enter the IP addresses 192.168.235.158 and 192.168.235.142 respectively to access
This is the whole content of Nginx virtual host application, thank you for reading!