CentOS 7.4 Tengine安装配置详解(六)

十五、反向代理:

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

blob.png


4、修改本地Windows 10系统的hosts文件,C:\Windows\System32\drivers\etc\hosts,末尾新增代码:192.168.1.222 node1.qiuyue.com,访问http://node1.qiuyue.com

blob.png


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

blob.png


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.comhttp://node1.qiuyue.com/bbs


blob.png

blob.png


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.comhttp://node1.qiuyue.com/forum


blob.png

blob.png


8、将以jpgpng或者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


blob.png

blob.png

(5)本地Windows 10访问http://node1.qiuyue.comhttp://node1.qiuyue.com/images/1.jpg


blob.png

blob.png


9、定义访问日志:

(1)node2的默认访问日志:# tail -3 /var/log/httpd/access_log

blob.png

备注:并不是记录访问的本地Windows 10IP,而是node1IP

(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;

       }

}

# 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.jpghttp://node1.qiuyue.com

(5)node2修改后的访问日志:# tail -5 /var/log/httpd/access_log

blob.png

Windows 10IP

blob.png

备注:已记录访问的本地Windows 10IP,而不是node1IP


猜你喜欢

转载自blog.51cto.com/qiuyue/2126740