要了解nginx的繼承模型,首先需要知道nginx使用多個(gè)配置塊進(jìn)行操作。 在nginx中,這樣的塊被稱為上下文,例如,放置在服務(wù)器上下文中的配置指令駐留在server { }塊中,就像放置在http上下文中的指令駐留在http { } 塊中一樣。
nginx中有6種可能的上下文,這里是從上到下的順序:
Global. Http. Server. If. Location. Nested Location. If in location. limit_except.默認(rèn)繼承模型是指令僅向下繼承。 從來沒有側(cè)身,絕對(duì)永遠(yuǎn)不會(huì)。 這包括您在內(nèi)部從一個(gè)位置重寫請(qǐng)求到另一個(gè)位置的情況 - 第一個(gè)位置中的每個(gè)指令都被遺忘,只有第二個(gè)位置指令適用于位置上下文。 在繼承行為方面,nginx中有四種類型的配置指令:
Normal指令 - 每個(gè)上下文一個(gè)值,例如:“root”或“index”。 Array指令 - 每個(gè)上下文有多個(gè)值,例如:“access_log”或“fastcgi_param” Action指令 - 不只是配置的東西,例如:“rewrite”或“fastcgi_pass” try_files指令。Normal指令是迄今為止最常見的指令,它遵循默認(rèn)的繼承模型而沒有任何意外。 讓我們看一個(gè)示例配置,顯示行為的情況。
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指令很像普通指令,因?yàn)樗鼈冏裱瓨?biāo)準(zhǔn)繼承模型,它始終向下繼承并替換在更高上下文中指定的任何指令。 可能令人困惑的是假設(shè)你添加到數(shù)組。Array 指令的行為是,如果在同一上下文中定義多個(gè)指令,則將添加到值,但如果在不同的上下文中定義多個(gè)指令,則較低的上下文將替換較高的上下文。 這意味著如果您希望它在多個(gè)上下文中存在,您有時(shí)需要雙重定義一個(gè)值。 這種情況的一個(gè)例子。
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指令是它開始變得有趣的地方。 它們被限制在一個(gè)上下文中并且永遠(yuǎn)不會(huì)向下繼承,但是它們可以在多個(gè)上下文中指定,并且在某些情況下將針對(duì)每個(gè)上下文執(zhí)行。 rewrite指令是一個(gè)action指令,允許在服務(wù)器和位置上下文中執(zhí)行兩個(gè)上下文。
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. }}
新聞熱點(diǎn)
疑難解答
圖片精選