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


轉載自https://www.jianshu.com/p/7a8a7eb3707a

1、瀏覽器直接訪問服務,獲取到的 Host 包含瀏覽器請求的 IP 和端口

測試服務器,centos 7

sudo pip install --upgrade pip

sudo pip install flask

把如下代碼放到文件ngx_header.py, 並用python運行如下腳本,

IP 是 eth0的IP,請根據自己的服務器,做相應的修改, 筆者使用的是阿里雲服務器,有公網IP,公網IP映射到本地eth0就是172.31.5.0

# 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='172.31.5.0', port=5000)

# python ngx_header.py

結果如下:

 

 

 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://172.31.5.0: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://172.31.5.0: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://172.31.5.0: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://172.31.5.0: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='172.31.5.0', port=5000)
// foo.html 代碼
# cat templates/foo.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>foo</title>
</head>
<body>
Host: {{ Host }}
</br>
<a href="2bar"">頁面跳轉</a>
</body>
</html>

// bar.html 代碼
# cat templates/bar.html 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>bar</title>
</head>
<body>
Host: {{ Host }}
</body>
</html>

# python ngx_header.py 

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

結果如下:

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM