大网数据广告活动
服务器运维问答

Why does child location disable parent location in Nginx?

· 52CE 我爱测

I want to turn access_log off for one php file. location ~ \.php$ { location ~ ^/dontlog\.php$ { access_log off; } # fastcgi stuff ... } This makes dontlog.php to not go though fastcgi, it just becomes text file, not php script. …

问题原文

原帖链接 · Qiao · CC BY-SA 4.0

I want to turn access_log off for one php file.

location ~ \.php$ {
    location ~ ^/dontlog\.php$ {
        access_log off;
    }

    # fastcgi stuff
    ...
}

This makes dontlog.php to not go though fastcgi, it just becomes text file, not php script. Other php files go through it. access_log off works.

I don't understand why this happens. Is it because if new location is found, parent locations ignored? I tired to put it after fastcgi code, same result.

已采纳解答

原帖链接 · USD Matt · CC BY-SA 4.0

See the blog post linked below for a good explanation of nginx inheritance. Unfortunately it's complicated by the fact that different types of configuration option are inherited in different ways. In your example, I suspect you are using fastcgi_pass , which does not inherit.

Action directives are where it starts to get interesting. They are confined to one context and will never inherit downwards, they can however be specified in multiple contexts and in some cases will be executed for each context

https://blog.martinfjordvald.com/2012/08/understanding-the-nginx-configuration-inheritance-model/

Also note the following example (from the same blog), which shows that setting a single fastcgi_param value in a nested block will override all values from a parent.

server {
    access_log /var/log/nginx/access.log;
    include fastcgi.conf;

    location ~ ^/calendar/.+\.php$ {
        access_log /var/log/nginx/php-requests.log; # If this executes then server context one never does.

        fastcgi_param ENV debug; # This *overwrites* the higher context array.
        include fastcgi.conf     # Therefore we include it in *this* context again.
    }
}

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

资料来源与适用范围

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

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

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