版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeoForBest/article/details/80469855
测试配置
- 后端网站地址
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
扫描二维码关注公众号,回复: 4526602 查看本文章
试验网站
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可能需要额外配置,需要查询相关资料