安全服務(wù)器是只允許所需數(shù)量的服務(wù)器。理想情況下,我們將通過單獨(dú)啟用其他功能來基于最小系統(tǒng)構(gòu)建服務(wù)器。進(jìn)行最少的配置也有助于調(diào)試。如果該錯(cuò)誤在最小系統(tǒng)中不可用,則分別添加功能,然后繼續(xù)搜索錯(cuò)誤。
這是運(yùn)行nginx所需的最低配置:
# /etc/nginx/nginx.confevents {} # event context have to be defined to consider config validhttp { server { listen 80; server_name javatpoint.co www.javatpoint.co *.javatpoint.co; return 200 "Hello"; }
登錄后復(fù)制
Root,Location和try_files指令
Root 指令
root指令用于設(shè)置請(qǐng)求的根目錄,從而允許nginx將傳入的請(qǐng)求映射到文件系統(tǒng)上。
server { listen 80; server_name javatpoint.co; root /var/www/javatpoint.co;}
登錄后復(fù)制
它允許nginx根據(jù)請(qǐng)求返回服務(wù)器內(nèi)容:
javatpoint.co:80/index.html # returns /var/www/learnfk.com/index.htmljavatpoint.co:80/foo/index.html # returns /var/www/learnfk.com/foo/index.html
登錄后復(fù)制
Location指令
location指令用于根據(jù)請(qǐng)求的URI(統(tǒng)一資源標(biāo)識(shí)符)來設(shè)置配置。
語(yǔ)法為:
location [modifier] path
登錄后復(fù)制
示例:
location /foo { # ...}
登錄后復(fù)制
如果未指定修飾符,則將路徑視為前綴,之后可以跟隨任何內(nèi)容。上面的示例將匹配:
/foo/fooo/foo123/foo/bar/index.html...
登錄后復(fù)制
我們還可以在給定的上下文中使用多個(gè)location指令:
server { listen 80; server_name javatpoint.co; root /var/www/javatpoint.co; location/{ return 200 "root"; } location /foo { return 200 "foo"; }}javatpoint.co:80 / # => "root"javatpoint.co:80 /foo # => "foo"javatpoint.co:80 /foo123 # => "foo"javatpoint.co:80 /bar # => "root"
登錄后復(fù)制
Nginx還提供了一些可以與 location 指令結(jié)合使用的修飾符。
搜索公眾號(hào)Linux中文社區(qū)后臺(tái)回復(fù)“私房菜”,獲取一份驚喜禮包。
修飾符已分配優(yōu)先級(jí):
= - Exact match^~ - Preferential match~ && ~* - Regex matchno modifier - Prefix match
登錄后復(fù)制
首先,nginx將檢查所有精確匹配項(xiàng)。如果不存在,它將尋找優(yōu)先選項(xiàng)。如果此匹配也失敗,則將按其出現(xiàn)順序測(cè)試正則表達(dá)式匹配。如果其他所有操作均失敗,則將使用最后一個(gè)前綴匹配。
location /match { return 200 'Prefix match: will match everything that starting with /match';}location ~* /match[0-9] { return 200 'Case insensitive regex match';}location ~ /MATCH[0-9] { return 200 'Case sensitive regex match';}location ^~ /match0 { return 200 'Preferential match';}location = /match { return 200 'Exact match';}/match # => 'Exact match'/match0 # => 'Preferential match'/match1 # => 'Case insensitive regex match'/MATCH1 # => 'Case sensitive regex match'/match-abc # => 'Prefix match: matches everything that starting with /match'
登錄后復(fù)制
try_files指令
該指令嘗試不同的路徑,并返回找到的任何路徑。
try_files $uri index.html =404;
登錄后復(fù)制
因此,/foo.html將嘗試按以下順序返回文件:
$uri(/foo.html);index.html
登錄后復(fù)制
如果未找到:404
如果我們?cè)诜?wù)器上下文中定義try_files,然后定義查找所有請(qǐng)求的位置,則不會(huì)執(zhí)行try_files。發(fā)生這種情況是因?yàn)榉?wù)器上下文中的try_files定義了其偽位置,該偽位置是可能的最低特定位置。因此,定義location/ 會(huì)比我們的偽位置更具體。
server { try_files $uri /index.html =404; location/{ }}
登錄后復(fù)制
因此,我們應(yīng)該避免在服務(wù)器上下文中使用try_files:
server { location/{ try_files $uri /index.html =404; }}
登錄后復(fù)制
以上就是Nginx – 最小配置的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!