欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

apache2, nginx, iis反向代理简单配置

程序员文章站 2022-06-25 12:08:27
...

测试配置

  • 后端网站地址192.168.1.100,运行在物理机
  • 虚拟机server2008192.168.241.141
  • 虚拟机ubuntu16192.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官网

https://www.digitalocean.com/community/tutorials/how-to-use-apache-as-a-reverse-proxy-with-mod_proxy-on-ubuntu-16-04

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 RewriteApplication Request Routing

安装完毕后应该有如下图标
apache2, nginx, iis反向代理简单配置

编辑默认站点重定向

图片中地址依据本次试验应填写192.168.1.100:5000
apache2, nginx, iis反向代理简单配置

试验网站

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
apache2, nginx, iis反向代理简单配置

nginx
apache2, nginx, iis反向代理简单配置

IIS
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可能需要额外配置,需要查询相关资料