nginx反向代理+缓存配置

in web with 0 comment

实验环境:3台centos6.5

一台nginx服务器:192.168.213.50
一台web1:192.168.213.3
一台web2:192.168.213.5
nginx服务器安装nginx:
下载就省略了。
直接编译安装:
#cd /usr/local/src/nginx
#yum install -y pcre pcre-devel
#./configure --prefix=/usr/local/nginx --with-pcre
#make && make install
#cd /usr/local/nginx
两台web服务器安装httpd
#yum install -y httpd
分别写入测试文件:
[root@web1 ~]# echo "

web1.test.com

" > /var/www/html/index.html

[root@web2 ~]# echo "

web2.test.com

" > /var/www/html/index.html

反向代理配置:
(1)代理一台服务器:
#vi conf/nginx.conf

location / {

proxy_pass http://192.168.213.5;

proxy_set_header X-Real-IP $remote_addr;

}

测试方法:

浏览器访问代理服务器:http://192.168.213.50

页面显示:

web2.test.com

//说明代理成功,查看web2中httpd的访问日志,可查看IP访问地址。

开启访问日志vi /etc/httpd/conf/httpd.conf

找到LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

替换为:

LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

(2)两台web实现反向代理+负载均衡:

upstream webserver {
server 192.168.213.3 weight=1; #权重各为1,表示访问比例相同。
server 192.168.213.5 weight=1;
}
server {
listen 80;
server_name localhost:80;
location / {
proxy_pass http://webserver;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# access_log /home/logs/aaa_access.log combined;
}
测试:访问http://192.168.213.50
出现web1和web2的页面交替出现。//说明代理成功。
Responses