加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

配置 – 无法在nginx中找到位置块

发布时间:2020-05-22 18:13:47 所属栏目:Nginx 来源:互联网
导读:很难在nginx配置中找出位置块.这就是我所拥有的:server { listen 80; server_name _; access_log /var/log/nginx/example.com.access_log; error_log /var/log/nginx/exa

很难在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,而且还是一个完整的web newb.

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

在Debian Squeeze backports中将nginx更新为版本.没有得到改善:

404 Not Found

nginx/1.1.19

最佳答案 它不起作用的原因是……

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

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

现在当你向“/ww/folder_a/file_a.php”发出一个请求时,请求命中了“/ var / www / word文件”,该命令位于“/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脚本将不再起作用.不确定你有没有.

如果你需要在“/ 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;

你的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参数,你只需在一个地方编辑它.

PS.更新你的nginx不会解决这个问题,因为它不是版本问题..但无论如何都要更新!

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读