本文演示一下nginx的http代理功能, 更多详细配置查看proxy模块reference.
nginx的安装参考 :
在172.16.3.150服务器上配置代理 :
例如请求 http://172.16.3.150将代理页面发到 http://172.16.3.67
# cd /opt/nginx1.6.0/conf/
# less nginx.conf
http {
...
server {
listen 80;
server_name localhost default_server; # default_server表示当前http配置内, 所有server都无法匹配时的默认配置.
# 如果没有配置default_server, 默认选择排第一的server作为默认的配置.
...
location / { # / 表示匹配所有URI. (不包括http://server_name:port)
proxy_pass http://172.16.3.67; # 表示代理到http://172.16.3.67
#root html;
#index index.html index.htm;
}
...
}
...
}
nginx -s reload
例如请求 http://172.16.3.150将代理页面发到 http://172.16.3.67
例如, 访问代理站点
http://172.16.3.150
:
直接访问主站.
使用tcpdump可以跟踪到http代理服务器172.16.3.150和后端的web server 172.16.3.67的数据流.
[参考]
[root@db-172-16-3-150 ~]# tcpdump -i em1|grep 172.16.3.67
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on em1, link-type EN10MB (Ethernet), capture size 65535 bytes
23:43:57.434669 IP db-172-16-3-150.sky-mobi.com.36179 > 172.16.3.67.http: Flags [S], seq 3777998277, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
23:43:57.434955 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36179: Flags [S.], seq 3245637592, ack 3777998278, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
23:43:57.434979 IP db-172-16-3-150.sky-mobi.com.36179 > 172.16.3.67.http: Flags [.], ack 1, win 115, length 0
23:43:57.435011 IP db-172-16-3-150.sky-mobi.com.36179 > 172.16.3.67.http: Flags [P.], seq 1:707, ack 1, win 115, length 706
23:43:57.435241 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36179: Flags [.], ack 707, win 126, length 0
23:43:57.651972 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36179: Flags [P.], seq 1:335, ack 707, win 126, length 334
23:43:57.651994 IP db-172-16-3-150.sky-mobi.com.36179 > 172.16.3.67.http: Flags [.], ack 335, win 123, length 0
23:43:57.652043 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36179: Flags [F.], seq 335, ack 707, win 126, length 0
23:43:57.652078 IP db-172-16-3-150.sky-mobi.com.36179 > 172.16.3.67.http: Flags [F.], seq 707, ack 336, win 123, length 0
23:43:57.652340 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36179: Flags [.], ack 708, win 126, length 0
23:44:29.255686 IP db-172-16-3-150.sky-mobi.com.36182 > 172.16.3.67.http: Flags [S], seq 499805544, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
23:44:29.255968 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36182: Flags [S.], seq 2904580936, ack 499805545, win 14600, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
23:44:29.255996 IP db-172-16-3-150.sky-mobi.com.36182 > 172.16.3.67.http: Flags [.], ack 1, win 115, length 0
23:44:29.256032 IP db-172-16-3-150.sky-mobi.com.36182 > 172.16.3.67.http: Flags [P.], seq 1:752, ack 1, win 115, length 751
23:44:29.256274 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36182: Flags [.], ack 752, win 126, length 0
23:44:29.891224 IP 172.16.3.67.http > db-172-16-3-150.sky-mobi.com.36182: Flags [.], seq 1:2921, ack 752, win 126, length 2920[参考]