1. 程式人生 > >nginx基於域名的虛擬主機配置實戰

nginx基於域名的虛擬主機配置實戰

linux

背景:

在www虛擬主機站點基礎上新增一個bbs虛擬主機站點。

1 備份配置文件

[[email protected] conf]# pwd
/application/nginx/conf
[[email protected] conf]# cp nginx.conf{,.oldboy.2017.0819}

2 編輯nginx.conf配置文件,新增bbs虛擬主機站點


[[email protected] conf]# vim nginx.conf.oldboy.20170819 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

新增bbs站點後的nginx.conf配置文件為:

[[email protected] conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }
}

修改配置文件後,需要建立站點相關文件

[[email protected] conf]# mkdir ../html/{www,bbs} -p    #相當於在/application/nginx/html/目錄下建立www和bbs兩個目錄
[[email protected] conf]# echo "www" >../html/www/index.html    #在www首頁index.html中寫入www字母
[[email protected] conf]# echo "bbs" >../html/bbs/index.html    #在bbs首頁index.html中寫入bbs字母


3 nginx配置文件檢查語法查看是否有錯誤:/application/nginx/sbin/nginx -t

[[email protected] conf]# /application/nginx/sbin/nginx -t    #自檢查看是否有錯誤
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

根據提示nginx配置文件檢查語法結果成功

4 nginx服務平滑重啟

[[email protected] conf]# /application/nginx/sbin/nginx -s reload

apche,nginx等如果平滑重啟reload失敗那麽就用stop和start,禁止用restart

[[email protected] conf]# /application/nginx/sbin/nginx -s stop    #stop是帶-s stop
[[email protected] conf]# /application/nginx/sbin/nginx    #start是直接nginx就是start

5 檢查nginx服務80端口是否啟動

[[email protected] conf]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   5784 root    6u  IPv4  23562      0t0  TCP *:http (LISTEN)
nginx   5785  www    6u  IPv4  23562      0t0  TCP *:http (LISTEN)

6 在linux中驗證nginx服務是否可用(當然也可以再windows中驗證)

6.1 在管理機m01機器上面本機hosts文件中添加172.16.1.8的dns解析172.16.1.8 web01 www.etiantian.org bbs.etiantian.org

[[email protected] ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5      lb01
172.16.1.6      lb01
172.16.1.7      web02
172.16.1.8      web01 www.etiantian.org bbs.etiantian.org
172.16.1.51     de01 db01.etiantian.org
172.16.1.31     nfs01
172.16.1.41     backup
172.16.1.61     m01

6.2 然後ping www.etiantian.org和bbs.etiantian.org看是否可用解析到172.16.1.8這個ip地址。

[[email protected] ~]# ping -c4 www.etiantian.org
PING web01 (172.16.1.8) 56(84) bytes of data.
64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.629 ms
64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.432 ms
64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.439 ms
64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.654 ms
--- web01 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.432/0.538/0.654/0.105 ms
[[email protected] ~]# ping -c4 bbs.etiantian.org
PING web01 (172.16.1.8) 56(84) bytes of data.
64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.896 ms
64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.459 ms
64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.782 ms
64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.445 ms
--- web01 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.445/0.645/0.896/0.199 ms

6.3 curl 兩個虛擬主機站點名字,看是否可用返回對應的www和bbs內容(首頁index.html裏面的內容)

[[email protected] ~]# curl www.etiantian.org
www
[[email protected] ~]# curl bbs.etiantian.org
bbs

7 windows瀏覽器中驗證

7.1 在windows主機hosts文件中添加dns解析

在C:\Windows\System32\drivers\etc\hosts中添加一條主機解析:

10.0.0.8 www.etiantian.org bbs.etiantian.org

7.2 在瀏覽器中分別輸入www.etiantian.org 和bbs.etiantian.org可以返回對應的首頁內容www和bbs,說明新增域名虛擬主機成功。

技術分享

技術分享


本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1957720

nginx基於域名的虛擬主機配置實戰