1. 程式人生 > 實用技巧 >nginx 的三種虛擬主機配置方法

nginx 的三種虛擬主機配置方法



nginx三種虛擬主機配置的方法.


基於埠


在生產環境中一般使用埠或者域名。

[root@web01 /etc/nginx/conf.d]# cat web01.conf 
server {
	listen 80;
	server_name 10.0.0.7;

	location / {
		root /code/web01;
		index index.html;
}
}
[root@web01 /etc/nginx/conf.d]# cat web02.conf 
server {
        listen 81;
        server_name 10.0.0.7;

        location / {
                root /code/web02;
                index index.html;
}
}

[root@web01 /etc/nginx/conf.d]# cat web03.conf 
server {
        listen 82;
        server_name 10.0.0.7;

        location / {
                root /code/web03;
                index index.html;
}
}

基於域名


[root@web01 /etc/nginx/conf.d]# cat web01.conf 
server {
	listen 80;
	server_name www.web01.com;

	location / {
		root /code/web01;
		index index.html;
}
}
[root@web01 /etc/nginx/conf.d]# cat web02.conf 
server {
        listen 80;
        server_name www.web02.com;

        location / {
                root /code/web02;
                index index.html;
}
}

[root@web01 /etc/nginx/conf.d]# cat web03.conf 
server {
        listen 80;
        server_name www.web03.com;

        location / {
                root /code/web03;
                index index.html;
}
}


基於IP


很少使用,保持域名和埠一樣。

就兩個解決方案:

  1. 新增物理網絡卡

實際上物理埠通常只有4個,當主機上面有10臺虛擬站點的時候使用新增物理網絡卡單獨配置IP來實現nginx虛擬主機就不適用了。

  1. 給一塊網絡卡新增多個ip,虛擬IP
# 1.繫結IP給eth0
[root@web01 /etc/nginx/conf.d]# ifconfig eth0:0 10.0.0.100/24
[root@web01 /etc/nginx/conf.d]# ifconfig eth0:1 10.0.0.101/24

# 2、配置檔案
[root@web01 /etc/nginx/conf.d]# cat web01.conf 
server {
	listen 80;
	server_name 10.0.0.7;

	location / {
		root /code/web01;
		index index.html;
}
}
[root@web01 /etc/nginx/conf.d]# cat web02.conf 
server {
        listen 80;
        server_name 10.0.0.100;

        location / {
                root /code/web02;
                index index.html;
}
}

[root@web01 /etc/nginx/conf.d]# cat web03.conf 
server {
        listen 80;
        server_name 10.0.0.101;

        location / {
                root /code/web03;
                index index.html;
}
}


QQ:1402122292 認準原創sheldon