apache2, nginx, iis反向代理簡單配置
阿新 • • 發佈:2018-12-16
測試配置
- 後端網站地址
192.168.1.100
,執行在物理機 - 虛擬機器server2008
192.168.241.141
- 虛擬機器ubuntu16
192.168.241.132
安裝apache2和nginx
Apache2.4
配置環境為ubuntu 16.04 server
啟用反向代理模組
sudo a2enmod proxy
# 如果沒有該模組
# apt-get install libapache2-mod-proxy*
systemctl restart apache2
複製一份預設虛擬站點檔案000-default.conf,新增以下內容
# ProxyPreserveHost On 填寫原始的HOST到後端伺服器
ProxyPreserveHost On
ProxyPass / http://192.168.1.100:5000/
ProxyPassReverse / http://192.168.1.100:5000/
更多詳細內容可參考 或 apache官網
nginx
修改default配置中location /
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
proxy_pass http://192.168.1.100:5000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set _header Host $http_host;
}
重啟nginx
IIS
安裝IIS模組URL Rewrite
和Application Request Routing
- http://download.microsoft.com/download/E/9/8/E9849D6A-020E-47E4-9FD0-A023E99B54EB/requestRouter_amd64.msi
- http://download.microsoft.com/download/E/A/9/EA9F19BC-0EEB-49C9-B32D-56852BBE56DA/rewrite_amd64_zh-CN.msi
安裝完畢後應該有如下圖示
編輯預設站點重定向
圖片中地址依據本次試驗應填寫
192.168.1.100:5000
試驗網站
192.168.1.100:5000
# coding=utf-8
#
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
s = ''
for _ in request.headers:
k, v = _
s += '<p>%s: %s</p>' % (k, v)
return '%s' % s
app.run(host='0.0.0.0')
不同代理伺服器對應的訊息頭
apache2
nginx
IIS
總結
保留原始Host
# apache2
ProxyPreserveHost On
# nginx
proxy_set_header Host $http_host;
獲取原始客戶端訪問ip
都可以從X-Forwarded-For
中提取第一個ip值
Apache2 和IIS設定X-Real-IP
值獲取客戶端ip可能需要額外配置,需要查詢相關資料