nginx配置虚拟主机,代理服务器
基于IP的虚拟主机
IP地址如下:
eth0 192.168.10.10
web1
eth0:0192.168.10.40 web2
nginx 配置文件修改如下:
35
server {
36
listen 192.168.1.10:80;
37
server_name web1;
38
location / {
39
root /www/html/web1;
40
index index.php index.html index.htm;
41
}
42
}
43
server {
44
listen 192.168.1.20:80;
45
server_name web2;
46
location / {
47
root /www/html/web2;
48
index index.php index.html index.htm;
49
}
50
}
测试如下:
基于端口的虚拟主机
192.168.10.10:80
web1
192.168.10.10:800 web2
nginx 配置文件修改如下:
35
server {
36
listen 80;
37
server_name web1;
38
location / {
39
root /www/html/web1;
40
index index.php index.html index.htm;
41
}
42
}
43
server {
44
listen 800;
45
server_name web2;
46
location / {
47
root /www/html/web2;
48
index index.php index.html index.htm;
49
}
50
}
测试如下:
基于域名的虚拟主机
192.168.10.10
www.sh.com
192.168.10.10 bbs.sh.com
本文为了方便在hosts文件中添加如上内容
nginx 配置文件修改如下:
35
server {
36
listen 80;
37
server_name www.sh.com;
38
location / {
39
root /www/html/web1;
40
index index.php index.html index.htm;
41
}
42
}
43
server {
44
listen 80;
45
server_name bbs.sh.com;
46
location / {
47
root /www/html/web2;
48
index index.php index.html index.htm;
49
}
50
}
测试如下
反向代理服务器
nginx 作为反向代理服务器,以实现负载均衡,和动,静态页面分离,apache1,apache2 作为http服务器,IP配置如上
nginx 配置静态页面index.html 显示web
apache1 配置php页面test.php 显示 apache1
apache1 配置虚拟目录discuz 安装有discuz
apache2 配置php页面test.php 显示 apache2
访问静态页面直接使用nginx的页面,访问php页面使用代理
不同请求的负载均衡
访问discuz定位于apache1
访问php页面定位于apache2
nginx 配置文件如下:
在server模块添加如下内容
location ^~ /discuz/ { # (^~表示不实用正则表达式匹配,直接匹配)
proxy_pass http://192.168.10.20;
}
location ~ \.php$ {
proxy_pass http://192.168.10.30:80; # 正则表达式匹配.php结尾的文件
}
测试如下:
同一请求的负载均衡
对于同一页面test.php 实现负载均衡(实际上apache1,apache2 使用相同的数据,可以考虑使用存储服务器实现数据同步)
nginx 配置文件如下:
在http模块添加如下内容:
upstream cluster1 {
server 192.168.10.20:80; #群集服务器1
server 192.168.10.30:80; #群集服务器2
}
在server模块添加如下内容
location ~ \.php$ {
proxy_pass http://cluster1; #定义的群集名字
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
对于apache1,和apache2 也可以设置权重如下:
upstream cluster1 {
server 192.168.10.20:80 weight=4; #群集服务器1 处理4/5的请求
server 192.168.10.30:80 ; #群集服务器2 处理1/5的请求
}
测试如下:
转载于:https://blog.51cto.com/zyfhlegend/702230