Configuration Can';无法在nginx中找到位置块

Configuration Can';无法在nginx中找到位置块,configuration,nginx,Configuration,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;

很难在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/root中显示网站,但如果位置按我认为的那样工作,浏览到/wp应将我带到/var/www/wordpress中的wordpress安装。我得到的只是:

404找不到

nginx/0.7.67

如果我将/var/www/wordpress目录重新定位到/var/www/root/wordpress并浏览到/wordpress,那么一切都是完美的

位置块有什么问题

我以前从未配置过nginx,而且我还是一个完全的网络新手

我希望能够有更多的其他应用程序以及位置块。这实际上只是一个在这里发布的基本示例

将nginx更新为Debian Squeeze Backport中的版本。无改善:

404找不到

nginx/1.1.19


它不起作用的原因是

在服务器级别,您有“root/var/www/root”。所以基本上,每个位置块都会使用它,除非被特别重写。这是很好的做法

然后,您在“wp”位置块中将其覆盖为“/var/www/wordpress”。但是,php位置块仍然使用默认值

现在,当您向位于/var/www/wordpress/folder\u a/file\u a.php的“/wp/folder\u a/file\u a.php”发出请求时,该请求将命中php位置块,并且给定该块的根文件夹处于活动状态,将在“/var/www/root/folder\u a/file\u a.php”中查找该文件。因此,您会得到一个“404未找到”

您可以将服务器级root指令更改为“/var/www/wordpress”,并删除wp位置中的覆盖。这将解决这个问题,但“/var/www/root”下的php脚本将不再工作。不知道你有没有

如果需要在“/var/www/root”和“/var/www/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;
    }
}
也就是说,在wp位置块下嵌套一个重复的php位置块。它将继承wp的root指令

为了帮助保持简洁和易于编辑等,您可以将fastcgi指令放入单独的文件中,并根据需要将其包含在其中

因此,在/path/fastcgi.params中,您有:

    fastcgi_index      index.php;
    fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass         127.0.0.1:9000;
然后,您的配置可以是:

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参数,只需在一个位置编辑它


另外,更新nginx无法解决此问题,因为这不是版本问题。。但无论如何都要更新

你试过改为/wp/吗?你不想想象nginx/0.7.67有多过时。我也试过/wp/了。这是Debian 6存储库中的nginx。我想Debian确实有点落后。不过,我倾向于坚持回购协议中的内容。你可以从中获取最新版本的挤压。我会尝试更新并发回。谢谢。我怀疑它使用了错误的文档\u root,但不知道您可以像那样堆叠位置块!成功后我会发回。有没有什么方法可以查看php脚本执行时脚本文件名的确切值?
location/wp/
必须是
location^~/wp/
,以防止外部php位置覆盖它。看看@kolbyjack和Dayo如果你能弄明白这一点,你就是我的英雄:谢谢!