問題描述:
nginx部署之后一般的跳轉(zhuǎn)會是https://域名
但是flask重定向之后會訪問http://域名:443,這時就會出現(xiàn)問題
nginx原先的配置:
upstream assetvul {
? ? ? ? server 127.0.0.1:5010;#項(xiàng)目IP和端口
? ? ? ? }
server {
? ? ? ? listen 443 ssl;
server_name?example.xxx.com;#域名
ssl_protocols? ? ? TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers? ? ? ? AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate? /opt/app/openresty/nginx/conf/ssl/xxx.com.crt;#證書
ssl_certificate_key /opt/app/openresty/nginx/conf/ssl/xxx.com.key;
ssl_session_cache? shared:SSL:10m;
ssl_session_timeout 10m;
access_log? /opt/log/nginx/xxx.access.log? main;
location / {
? ? ? ? ? ? proxy_redirect off;
? ? ? ? ? ? proxy_set_header Host $host:$server_port;
? ? ? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? client_max_body_size 50m;
? ? ? ? ? ? client_body_buffer_size 256k;
? ? ? ? ? ? proxy_connect_timeout 30;
? ? ? ? ? ? proxy_send_timeout 30;
? ? ? ? ? ? proxy_read_timeout 60;
? ? ? ? ? ? proxy_buffer_size 256k;
? ? ? ? ? ? proxy_buffers 4 256k;
? ? ? ? ? ? proxy_busy_buffers_size 256k;
? ? ? ? ? ? proxy_temp_file_write_size 256k;
? ? ? ? ? ? proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
? ? ? ? ? ? proxy_max_temp_file_size 128m;
? ? proxy_pass http://example;
}
}
這個配置一般來說是正常的,但是總會遇到特殊情況,所以
解決方法1:
在nginx配置添加兩個頭部
proxy_set_headerX-Scheme$scheme;
proxy_set_headerX-Forwarded-Proto$scheme;
如果還不行,讀取請求的X_Forwarded_Proto頭部來獲取協(xié)議參考代碼:
from flask import Flask
from werkzeug.contrib.fixers impor tProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
解決方法2:
當(dāng)然能改nginx配置的就盡量改nginx配置,從我的角度我是不想改代碼的,所以就只能讓nginx多跳幾次
upstream assetvul {
? ? ? ? server 127.0.0.1:5010;#項(xiàng)目IP端口
? ? }
? #=================================================================
? ? server {
? ? ? ? listen 80;
? ? ? ? server_name ?example.xxx.com;#域名
? ? ? ? rewrite ^(.*)$ https://$host$1 permanent;
? ? }
? ? #=================================================================
? ? server {
? ? ? ? listen 443 ssl;
? ? ? ? server_name example.xxx.com;#域名
? ? ? ? ssl_protocols? ? ? TLSv1 TLSv1.1 TLSv1.2;
? ? ? ? ssl_ciphers? ? ? ? AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
? ? ? ? ssl_certificate? ? /opt/app/openresty/nginx/conf/ssl/xxx.com.crt;
? ? ? ? ssl_certificate_key /opt/app/openresty/nginx/conf/ssl/xxx.com.key;
? ? ? ? ssl_session_cache? shared:SSL:10m;
? ? ? ? ssl_session_timeout 10m;
? ? ? ? access_log /opt/log/nginx/example.access.log main;
? ? ? ? location / {
? ? ? ? ? ? proxy_redirect off;
? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ? ? client_max_body_size 50m;
? ? ? ? ? ? client_body_buffer_size 256k;
? ? ? ? ? ? proxy_connect_timeout 30;
? ? ? ? ? ? proxy_send_timeout 30;
? ? ? ? ? ? proxy_read_timeout 60;
? ? ? ? ? ? proxy_buffer_size 256k;
? ? ? ? ? ? proxy_buffers 4 256k;
? ? ? ? ? ? proxy_busy_buffers_size 256k;
? ? ? ? ? ? proxy_temp_file_write_size 256k;
? ? ? ? ? ? proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
? ? ? ? ? ? proxy_max_temp_file_size 128m;
? ? ? ? ? ? proxy_pass http://example;
? ? ? ? }
? ? }
以上兩種親測有效
參考文章: