url_for() didn't work will when I use nginx

256 views Asked by At

The code in my template is this:

<a href="{{ url_for('.post',id=post.id) }}"><h2 >{{ post.title }}</h2></a>

My blog url is localhost:5000/index/all,It looks like(use app.run()):

enter image description here

The url for first post is localhost:5000/post/10

But if I use fcgi and nginx, it look like this:

enter image description here

The url for first post is localhost:5000/index/all/post/10 which not exist actually.

Why url_for() didn't return right url when I use nginx?

1

There are 1 answers

0
大易归真 On

I also ask this question in Github.Thanks for miguelgrinberg's help,it's solved

FastCGI tends to have issues like this, where for odd reasons the CGI environment variables passed to the application are set incorrectly.

I add two line in manage.py and it works well in nginx.

from werkzeug.contrib.fixers import CGIRootFix


app = CGIRootFix(app,app_root='/')