`

[转]Understanding the Nginx Configuration Inheritance Model

阅读更多

To understand the inheritance model of nginx you first need to know that nginx operates with multiple blocks of configuration. In nginx such a block is referred to as a context, for instance, a configuration directive placed in server context resides within a server { } block just like a directive placed in http context resides in the http { } block.

There are 6 possible contexts in nginx, here in top to bottom order:

  • Global.
  • Http.
  • Server.
  • If.
  • Location.
    • Nested Location.
    • If in location.
    • limit_except.

The default inheritance model is that directives inherit downwards only. Never sideways and definitely never up. This includes scenarios where you rewrite a request internally from one location to another – every directive in the first location is forgotten and only the second location directives apply for the location context.When it comes to inheritance behaviour there are four types of configuration directives in nginx:

  • Normal directive – One value per context, for example: “root” or “index”.
  • Array directive – Multiple values per context, for example: “access_log” or “fastcgi_param”
  • Action directive – Something which does not just configure, for example: “rewrite” or “fastcgi_pass”
  • try_files directive.

Normal directives are by far the most common one and follows the default inheritance model without any surprises. Lets have a look at an example configuration that show cases the behaviour.

    server {
    root /home/user/public_html;
     
    location /app {
    root /usr/share;# This results in /usr/share/app
    # Full URI is ALWAYS appended.
    }
     
    location /app2 {
    // Server context root applies here.
    }
    }

 

Array directives are a lot like normal directives in the sense that they follow the standard inheritance model of always inheriting downwards and replacing any directives specified in a higher context. What might be confusing about these is to assume that you add to the array. The behaviour of an array directive is that if you define multiple directives in the same context you will add to the values, but if you define multiple directives in different contexts then the lower context will replace the higher context ones. This means that you need to sometimes double define a value if you want it present in multiple context. An example of such a scenario.

    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.
    }
    }

 

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. The rewrite directive is an action directive that is allowed in server and location context where both contexts might be executed.

    server {
    rewrite ^/booking(.*) /calendar$1 permanent;# Always executes.
     
    location /calendar {
    rewrite ^/index.php;# Can execute in addition to and does not replace server context rewrites.
    }
    }

 

Naturally, it’s not quite that simple. Within locations there are three possible contexts, a nested location, an if and limit_except. The behaviour of a directive is actually entirely up to the module that defines it. All the normal and array directives will inherit properly if they are allowed in that context. For action directives the story is a bit different. Generally they will not inherit into a nested location but it ultimately depends on how the module wants it to be and it can differ on a directive by directive basis. The nginx documentation is not of use here either so you’ll have to just try it and see if nginx complains. For good measure, lets have an example of the most common behaviour and how it affects rewrite :

    server {
    location /calendar {
    rewrite ^/static.php;# Executes unless inner location matches.
     
    location ~ \.php$ {
    fastcgi_pass backend;# Outer location context rewrite is not executed.
    }
    }
    }

 

 

The try_files directive is mostly like every other action directive mentioned above, the difference is that if placed in server context nginx actually creates a pseudo-location that is the least specific location possible. That means if a request matches a defined location the try_files directive will not be executed. This means that if you have location / defined then you have a location that matches every possible request and as such try_files will never actually execution. Therefore always place try_files in location context instead of server context if at all possible.

    server {
    try_files $uri /index.php;# This never executes.
     
    location /{
    # Whatever here, or empty.
    }
     
    location ~ \.php$ {
    # If this location executes then try_files still does not execute.
    # Even if location / did not exist.
    }
    }

 

  1. Nginx Configuration Primer
  2. Implementing Full-Page caching with Nginx and PHP
  3. How to Solve “No input file specified” with PHP and Nginx
  4. fastcgi_params Versus fastcgi.conf – Nginx Config History
  5. Optimized File Uploading With PHP & Nginx

原文网址:http://blog.martinfjordvald.com/2012/08/understanding-the-nginx-configuration-inheritance-model/

分享到:
评论

相关推荐

    nginx-configuration, 创建和管理 Nginx 配置的实用程序和模板.zip

    nginx-configuration, 创建和管理 Nginx 配置的实用程序和模板 Nginx 配置针对各种用例的Nginx 配置模板集合,主要用于运行 PHP web应用程序。安装sudo git clone ...

    The Complete NGINX Cookbook

    The Complete NGINX Cookbook The Complete NGINX Cookbook The Complete NGINX Cookbook The Complete NGINX Cookbook The Complete NGINX Cookbook

    Mastering-Nginx.pdf

    This guide will serve to clarify the murky waters of NGINX configuration. In doing so you will learn how to tune NGINX for various situations, what some of the more obscure configuration options do, ...

    Nginx HTTP Server, 3nd Edition

    Get to know the basics of the Nginx configuration: syntax, structure, and semantics Understand the advanced load balancing functionality of Nginx and the newest innovative IO mechanisms Create virtual...

    nginx configuration配置

    nginx配置

    nginx 官方cookbook

    The following chapters have been written for an audience that has some understanding of NGINX, modern web architectures such as n-tier or microservice designs, and common web protocols such as TCP, ...

    NGINX下RTSP转RTMP

    Windows 下使用NGINX,搭建RTMP流媒体服务器,包含使用说明,下载就可使用。

    keystoke证书转换nginx证书工具

    keystoke证书转换nginx证书工具,jdk keytool.exe生成的.keystore格式证书通过他可以转换为.key、.pfx、.crt证书,用于配置https证书

    nginx rtsp转rtmp推流到http播放的配置文件

    配套博文见《ffmpeg 将rtsp转成rtmp用网页播放安装及使用教程》 https://blog.csdn.net/sac761/article/details/108731650

    nginx-configuration-examples:很酷的 Nginx 配置示例

    Nginx 配置示例 特征 维护模式 当文件/usr/share/nginx/html/maintenance-mode存在时,Nginx 会返回/usr/share/nginx/html/maintenance.html的内容以及状态码503 (服务暂时不可用)。 可以通过以下 HTTP 请求启用...

    jks证书格式转换nginx使用格式

    由于生成的证书是jks格式,nginx不能直接用,需要要转成PEM格式,这要用到jks2pfx工具进行转换。 jks2pfx的命令格式:JKS2PFX.bat keystore password alias exportname keystore:KeyStore文件绝对路径 password:...

    海康rtsp拉流,rtmp推流,nginx部署转flv集成

    海康rtsp拉流,rtmp推流,nginx部署转flv集成

    apache到nginx配置文件转换工具

    一个开源的Linux下运行的命令行工具,能将Apache的配置文件转换成相应的Nginx的配置文件。本工具能自动将Apache Web服务器的配置文件转化成Nginx的配置文件,减少WEB服务器迁移的工作量。 该工具从Apache Web服务器...

    nginx、FFmpeg转流文件Java源码

    nginx、ffmpeg转流文件源码。 多个同时转流。 nginx、ffmpeg转流文件源码 代码

    [官方]Complete NGINX Cookbook

    The Complete NGINX Cookbook, Official Nginx book for beginner and professionals.

    apache配置文件转换到nginx配置文件的工具

    一个开源的Linux下运行的命令行工具,能将Apache的配置文件转换成相应的Nginx的配置文件。本工具能自动将Apache Web服务器的配置文件转化成Nginx的配置文件,减少WEB服务器迁移的工作量。 该工具从Apache Web服务器...

    The Complete NGINX Cookbook 2019 Update

    Learn how to configure caching, load balancing, security, cloud deployments, gRPC, HTTP/2 server push, and other critical NGINX features in this free O’Reilly ebook

    Complete-NGINX-Cookbook-2019.pdf

    NGINX is one of the most widely used web servers available today, in part because of its capabilities as a load balancer and reverse proxy server for HTTP and other network protocols. The 2019 edition...

    nginx离线安装附安装包附安装日志

    1、安装gcc-c++、gcc  对于gcc、gcc-c++,... nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful OK,测试通过,启动nginx

    nginx证书转换jks

    nginx证书转换jks,将jks文件导出为Nginx所需要的文件.crt和.key

Global site tag (gtag.js) - Google Analytics