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
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
CentOS 7.4 Tengine安裝配置詳解(六)