【问题标题】:NGINX 404 when requesting non root location请求非根位置时的 NGINX 404
【发布时间】:2020-06-01 14:59:28
【问题描述】:

我有这个基本的 conf 文件:

server {

    root /usr/share/nginx/html/default/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass my-php:9000; 
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;

        fastcgi_param SERVER_PORT $server_port;
    }

    location ~ \.php$ {
      return 404;
    }

    location ~ /\.ht {
         deny  all;
    }
}

位于 Nginx docker 容器中:/etc/nginx/conf.d/default.conf

如果我请求网站,它会很好用

127.0.0.1:8080

但如果我想从浏览器访问文件,则会出现 404 失败: 127.0.0.1:8080/check.php

即使文件存在于公共目录中,如果我将其重命名为 index.php,它会通过请求 127.0.0.1:8080 加载而不会出现任何问题,但是需要在 nginx conf 中更改什么,因此 check.php 文件将由 @ 可用987654325@?

问题2: 我可以按预期请求127.0.0.1:8080 200 OK 和 BODY 但我得到:

http://127.0.0.1:8080/index.php 404 Not Found
http://127.0.0.1:8080/check.php 404 Not Found

如果我关闭这个块:

location / {
    try_files $uri /index.php$is_args$args;
}

我得到 access.log "GET / HTTP/1.1" 403 和请求-响应

http://127.0.0.1:8080/ 403 Forbidden

但这种匹配似乎有效:

location ~ ^/index\.php(/|$) { ... }

对于 php,因为如果我让 fastcgi_pass 无效

fastcgi_pass wrong-containername:9000;

我得到错误而不是 200 OK。

1.如何为 php-fpm 位置制作一个可以使用的模式

http(s)://127.0.0.1:8080/
http(s)://127.0.0.1:8080/index.php
http(s)://127.0.0.1:8080/check.php

2.And 附加处理所有其他使用 php-fpm 到目录的请求,其中可能是 index.php、check.php 或whatever.php

http(s)://127.0.0.1:8080/*

【问题讨论】:

    标签: php nginx server http-status-code-404 config


    【解决方案1】:

    您的配置只允许执行index.php。这是一个安全的配置,所以我建议将其保留在适当的位置并进行调整以列出任何其他脚本,例如改变:

    location ~ ^/index\.php(/|$) {
    

    location ~ ^/(index|check)\.php(/|$) {
    

    将只允许执行/index.php/check.php

    【讨论】:

    • 我厌倦了你的回答,但它没有用,这可能是因为我得到:http://127.0.0.1:8080/index.php 404 Not Found for index.php 所以可能还有另一个与check.php 无关的问题请参阅我的问题的编辑。
    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2017-04-27
    • 2012-09-24
    • 2021-02-13
    • 1970-01-01
    相关资源
    最近更新 更多