大网数据广告活动
网站运营与故障问答

Nginx deny-all fails to execute PHP code in 403.html

· 52CE 我爱测

I want to deny access to some directories and files, so I use the deny_all option to return a 403.html page. However, it does not allow me to execute any PHP code in that page. Something to do with "internal"? Should I use "retur…

问题原文

原帖链接 · Alph · CC BY-SA 3.0

I want to deny access to some directories and files, so I use the deny_all option to return a 403.html page. However, it does not allow me to execute any PHP code in that page.

Something to do with "internal"? Should I use "return" or "error_page" instead?

Here's my config, any other suggestions appreciated...

#
#  Redirect all to www
#
server 
{
    listen 80;

    server_name site.com;
    return 301 $scheme://www.site.com$request_uri;
}

#
#  Validate and process requests
#
server 
{
    listen 80;

    server_name www.site.com *.site.com;

    root /var/www/site.com;
    index index.html;
#
#  Error and Access logs
#
    error_log /var/log/nginx/error.site.log notice;
    access_log /var/log/nginx/access.site.log;

    error_page 403 /Src/403.html;
    location = /Src/403.html 
    {
      internal;
    }

    error_page 404 /Src/404.html;
    location = /Src/404.html 
    {
      internal;
    }

    location / 
    {
      try_files $uri $uri/ =404;
    }

    location ~ \.(py|sh|tgz|xml)$ 
    {
      deny all;
    }

    location ~ \.(html|php)$ 
    {
      include fastcgi.conf;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.html;
      try_files $uri.html $uri.php $uri/ =404;
    }

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The problem is the location itself. nginx chooses one location process a request. In the case of /Src/403.html, the location = /Src/404.html block takes precedence over the location ~ \.(html|php)$ block, which means that none of the PHP directives are included. See this document for details.

If you care that the /Src/403.html URI remains internal, include the necessary directives into that location block. For example:

error_page 403 /Src/403.html;
location = /Src/403.html 
{
    internal;
    include fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
} 

Otherwise, you can just delete the entire location block, in which case, the /Src/403.html URI will be processed by the location ~ \.(html|php)$ block.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。