NGINX 404请求非根目录位置时

时间:2020-02-17 16:19:56

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

我有这个基本的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 conteainer中:/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文件127.0.0.1:8080/check.php可用吗?

问题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。另外,它还可以处理其他所有使用php-fpm发送给dirs的请求,这些请求可能是index.php或check.php或what.php

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

1 个答案:

答案 0 :(得分:0)

您的配置仅允许执行index.php。这是一个安全的配置,因此我建议对其进行适当调整,以列出所有其他脚本,例如更改:

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

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

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

相关问题