nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比

1、浏览器直接访问服务,获取到的 Host 包含浏览器请求的 IP 和端口

# cat ngx_header.py 
from flask import Flask, request, jsonify
app = Flask(__name__)


@app.route('/')
def get_host():
    host = request.headers.get('Host')
    return jsonify({'Host': host}), 200


if __name__ == '__main__':
    app.run(host='10.1.200.107', port=5000)

# python ngx_header.py

结果如下:

nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比_第1张图片

2、配置 nginx 代理服务后
2.1 不设置 proxy_set_header Host 时,浏览器直接访问 nginx,获取到的 Host 是 proxy_pass 后面的值,即 $proxy_host 的值,参考 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

# cat ngx_header.conf 
server {
    listen 8090;
    server_name _;
    location / {
        proxy_pass http://10.1.200.107:5000;
    }
}

结果如下:


2.2 设置 proxy_set_header Host $host 时,浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息

# cat ngx_header.conf 
server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://10.1.200.107:5000;
    }
}

结果如下:



2.3 设置 proxy_set_header Host $host:$proxy_port 时,浏览器直接访问 nginx,获取到的 Host 是 $host:$proxy_port 的值

# cat ngx_header.conf 
server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $host:$proxy_port;
        proxy_pass http://10.1.200.107:5000;
    }
}

结果如下:


2.4 设置 proxy_set_header Host $http_host 时,浏览器直接访问 nginx,获取到的 Host 包含浏览器请求的 IP 和端口

server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://10.1.200.107:5000;
    }
}

结果如下:



2.5 设置 proxy_set_header Host $host 时,浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息。此时代码中如果有重定向路由,那么重定向时就会丢失端口信息,导致 404

# tree .
.
├── ngx_header.py
└── templates
    ├── bar.html
    └── foo.html

1 directory, 3 files

// ngx_header.py 代码
# cat ngx_header.py 
from flask import Flask, request, render_template, redirect
app = Flask(__name__)


@app.route('/')
def get_header():
    host = request.headers.get('Host')
    return render_template('foo.html',Host=host)


@app.route('/bar')
def get_header2():
    host = request.headers.get('Host')
    return render_template('bar.html',Host=host)


@app.route('/2bar')
def get_header3():
    # 代码层实现的重定向
    return redirect('/bar')


if __name__ == '__main__':
    app.run(host='10.1.200.107', port=5000)

// foo.html 代码
# cat templates/foo.html 



    
    foo


Host: {{ Host }}

页面跳转 // bar.html 代码 # cat templates/bar.html bar Host: {{ Host }} # python ngx_header.py # cat ngx_header.conf server { listen 8090; server_name _; location / { proxy_set_header Host $host; proxy_pass http://10.1.200.107:5000; } }

结果如下:


nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比_第2张图片

nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比_第3张图片

nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比_第4张图片

你可能感兴趣的:(nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比)