You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx无法拦截.php文件访问的问题求助

Nginx无法拦截.php文件访问的问题求助

各位大佬好,我最近碰到个Nginx的棘手问题,想请大家帮忙排查下!

我本来想让Nginx直接拦截所有.php文件的访问,按照自己的理解写了这段配置,执行sudo systemctl restart nginx重启服务后,结果完全没达到预期——访问.php文件时,Nginx并没有返回403禁止访问,反而把请求转发到后端服务器了,我还在服务器的事件查看器里看到了相关错误。

我写的拦截配置是这样的:

location ~ \.php$ {
        return 403;
}

对应的Nginx访问日志是:

x.x.x.x - - [19/Jul/2024:08:09:17 -0300] "GET /test-user.php HTTP/1.1" 500 1405 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"

UPDATE - 补充完整配置片段

我把配置里的http块部分内容贴出来,方便大家排查:

http {
        keepalive_timeout               60s;
        proxy_read_timeout              60s;
        proxy_send_timeout              60s;
        proxy_connect_timeout   5s;

        client_max_body_size    2M;

        sendfile                        on;
        tcp_nopush                      on;
        types_hash_max_size             2048;
        server_tokens                   off;

        server_names_hash_bucket_size   64;

        # 后续还有其他配置内容...
}

备注:内容来源于stack exchange,提问作者rd1218

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 09:59:31