2009-09-03 60 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; 

然後只包括位置{}塊中的根在那裏你提供靜態資產。