Build nginx Web Hosting - based domain names, and IP ports

nginx virtual hosts three

1, name-based virtual hosting

2, IP-based virtual hosts

3, Port-based virtual hosts


First, based on the domain name to build

1, compile and install nginx Service

2, configure the DNS Domain Name Service

3, the virtual host configuration

a, create a self-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

b, edit the configuration file nginx.conf

vim /usr/local/nginx/conf/nginx.conf
    include conf.d/*.conf;
    server {
        listen             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             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;
        }
    }

c, heavy-duty service

systemctl restart nginx
netstat -ntap | grep 80

d, access test

www.kgc.com
www.accp.com

Second, based on the port

a, create a test page to another port

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html

b, edit the configuration file nginx.conf, modify only listen address



    server {
        listen             192.168.109.137: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;
        }
    }
    server {
        listen             192.168.109.137:8080;
        server_name  www.accp.com;
        charset utf-8;
        access_log  logs/www.accp8080.com.access.log  ;
        location / {
            root   /var/www/html/accp8080;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

c, reload nginx service

systemctl restart nginx
netstat -ntap | grep 80

d, test page

www.accp.com
www.accp.com8080

Third, based on IP

1, the configuration file modify the page area data profile

vim /var/named/kgc.com.zone
systemctl restart named

2, the configuration nginx.conf edit, modify ip address

 server {
        listen             192.168.109.137: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.109.134: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;
        }
    }

c, reload nginx service

systemctl restart nginx
netstat -ntap | grep 80

d, test page

192.168.109.137
192.168.109.134


Guess you like

Origin blog.51cto.com/14475876/2449830