2012-05-01 27 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