Django and Nginx try_files 403 for site root page

筅森魡賤 提交于 2019-12-21 22:10:19

问题


I use such Nginx configuration for the domain:

server_name_in_redirect off; 
listen 80;
server_name  ~^(www\.)?(.+)$;
root /var/www/$2/htdocs;

location / {
    try_files  $uri  $uri/ $uri/index.htm  @django;
    index index.html index.htm;
}

location @django {
    fastcgi_pass 127.0.0.1:8801;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_pass_header Authorization;
    fastcgi_intercept_errors off;
    fastcgi_param REMOTE_ADDR $remote_addr;
}

Django URL config:

urlpatterns = patterns('',   
    url(r'^$', home, name='home'),
    url(r'index.htm', home, name='home'),    
    url(r'^(?P<name>.*).htm$', plain_page, name="plain_page"),
}

all urls like http://domain.com/somepage.htm works good, except http://domain.com/ it always shows 403 by Nginx.

if you add static index.htm file to the site root - it's opened because of try_files directive

if you have no static index.htm, but call http://domain.com/index.htm page is opened by django

buf it you have no static index.htm and open http://domain.com/ you get no page, but by idea index.htm should be looked and passed to the django as the last in the try_files chain.

how to make http://domain.com/ work (should call django's index.htm) in this case?


回答1:


Add this

location = / { rewrite ^(.*)$ /index.htm last; }

underneath the root line to do a rewrite of the URI before further processing.

PS. You have probably sorted this out during the year since your asked, but here it is for other to see.




回答2:


A better solution is you provide a / url in your urls.py is remove the

 root /var/www/$2/htdocs;

Then only include the root in location {} blocks where you serve up static assets.



来源:https://stackoverflow.com/questions/1376096/django-and-nginx-try-files-403-for-site-root-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!