设为首页 收藏本站
查看: 408|回复: 0

[经验分享] [转]Understanding the Nginx Configuration Inheritance Model

[复制链接]

尚未签到

发表于 2016-12-25 10:21:44 | 显示全部楼层 |阅读模式
Understanding the Nginx Configuration Inheritance Model





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


  • Nginx Configuration Primer
  • Implementing Full-Page caching with Nginx and PHP
  • How to Solve “No input file specified” with PHP and Nginx
  • fastcgi_params Versus fastcgi.conf – Nginx Config History
  • Optimized File Uploading With PHP & Nginx
  原文网址:http://blog.martinfjordvald.com/2012/08/understanding-the-nginx-configuration-inheritance-model/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319092-1-1.html 上篇帖子: Ubuntu10下Nginx-0.8.54安装 下篇帖子: Keepalived+Nginx 实现双机热备
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表