关于Nginx默认监听端口不是80而造成程序出错的简单问题

关于Nginx默认监听端口不是80而造成程序出错的简单问题

一、遇到的问题

code:

comments = []
@app.route("/", methods=["GET", "POST"])
def index():
   if request.method == "GET":
     return render_template("index.html", comments=comments)
   #如果是post:
   comments.append(request.form["contents"])
   return redirect(url_for('index'))

场景还原:我的服务器没开80端口,我用nginx listen 81端口。利用flask框架(上述代码)遇到url_for('index')定向到 xxx.cn。可是我需要定向到 xxx.cn:81。(手动访问xxx.cn:81都是可以的。)
url_for()的参数上下了一会功夫,没反应。仔细想一下这应该是前端问题,可能 Nginx 有相应的办法。
首先我参考 http://www.cnblogs.com/kevingrace/p/8073646.html 尝试添加 proxy_redirect http://ip/ http://ip:81/;,还是不行。。。

二、解决办法

参考 http://blog.sina.com.cn/s/blog_646085780102wuxp.html
正确方式:配置 /Nginx.conf-http-server-location 添加如下语句:

proxy_set_header Host $host:81;

三、后记

看到Nginx.conf还有很多其他配置选项我现在还没用到,只要思路清晰,以后遇到还是可以一步步加进来。

原文地址:https://www.cnblogs.com/aubucuo/p/nginx1.html