1、演示环境:
IP | 操作系统 | 节点 | 角色 |
192.168.1.222 | CentOS 7.4 | node1 | Tengine服务器 |
192.168.1.144 | CentOS 6.9 | node2 | Apache服务器 |
2、node2安装Apache服务,并创建测试页:
# yum -y install httpd
# mkdir -pv /var/www/html/bbs
# echo "<h3>httpd on node2</h3>" > /var/www/html/index.html
# echo "<h3>bbs on node2</h3>" > /var/www/html/bbs/index.html
# service httpd start
# ss -tunlp | grep :80
3、node1访问node2:# curl http://192.168.1.144
4、修改本地Windows 10系统的hosts文件,C:\Windows\System32\drivers\etc\hosts,末尾新增代码:192.168.1.222 node1.qiuyue.com,访问http://node1.qiuyue.com:
5、将所有请求都反代至http://192.168.1.144:
(1)修改node1配置文件nginx.conf:
server {
listen 80;
server_name node1.qiuyue.com;
location / {
proxy_pass http://192.168.1.144;
index index.html index.html;
}
}
(2)重载服务:# nginx -t # nginx -s reload
(3)本地Windows 10访问http://node1.qiuyue.com
6、将/bbs的请求反代至http://192.168.1.144/bbs:
(1)修改node1配置文件nginx.conf:
server {
listen 80;
server_name node1.qiuyue.com;
location / {
root html;
index index.html index.html;
}
location /bbs {
proxy_pass http://192.168.1.144/bbs;
index index.html index.html;
}
}
(2)重载服务:# nginx -t # nginx -s reload
(3)本地Windows 10访问http://node1.qiuyue.com、http://node1.qiuyue.com/bbs
7、将/forum的请求反代至http://192.168.1.144/bbs:
(1)修改node1配置文件nginx.conf:
server {
listen 80;
server_name node1.qiuyue.com;
location / {
root html;
index index.html index.html;
}
location /forum {
proxy_pass http://192.168.1.144/bbs;
index index.html index.html;
}
}
(2)重载服务:# nginx -t # nginx -s reload
(3)本地Windows 10访问http://node1.qiuyue.com、http://node1.qiuyue.com/forum
8、将以jpg、png或者gif结尾的请求反代至http://192.168.1.144:
(1)修改node1配置文件nginx.conf:
server {
listen 80;
server_name node1.qiuyue.com;
location / {
root html;
index index.html index.html;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://192.168.1.144;
}
}
(2)重载服务:# nginx -t # nginx -s reload
(3)node2创建测试目录:# mkdir -pv /var/www/html/images
(4)node2上传测试图片1.jpg至/var/www/html/images
(5)本地Windows 10访问http://node1.qiuyue.com、http://node1.qiuyue.com/images/1.jpg
9、定义访问日志:
(1)node2的默认访问日志:# tail -3 /var/log/httpd/access_log
备注:并不是记录访问的本地Windows 10的IP,而是node1的IP
(2)修改node1配置文件nginx.conf:
server {
listen 80;
server_name node1.qiuyue.com;
location / {
root html;
index index.html index.html;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://192.168.1.144;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
(2)重载服务:# nginx -t # nginx -s reload
(3)修改node2配置文件httpd.conf:
确认访问日志使用的是CustomLog logs/access_log combined
将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
# service httpd restart
(4)本地Windows 10访问http://node1.qiuyue.com/images/1.jpg、http://node1.qiuyue.com
(5)node2修改后的访问日志:# tail -5 /var/log/httpd/access_log
Windows 10的IP:
备注:已记录访问的本地Windows 10的IP,而不是node1的IP
原文地址:http://blog.51cto.com/qiuyue/2126740