2012-05-01 28 views
6

在nginx配置文件中試圖找出位置塊很困難。這是我有:在nginx中找不到位置塊

server { 
    listen   80; 
    server_name  _; 

    access_log  /var/log/nginx/example.com.access_log; 
    error_log  /var/log/nginx/example.com.error_log warn; 

    root    /var/www/root; 
    index    index.php index.htm index.html; 
    fastcgi_index  index.php; 

    location /wp/ { 
     root    /var/www/wordpress; 
     index    index.php index.htm index.html; 
     fastcgi_index  index.php; 
    } 

    location ~* \.php$ { 
     try_files   $uri =404; 
     keepalive_timeout 0; 
     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_pass   127.0.0.1:9000; 
    } 
} 

瀏覽到/運行正常,並顯示在/ var/WWW /根的網站,但如果地方工作,我認爲他們應該在瀏覽/ WP應該帶我去的WordPress安裝在/ var/www/wordpress中。所有我得到的是:

404未找到

的nginx/0.7.67

如果我重新定位在/ var/WWW/WordPress的目錄到/ var/WWW /根/ WordPress的並瀏覽到/ wordpress都是完美的。

我在做錯位置塊?

我從來沒有配置過nginx,而且完全是一個完整的web newb。

我希望能夠爲其他應用程序提供更多位置塊。這真的只是這裏張貼的一個基本範例。

已將nginx更新到Debian Squeeze backports中的版本。無改善:

404未找到

的nginx/1.1.19

+0

你試過去/ wp /而不是? –

+0

你不想去想如何過時nginx/0.7.67是。 – Dayo

+0

我試過/ wp /以及。這是來自Debian 6存儲庫的nginx。我猜Debian確實傾向於稍微落後一點。儘管我傾向於堅持回購協議中的內容。 – goji

回答

7

爲什麼它不工作的原因是...

在服務器級別,你有「root/var/www/root」。所以基本上,每個位置塊將使用這個,除非被特別覆蓋。這是很好的做法。

然後,您可以在「wp」位置塊中將其覆蓋爲「/ var/www/wordpress」。但是,php位置塊仍然使用默認值。

現在,當您向「/wp/folder_a/file_a.php」請求物理位於「/var/www/wordpress/folder_a/file_a.php」時,請求會觸及php位置塊,並且因爲該塊的活動根文件夾會在「/var/www/root/folder_a/file_a.php」中查找該文件。結果,你得到了「404找不到」。

您可以將服務器級別的根指令更改爲「/ var/www/wordpress」並刪除wp位置中的覆蓋。這將解決這個問題,但「/ var/www/root」下的php腳本將不再起作用。不知道你有沒有。

如果您需要在兩個 「/無功/網絡/ root」 和 「/無功/網絡/ WordPress的」 下運行PHP,你需要做的是:

server { 
    ... 
    root    /var/www/root; 
    index    index.php index.htm index.html; 
    # Keep fastcgi directives together under location 
    # so removed fastcgi_index 

    # Put keepalive_timeout under 'http' section if possible 

    location /wp/ { 
     root    /var/www/wordpress; 
     # One appearance of 'index' under server block is sufficient 
     location ~* \.php$ { 
      try_files   $uri =404; 
      fastcgi_index  index.php; 
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_pass   127.0.0.1:9000; 
     } 
    } 

    location ~* \.php$ { 
     try_files   $uri =404; 
     fastcgi_index  index.php; 
     fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     fastcgi_pass   127.0.0.1:9000; 
    } 
} 

也就是說,巢重複PHP位於wp位置塊下的位置塊。它將繼承wp的根指令。

爲了幫助保持事物的緩和和簡化編輯等,您可以將fastcgi指令放入單獨的文件中並根據需要包含它。

所以在/ path/fastcgi。PARAMS,您有:然後

fastcgi_index  index.php; 
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_pass   127.0.0.1:9000; 

你的conf可以是:

server { 
    ... 
    root    /var/www/root; 
    ... 
    location /wp/ { 
     root    /var/www/wordpress; 
     location ~* \.php$ { 
      try_files   $uri =404; 
      include /path/fastcgi.params; 
     } 
    } 

    location ~* \.php$ { 
     try_files   $uri =404; 
     include /path/fastcgi.params; 
    } 
} 

這樣,如果你需要編輯任何FastCGI的PARAM,你只是在一個地方進行編輯。

PS。更新你的nginx不會解決這個問題,因爲它不是一個版本問題..但無論如何都要更新!

+0

謝謝。我曾懷疑它使用了錯誤的document_root,但不知道你可以堆疊這樣的位置塊!當我取得成功時,我會回覆。 – goji

+0

有什麼辦法可以準確地查看腳本執行時腳本的SCRIPT_FILENAME值是什麼? – goji

+0

'location/wp /'需要'location ^〜/ wp /'來防止外部php位置覆蓋它。看到[文檔](http://wiki.nginx.org/HttpCoreModule#location) – kolbyjack