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
很少使用,保持域名和端口一樣。
就兩個解決方案:
- 添加物理網卡
實際上物理端口通常只有4個,當主機上面有10台虛擬站點的時候使用添加物理網卡單獨配置IP來實現nginx虛擬主機就不適用了。
- 給一塊網卡添加多個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;
}
}
FBI WARNING
QQ:1402122292 認准原創sheldon 別人叫我曉東
