2009-09-03 61 views
2

try_files 403我用这样的Nginx配置为域:Django和Nginx的站点根页面

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配置:

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

http://domain.com/somepage.htm所有URL的作品好,除了http://domain.com/它总是由Nginx显示403。

如果添加静态index.htm文件到网站根 - 它打开,因为try_files的指令

如果你没有静态的index.htm,但拨打http://domain.com/index.htm页由Django的

打开它BUF你没有静态index.htm,并打开http://domain.com/你没有页面,但想法 index.htm应该被查看并传递给django作为try_files链中的最后一个。

如何在这种情况下使http://domain.com/工作(应该调用django的index.htm)?

回答

4

添加此

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

root线下面进一步的处理之前执行的URI的重写

PS。自从你问了以后,你可能在一年中对此进行了整理,但这里是为了让其他人看到。

+0

谢谢。据我所知,我以某种方式解决了这个问题,并采用了不同的方法。 – Zelid 2010-09-15 10:25:24

0

更好的解决办法是你提供你的urls.py A/URL是删除

root /var/www/$2/htdocs; 

然后只包括位置{}块中的根在那里你提供静态资产。