nginx配置基於域名、端口、IP的虛擬主機

一、基於域名的虛擬主機:html

  絕大多數企業對外提供服務的網站使用的都是基於域名的主機,經過不一樣的域名區分不一樣的虛擬主機。nginx

首先咱們進入安裝nginxd的目錄下:/application/nginx-1.6.3/confvim

咱們去除掉默認配置文件裏的註釋和空行並重定向到nginx.conf文件裏,同時咱們須要配置以下:windows

egrep -v "#|^$" nginx.conf.default >nginx.conf   //去掉包含#號和空行的內容瀏覽器

[root@lamp01 conf]# vim 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.jyw1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
     server {
        listen       80;
        server_name  bbs.jyw2.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
         }
     }
}

建立站點目錄文件:安全

mkdir ../html/{www,bbs} -p  網絡

[root@lamp01 conf]# tree ../html/
../html/
├── 50x.html
├── bbs
│   └── index.html
├── index.html
└── www
     └── index.html

 爲站點目錄生成首頁並追加內容:app

 經過hosts來作dns解析負載均衡

[root@lamp01 conf]# echo "www.jyw1.com" >../html/www/index.html 
[root@lamp01 conf]# echo "bbs.jyw2.com" >../html/bbs/index.html 
[root@lamp01 conf]# cat ../html/{www,bbs}/index.html 
www.jyw1.com
bbs.jyw2.com
[root@lamp01 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
[root@lamp01 conf]# /application/nginx/sbin/nginx -s reload  #刷新配置
[root@lamp01 conf]# vi /etc/hosts
[root@lamp01 conf]# ping www.jyw1.com
PING www.jyw1.com (192.168.43.118) 56(84) bytes of data.
64 bytes from www.jyw1.com (192.168.43.118): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from www.jyw1.com (192.168.43.118): icmp_seq=2 ttl=64 time=0.020 ms
^C
--- www.jyw1.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1280ms
rtt min/avg/max/mdev = 0.020/0.021/0.022/0.001 ms
[root@lamp01 conf]# ping bbs.jyw2.com
PING bbs.jyw2.com (192.168.43.118) 56(84) bytes of data.
64 bytes from www.jyw1.com (192.168.43.118): icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from www.jyw1.com (192.168.43.118): icmp_seq=2 ttl=64 time=0.020 ms
^C
--- bbs.jyw2.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1311ms
rtt min/avg/max/mdev = 0.015/0.017/0.020/0.004 ms
[root@lamp01 conf]# curl www.jyw1.com
www.jyw1.com
[root@lamp01 conf]# curl bbs.jyw2.com
bbs.jyw2.com
[root@lamp01 conf]# 

我是用的windows系統,配置一下host在「C:\Windows\System32\drivers\etc」下的hosts中配置一下域名重定向curl

192.168.43.118 www.jyw1.com bbs.jyw2.com

 而後cmd再ping一下這個域名是否正確指向了這個IP上

打開瀏覽器,輸入www.test.com會獲得如下結果,就說明外網訪問成功

ok,經過多域名來訪問站點,一樣也能夠一個站點多個域名。

2.基於端口的虛擬主機配置:

  此類型的虛擬主機主要應用於企業內部的網站,須要加端口號才能訪問,經過不一樣的端口來區分不一樣的虛擬主機,爲企業網站安全優化,例如:企業的網站後臺,資料共享等。

 在前面配置的基於域名的虛擬主機的基礎上只須要修改nginx.conf文件,刷新配置便可完成:

修改nginx.conf配置文件

[root@lamp01 conf]# vim 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       8001;
        server_name  www.jyw1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
     server {
        listen       8002;
        server_name  bbs.jyw2.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
         }
     }
}

刷新配置,測試結果以下

[root@lamp01 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
[root@lamp01 conf]# /application/nginx/sbin/nginx -s reload
[root@lamp01 conf]# curl www.jyw1.com
curl: (7) couldn't connect to host
[root@lamp01 conf]# curl www.jyw1.com:8001
www.jyw1.com
[root@lamp01 conf]# curl bbs.jyw2.com:8002
bbs.jyw2.com
[root@lamp01 conf]# 

在windows下測試以下:

ok,既能夠對應多個站點,多個端口訪問,也能夠對應一個站點多個端口訪問。

3.基於IP地址虛擬主機:

  經過不一樣的IP區分不一樣的虛擬主機,此類型企業應用於比較少,一般用到不通業務流中或者負載均衡上面。

 在前面配置的基於域名的虛擬主機的基礎上只須要修改nginx.conf文件,刷新配置便可完成:

修改nginx.conf配置文件

[root@lamp01 conf]# vim 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      192.168.43.118:80;
        server_name  www.jyw1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
     server {
        listen 192.168.43.88:80;
        server_name  bbs.jyw2.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
         }
     }
}

修改完配置文件後,咱們須要虛擬一塊網卡:

ip addr add  //添加

ip addr del 192.168.43.88/24  dev eth0  //刪除

臨時性網卡: ip addr add 192.168.43.88/24 label eth0:1 dev eth0  或者  ifconfig eth0:1 192.168.43.88/24 up 

[root@lamp01 conf]# ip addr add 192.168.43.88/24 label eth0:1 dev eth0  添加網卡(推薦使用ip addr)
[root@lamp01 conf]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:4D:22  
          inet addr:192.168.43.118  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe01:4d22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1100 errors:0 dropped:0 overruns:0 frame:0
          TX packets:664 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:94218 (92.0 KiB)  TX bytes:76354 (74.5 KiB)

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:01:4D:22  
          inet addr:192.168.43.88  Bcast:192.168.43.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
[root@lamp01 conf]# ip addr del 192.168.43.99/24 dev eth0   刪除網卡
[root@lamp01 conf]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:4D:22  
          inet addr:192.168.43.118  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe01:4d22/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1218 errors:0 dropped:0 overruns:0 frame:0
          TX packets:724 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:103486 (101.0 KiB)  TX bytes:83366 (81.4 KiB)
[root@lamp01 conf]# 

永久性網卡:

cd /etc/sysconfig/network-scripts/    //進入到網卡配置文件的目錄

cp ifcfg-eth0 ifcfg-eth0:1               //拷貝配置文件並重命名

vim ifcfg-eth0:1                           //編輯配置文件

/etc/init.d/network restart           //重啓網絡服務

圖略....

下面咱們刷新配置,查看效果:

[root@lamp01 ~]# /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
[root@lamp01 ~]# /application/nginx/sbin/nginx -s reload
[root@lamp01 ~]# curl 192.168.43.88
bbs.jyw2.com
[root@lamp01 ~]# curl 192.168.43.118
www.jyw1.com
[root@lamp01 ~]# 

 在windows下測試以下:

 

ok,既能夠多個IP對應一個站點,也能夠對應多個站點訪問。

相關文章
相關標籤/搜索