Hi,
I need help on nginx fcgi settings due to url_for always get wrong url
path.
1. my app organization
myapp/
myapp/
__init__.py
models/
templates/
users/
index.html
hello.html
say_hi.html
views/
users.py
__init__.py
static/
css/
images/
javascripts/
2. source code
# ---- myapp/__init__.py
...
from myapp.views.users import users as users_view
app.register_module(users_view)
...
# ----- myapp/views/users.py
users = Module(__name__)
@users.route('/')
def index():
# ....
@users.route('/hello/')
def hello():
# ....
@users.route('/hello/<name>/')
def say_hi(name):
# .....
# --- myapp/templates/users/index.html
url_for('index') ---> /
url_for('hello') ---> /hello
url_for('.static', filename='css/main.css') --> /static/css/main.css
# --- myapp/templates/users/hello.html
url_for('index') ---> /hello
url_for('hello') ---> /hello/hello (tried url_for('.users.index'),
same result)
url_for('say_hi', name = name) --> /hello/say_hi/<name>
url_for('.static', filename='css/main.css') -->
/hello/static/css/main.css
3. nginx settings
#------------ /opt/nginx/conf/sites-available/myapp
----------------------------------------
fastcgi_cache_path /opt/nginx/cache/myapp
levels=1:2 keys_zone=flask:50m;
server {
listen 81;
server_name paperless;
access_log /opt/nginx/logs/myapp.access_log;
error_log /opt/nginx/logs/myapp.error_log warn;
root /var/www/myapp/myapp;
location / {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "";
fastcgi_param PATH_INFO $path_url;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass unix:/tmp/myapp-fcgi.sock;
fastcgi_cache myapp;
fastcgi_cache_key $server_addr$request_uri;
fastcgi_cache_valid any 1m;
fastcgi_hide_header Set-Cookie;
}
location /static/ {
root /var/www/myapp/myapp;
}
}
I'm not familliar with nginx settings, but I think it's because of nginx
setting problem results in the wrong url path.
Anyone can help me on that?
Thanks a lot.
Lix Xu
Hi, It seems that no one can help on this. Lix Xu On Fri, May 28, 2010 at 9:12 AM, Lix Xu <xuzenglin@gmail.com> wrote: > Hi, > I need help on nginx fcgi settings due to url_for always get wrong url > path. > PS: It works well under built-in server . > 1. my app organization > myapp/ > myapp/ > __init__.py > models/ > templates/ > users/ > index.html > hello.html > say_hi.html > views/ > users.py > __init__.py > static/ > css/ > images/ > javascripts/ > 2. source code > # ---- myapp/__init__.py > ... > from myapp.views.users import users as users_view > app.register_module(users_view) > ... > > # ----- myapp/views/users.py > users = Module(__name__) > @users.route('/') > def index(): > # .... > > @users.route('/hello/') > def hello(): > # .... > > @users.route('/hello/<name>/') > def say_hi(name): > # ..... > > # --- myapp/templates/users/index.html > url_for('index') ---> / > url_for('hello') ---> /hello > url_for('.static', filename='css/main.css') --> /static/css/main.css > > # --- myapp/templates/users/hello.html > url_for('index') ---> /hello > url_for('hello') ---> /hello/hello (tried url_for('.users.index'), > same result) > url_for('say_hi', name = name) --> /hello/say_hi/<name> > url_for('.static', filename='css/main.css') --> > /hello/static/css/main.css > > 3. nginx settings > #------------ /opt/nginx/conf/sites-available/myapp > ---------------------------------------- > fastcgi_cache_path /opt/nginx/cache/myapp > levels=1:2 keys_zone=myapp:50m; > > server { > listen 81; > server_name paperless; > access_log /opt/nginx/logs/myapp.access_log; > error_log /opt/nginx/logs/myapp.error_log warn; > root /var/www/myapp/myapp; > location / { > include fastcgi_params; > fastcgi_param SCRIPT_FILENAME ""; > fastcgi_param PATH_INFO $path_url; > fastcgi_param PATH_INFO $fastcgi_script_name; > fastcgi_pass unix:/tmp/myapp-fcgi.sock; > fastcgi_cache myapp; > fastcgi_cache_key $server_addr$request_uri; > fastcgi_cache_valid any 1m; > fastcgi_hide_header Set-Cookie; > } > > location /static/ { > root /var/www/myapp/myapp; > } > } > > I'm not familliar with nginx settings, but I think it's because of nginx > setting problem results in the wrong url path. > > Anyone can help me on that? > > Thanks a lot. > > Lix Xu >
Hi Lix, First I recommend moving to a different type of protocol other than FCGI. In particular you should use WSGI. There are quite a few good WGSI server that you can proxy behind nginx. Gunicorn and uwsgi come to mind. On Sat, May 29, 2010 at 10:33 AM, Lix Xu <xuzenglin@gmail.com> wrote: > Hi, > It seems that no one can help on this. > > Lix Xu > > On Fri, May 28, 2010 at 9:12 AM, Lix Xu <xuzenglin@gmail.com> wrote: >> >> Hi, >> I need help on nginx fcgi settings due to url_for always get wrong url >> path. > > PS: It works well under built-in server . >> >> 1. my app organization >> myapp/ >> myapp/ >> __init__.py >> models/ >> templates/ >> users/ >> index.html >> hello.html >> say_hi.html >> views/ >> users.py >> __init__.py >> static/ >> css/ >> images/ >> javascripts/ >> 2. source code >> # ---- myapp/__init__.py >> ... >> from myapp.views.users import users as users_view >> app.register_module(users_view) >> ... >> >> # ----- myapp/views/users.py >> users = Module(__name__) >> @users.route('/') >> def index(): >> # .... >> >> @users.route('/hello/') >> def hello(): >> # .... >> >> @users.route('/hello/<name>/') >> def say_hi(name): >> # ..... >> >> # --- myapp/templates/users/index.html >> url_for('index') ---> / >> url_for('hello') ---> /hello >> url_for('.static', filename='css/main.css') --> >> /static/css/main.css >> >> # --- myapp/templates/users/hello.html >> url_for('index') ---> /hello >> url_for('hello') ---> /hello/hello (tried url_for('.users.index'), >> same result) >> url_for('say_hi', name = name) --> /hello/say_hi/<name> >> url_for('.static', filename='css/main.css') --> >> /hello/static/css/main.css >> >> 3. nginx settings >> #------------ /opt/nginx/conf/sites-available/myapp >> ---------------------------------------- >> fastcgi_cache_path /opt/nginx/cache/myapp >> levels=1:2 keys_zone=myapp:50m; >> >> server { >> listen 81; >> server_name paperless; >> access_log /opt/nginx/logs/myapp.access_log; >> error_log /opt/nginx/logs/myapp.error_log warn; >> root /var/www/myapp/myapp; >> location / { >> include fastcgi_params; >> fastcgi_param SCRIPT_FILENAME ""; >> fastcgi_param PATH_INFO $path_url; >> fastcgi_param PATH_INFO $fastcgi_script_name; >> fastcgi_pass unix:/tmp/myapp-fcgi.sock; >> fastcgi_cache myapp; >> fastcgi_cache_key $server_addr$request_uri; >> fastcgi_cache_valid any 1m; >> fastcgi_hide_header Set-Cookie; >> } >> >> location /static/ { >> root /var/www/myapp/myapp; >> } >> } >> >> I'm not familliar with nginx settings, but I think it's because of >> nginx setting problem results in the wrong url path. >> >> Anyone can help me on that? >> >> Thanks a lot. >> >> Lix Xu > >
Hi Silas, Thanks for suggestion. As I have no idea to serve WSGI under Nginx, I turned to Cherokee (which can works well for url_for). And I'm tring to find to deploy Flask using Nginx and FCGI & WSGI. Again, thanks a lot. Lix Xu On Sat, May 29, 2010 at 10:42 PM, Silas Baronda <silas.baronda@gmail.com>wrote: > Hi Lix, > > First I recommend moving to a different type of protocol other than > FCGI. In particular you should use WSGI. > > There are quite a few good WGSI server that you can proxy behind > nginx. Gunicorn and uwsgi > come to mind. > > On Sat, May 29, 2010 at 10:33 AM, Lix Xu <xuzenglin@gmail.com> wrote: > > Hi, > > It seems that no one can help on this. > > > > Lix Xu > > > > On Fri, May 28, 2010 at 9:12 AM, Lix Xu <xuzenglin@gmail.com> wrote: > >> > >> Hi, > >> I need help on nginx fcgi settings due to url_for always get wrong > url > >> path. > > > > PS: It works well under built-in server . > >> > >> 1. my app organization > >> myapp/ > >> myapp/ > >> __init__.py > >> models/ > >> templates/ > >> users/ > >> index.html > >> hello.html > >> say_hi.html > >> views/ > >> users.py > >> __init__.py > >> static/ > >> css/ > >> images/ > >> javascripts/ > >> 2. source code > >> # ---- myapp/__init__.py > >> ... > >> from myapp.views.users import users as users_view > >> app.register_module(users_view) > >> ... > >> > >> # ----- myapp/views/users.py > >> users = Module(__name__) > >> @users.route('/') > >> def index(): > >> # .... > >> > >> @users.route('/hello/') > >> def hello(): > >> # .... > >> > >> @users.route('/hello/<name>/') > >> def say_hi(name): > >> # ..... > >> > >> # --- myapp/templates/users/index.html > >> url_for('index') ---> / > >> url_for('hello') ---> /hello > >> url_for('.static', filename='css/main.css') --> > >> /static/css/main.css > >> > >> # --- myapp/templates/users/hello.html > >> url_for('index') ---> /hello > >> url_for('hello') ---> /hello/hello (tried > url_for('.users.index'), > >> same result) > >> url_for('say_hi', name = name) --> /hello/say_hi/<name> > >> url_for('.static', filename='css/main.css') --> > >> /hello/static/css/main.css > >> > >> 3. nginx settings > >> #------------ /opt/nginx/conf/sites-available/myapp > >> ---------------------------------------- > >> fastcgi_cache_path /opt/nginx/cache/myapp > >> levels=1:2 keys_zone=myapp:50m; > >> > >> server { > >> listen 81; > >> server_name paperless; > >> access_log /opt/nginx/logs/myapp.access_log; > >> error_log /opt/nginx/logs/myapp.error_log warn; > >> root /var/www/myapp/myapp; > >> location / { > >> include fastcgi_params; > >> fastcgi_param SCRIPT_FILENAME ""; > >> fastcgi_param PATH_INFO $path_url; > >> fastcgi_param PATH_INFO $fastcgi_script_name; > >> fastcgi_pass unix:/tmp/myapp-fcgi.sock; > >> fastcgi_cache myapp; > >> fastcgi_cache_key $server_addr$request_uri; > >> fastcgi_cache_valid any 1m; > >> fastcgi_hide_header Set-Cookie; > >> } > >> > >> location /static/ { > >> root /var/www/myapp/myapp; > >> } > >> } > >> > >> I'm not familliar with nginx settings, but I think it's because of > >> nginx setting problem results in the wrong url path. > >> > >> Anyone can help me on that? > >> > >> Thanks a lot. > >> > >> Lix Xu > > > > >
Just got the solution use Nginx & fastcgi to serve Flask.
my application arch is:
/hello
/hello
__init__.py
/models -> database related
/static -> static content
/images
/css
/js
/templates -> html related
/views -> controller related
the nginx conf like below, please replace hello to your application name,
and the point is the three lines:
fastcgi_split_path_info ^(/)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $fastcgi_path_info;
# nginx.conf
...
server {
listen 80;
server_name localhost;
access_log /opt/nginx/logs/hello.access_log;
error_log /opt/nginx/logs/hello.error_log warn;
location = /hello{ rewrite ^ /var/www/hello/hello/ last; }
location / { try_files $uri @hello; }
location @hello{
include fastcgi_params;
fastcgi_split_path_info ^(/)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $fastcgi_path_info;
fastcgi_pass unix:/tmp/hello.sock;
}
}
...
# /var/www/hello/hello-start.sh
#!/bin/sh
chmod 755 /var/www/hello/hello.fcgi
spawn-fcgi -u 33 -g 33 -d /var/www/hello-f /var/www/hello/hello.fcgi -s
/tmp/hello.sock
#/var/www/hello/hello.fcgi
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from flup.server.fcgi import WSGIServer
from hello import app
WSGIServer(app, bindAddress='/tmp/hello.sock').run()
Hope this is useful to you.
Regards.
Lix Xu
On Mon, May 31, 2010 at 11:45 AM, Lix Xu <xuzenglin@gmail.com> wrote:
> Hi Silas,
> Thanks for suggestion. As I have no idea to serve WSGI under Nginx, I
> turned to Cherokee (which can works well for url_for). And I'm tring to find
> to deploy Flask using Nginx and FCGI & WSGI.
> Again, thanks a lot.
>
> Lix Xu
>
>
> On Sat, May 29, 2010 at 10:42 PM, Silas Baronda <silas.baronda@gmail.com>wrote:
>
>> Hi Lix,
>>
>> First I recommend moving to a different type of protocol other than
>> FCGI. In particular you should use WSGI.
>>
>> There are quite a few good WGSI server that you can proxy behind
>> nginx. Gunicorn and uwsgi
>> come to mind.
>>
>> On Sat, May 29, 2010 at 10:33 AM, Lix Xu <xuzenglin@gmail.com> wrote:
>> > Hi,
>> > It seems that no one can help on this.
>> >
>> > Lix Xu
>> >
>> > On Fri, May 28, 2010 at 9:12 AM, Lix Xu <xuzenglin@gmail.com> wrote:
>> >>
>> >> Hi,
>> >> I need help on nginx fcgi settings due to url_for always get wrong
>> url
>> >> path.
>> >
>> > PS: It works well under built-in server .
>> >>
>> >> 1. my app organization
>> >> myapp/
>> >> myapp/
>> >> __init__.py
>> >> models/
>> >> templates/
>> >> users/
>> >> index.html
>> >> hello.html
>> >> say_hi.html
>> >> views/
>> >> users.py
>> >> __init__.py
>> >> static/
>> >> css/
>> >> images/
>> >> javascripts/
>> >> 2. source code
>> >> # ---- myapp/__init__.py
>> >> ...
>> >> from myapp.views.users import users as users_view
>> >> app.register_module(users_view)
>> >> ...
>> >>
>> >> # ----- myapp/views/users.py
>> >> users = Module(__name__)
>> >> @users.route('/')
>> >> def index():
>> >> # ....
>> >>
>> >> @users.route('/hello/')
>> >> def hello():
>> >> # ....
>> >>
>> >> @users.route('/hello/<name>/')
>> >> def say_hi(name):
>> >> # .....
>> >>
>> >> # --- myapp/templates/users/index.html
>> >> url_for('index') ---> /
>> >> url_for('hello') ---> /hello
>> >> url_for('.static', filename='css/main.css') -->
>> >> /static/css/main.css
>> >>
>> >> # --- myapp/templates/users/hello.html
>> >> url_for('index') ---> /hello
>> >> url_for('hello') ---> /hello/hello (tried
>> url_for('.users.index'),
>> >> same result)
>> >> url_for('say_hi', name = name) --> /hello/say_hi/<name>
>> >> url_for('.static', filename='css/main.css') -->
>> >> /hello/static/css/main.css
>> >>
>> >> 3. nginx settings
>> >> #------------ /opt/nginx/conf/sites-available/myapp
>> >> ----------------------------------------
>> >> fastcgi_cache_path /opt/nginx/cache/myapp
>> >> levels=1:2
>> keys_zone=myapp:50m;
>> >>
>> >> server {
>> >> listen 81;
>> >> server_name paperless;
>> >> access_log /opt/nginx/logs/myapp.access_log;
>> >> error_log /opt/nginx/logs/myapp.error_log warn;
>> >> root /var/www/myapp/myapp;
>> >> location / {
>> >> include fastcgi_params;
>> >> fastcgi_param SCRIPT_FILENAME "";
>> >> fastcgi_param PATH_INFO $path_url;
>> >> fastcgi_param PATH_INFO $fastcgi_script_name;
>> >> fastcgi_pass unix:/tmp/myapp-fcgi.sock;
>> >> fastcgi_cache myapp;
>> >> fastcgi_cache_key $server_addr$request_uri;
>> >> fastcgi_cache_valid any 1m;
>> >> fastcgi_hide_header Set-Cookie;
>> >> }
>> >>
>> >> location /static/ {
>> >> root /var/www/myapp/myapp;
>> >> }
>> >> }
>> >>
>> >> I'm not familliar with nginx settings, but I think it's because of
>> >> nginx setting problem results in the wrong url path.
>> >>
>> >> Anyone can help me on that?
>> >>
>> >> Thanks a lot.
>> >>
>> >> Lix Xu
>> >
>> >
>>
>
>
Oh, I have the same problem when deploy flask on nginx using fcgi. 2010/7/6 Lix Xu <xuzenglin@gmail.com> > Just got the solution use Nginx & fastcgi to serve Flask. > my application arch is: > /hello > /hello > __init__.py > /models -> database related > /static -> static content > /images > /css > /js > /templates -> html related > /views -> controller related > > the nginx conf like below, please replace hello to your application name, > and the point is the three lines: > fastcgi_split_path_info ^(/)(.*)$; > fastcgi_param PATH_INFO $fastcgi_path_info; > fastcgi_param SCRIPT_FILENAME $fastcgi_path_info; > > # nginx.conf > > ... > server { > listen 80; > server_name localhost; > access_log /opt/nginx/logs/hello.access_log; > error_log /opt/nginx/logs/hello.error_log warn; > location = /hello{ rewrite ^ /var/www/hello/hello/ last; } > location / { try_files $uri @hello; } > location @hello{ > include fastcgi_params; > fastcgi_split_path_info ^(/)(.*)$; > fastcgi_param PATH_INFO $fastcgi_path_info; > fastcgi_param SCRIPT_FILENAME $fastcgi_path_info; > fastcgi_pass unix:/tmp/hello.sock; > } > } > ... > > # /var/www/hello/hello-start.sh > #!/bin/sh > > chmod 755 /var/www/hello/hello.fcgi > spawn-fcgi -u 33 -g 33 -d /var/www/hello-f /var/www/hello/hello.fcgi -s > /tmp/hello.sock > > > #/var/www/hello/hello.fcgi > #!/usr/bin/env python > #-*- coding:utf-8 -*- > > from flup.server.fcgi import WSGIServer > from hello import app > > WSGIServer(app, bindAddress='/tmp/hello.sock').run() > > Hope this is useful to you. > > Regards. > > Lix Xu > > On Mon, May 31, 2010 at 11:45 AM, Lix Xu <xuzenglin@gmail.com> wrote: > >> Hi Silas, >> Thanks for suggestion. As I have no idea to serve WSGI under Nginx, I >> turned to Cherokee (which can works well for url_for). And I'm tring to find >> to deploy Flask using Nginx and FCGI & WSGI. >> Again, thanks a lot. >> >> Lix Xu >> >> >> On Sat, May 29, 2010 at 10:42 PM, Silas Baronda <silas.baronda@gmail.com>wrote: >> >>> Hi Lix, >>> >>> First I recommend moving to a different type of protocol other than >>> FCGI. In particular you should use WSGI. >>> >>> There are quite a few good WGSI server that you can proxy behind >>> nginx. Gunicorn and uwsgi >>> come to mind. >>> >>> On Sat, May 29, 2010 at 10:33 AM, Lix Xu <xuzenglin@gmail.com> wrote: >>> > Hi, >>> > It seems that no one can help on this. >>> > >>> > Lix Xu >>> > >>> > On Fri, May 28, 2010 at 9:12 AM, Lix Xu <xuzenglin@gmail.com> wrote: >>> >> >>> >> Hi, >>> >> I need help on nginx fcgi settings due to url_for always get wrong >>> url >>> >> path. >>> > >>> > PS: It works well under built-in server . >>> >> >>> >> 1. my app organization >>> >> myapp/ >>> >> myapp/ >>> >> __init__.py >>> >> models/ >>> >> templates/ >>> >> users/ >>> >> index.html >>> >> hello.html >>> >> say_hi.html >>> >> views/ >>> >> users.py >>> >> __init__.py >>> >> static/ >>> >> css/ >>> >> images/ >>> >> javascripts/ >>> >> 2. source code >>> >> # ---- myapp/__init__.py >>> >> ... >>> >> from myapp.views.users import users as users_view >>> >> app.register_module(users_view) >>> >> ... >>> >> >>> >> # ----- myapp/views/users.py >>> >> users = Module(__name__) >>> >> @users.route('/') >>> >> def index(): >>> >> # .... >>> >> >>> >> @users.route('/hello/') >>> >> def hello(): >>> >> # .... >>> >> >>> >> @users.route('/hello/<name>/') >>> >> def say_hi(name): >>> >> # ..... >>> >> >>> >> # --- myapp/templates/users/index.html >>> >> url_for('index') ---> / >>> >> url_for('hello') ---> /hello >>> >> url_for('.static', filename='css/main.css') --> >>> >> /static/css/main.css >>> >> >>> >> # --- myapp/templates/users/hello.html >>> >> url_for('index') ---> /hello >>> >> url_for('hello') ---> /hello/hello (tried >>> url_for('.users.index'), >>> >> same result) >>> >> url_for('say_hi', name = name) --> /hello/say_hi/<name> >>> >> url_for('.static', filename='css/main.css') --> >>> >> /hello/static/css/main.css >>> >> >>> >> 3. nginx settings >>> >> #------------ /opt/nginx/conf/sites-available/myapp >>> >> ---------------------------------------- >>> >> fastcgi_cache_path /opt/nginx/cache/myapp >>> >> levels=1:2 >>> keys_zone=myapp:50m; >>> >> >>> >> server { >>> >> listen 81; >>> >> server_name paperless; >>> >> access_log /opt/nginx/logs/myapp.access_log; >>> >> error_log /opt/nginx/logs/myapp.error_log warn; >>> >> root /var/www/myapp/myapp; >>> >> location / { >>> >> include fastcgi_params; >>> >> fastcgi_param SCRIPT_FILENAME ""; >>> >> fastcgi_param PATH_INFO $path_url; >>> >> fastcgi_param PATH_INFO $fastcgi_script_name; >>> >> fastcgi_pass unix:/tmp/myapp-fcgi.sock; >>> >> fastcgi_cache myapp; >>> >> fastcgi_cache_key $server_addr$request_uri; >>> >> fastcgi_cache_valid any 1m; >>> >> fastcgi_hide_header Set-Cookie; >>> >> } >>> >> >>> >> location /static/ { >>> >> root /var/www/myapp/myapp; >>> >> } >>> >> } >>> >> >>> >> I'm not familliar with nginx settings, but I think it's because of >>> >> nginx setting problem results in the wrong url path. >>> >> >>> >> Anyone can help me on that? >>> >> >>> >> Thanks a lot. >>> >> >>> >> Lix Xu >>> > >>> > >>> >> >> >