configuration - 无法在 nginx 中找出位置 block

标签 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;
    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 Not Found

nginx/0.7.67



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

我在位置块上做错了什么?

我以前从未配置过 nginx,无论如何我都是一个完整的网络新手。

我也希望能够为其他应用程序提供更多位置块。这实际上只是在这里发布的基本示例。

将 nginx 更新到 Debian Squeeze 向后移植的版本。没提升:

404 Not Found

nginx/1.1.19

最佳答案

它不工作的原因是......

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

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

现在,当您向物理上位于“/var/www/wordpress/folder_a/file_a.php”的“/wp/folder_a/file_a.php”提出请求时,该请求会命中 php 位置块并给出根文件夹该块的 active 将在“/var/www/root/folder_a/file_a.php”中查找文件。结果,您会收到“404 not found”。

您可以将服务器级别的根指令更改为“/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 的根指令。

为了帮助保持简洁并简化编辑等,您可以将 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 参数,您只需在一个地方编辑它。

附注。更新您的 nginx 不会解决这个问题,因为它不是版本问题……但无论如何都要更新!

关于configuration - 无法在 nginx 中找出位置 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10393740/

相关文章:

WCF 错误 - 找不到与方案匹配的基地址

Nginx子域配置

nginx - Nginx.conf可以访问环境变量吗?

java - Spring Security Java 配置问题

javascript - 是否可以自定义 Chrome 或其他浏览器以在所有打开的选项卡上运行本地 JavaScript 文件?

PHP 文件由浏览器下载,而不是由本地开发服务器 (MAMP) 处理

ruby-on-rails - 带有 RVM、Phusion Passenger、Apache 和 Nginx 的 Ubuntu 服务器

nginx - 如果删除了日志文件,则uWSGI日志记录将无法正常工作

Nginx - 如何使用不同的子域名运行多个 Odoo 实例

java - 更改 Android 中应用程序的 DisplayMetrics(API 级别低于 17)