访问 [https://www.oyohyee.com/admin]
会301重定向到 [http://www.oyohyee.com,www.oy...]
使用curl -IL https://www.oyohyee.com/admin
得到如下信息
HTTP/1.1 301 MOVED PERMANENTLY
Server: nginx/1.12.2
Date: Thu, 12 Apr 2018 00:45:21 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 297
Connection: keep-alive
Location: http://www.oyohyee.com,www.oyohyee.com/admin/
(这里是nginx进行的跳转,同时跳转后的链接是http协议)
初步判断是nginx的问题,但是不知道应该怎么解决,并且很好奇为什么会有这个跳转
希望能够按照flask规则跳转到[https://www.oyohyee.com/admin/]
相关信息如下:
centos + nginx + gunicorn + flask
flask路由函数
@app.route('/admin/')
def admin():
return render_template("admin/admin.html")
flask重定向规则
唯一 URL / 重定向行为
Flask 的 URL 规则基于 Werkzeug 的路由模块。这个模块背后的思想是基于 Apache
以及更早的 HTTP 服务器主张的先例,保证优雅且唯一的 URL。 以这两个规则为例:@app.route('/projects/') def projects(): return 'The project page' @app.route('/about') def about(): return 'The about page'
虽然它们看起来着实相似,但它们结尾斜线的使用在 URL 定义 中不同。 第一种情况中,指向 projects 的规范 URL 尾端有一个斜线。这种感觉很像在文件系统中的文件夹。访问一个结尾不带斜线的 URL 会被
Flask 重定向到带斜线的规范 URL 去。 然而,第二种情况的 URL 结尾不带斜线,类似 UNIX-like
系统下的文件的路径名。访问结尾带斜线的 URL 会产生一个 404 “Not Found” 错误。 这个行为使得在遗忘尾斜线时,允许关联的
URL 接任工作,与 Apache 和其它的服务器的行为并无二异。此外,也保证了 URL 的唯一,有助于避免搜索引擎索引同一个页面两次。
nginx log信息
"HEAD /admin HTTP/1.1" 301 0 "-" "curl/7.29.0" "-"
nginx配置信息
server {
listen 80;
server_name www.oyohyee.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl http2 default_server;
server_name www.oyohyee.com;
ssl_certificate "/etc/nginx/ssl/1_www.oyohyee.com_bundle.crt";
ssl_certificate_key "/etc/nginx/ssl/2_www.oyohyee.com.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ^~ / {
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Host $http_host;
}
location ^~ /static/ {
root /data/OBlog/OBlog/front/;
}
}
1、301跳转的
Location
的域名重复并且多了一个逗号的原因是,nginx反代时多了一个Host
头去掉下方的即可
2、另外你得让Python代码知道协议是https
1) 试下在nginx添加这两个头部
2) 如果无效,还需要调整一下代码,读取请求的
X_Forwarded_Proto
头部来获取协议参考代码: