Nginx配置靜態資源
阿新 • • 發佈:2018-06-11
方式 字符 AR rec HR express ocs docs http
- 打開 /etc/nginx/sites-available 的 default文件
sudo cd /etc/nginx/sites-available sudo vim default
- 修改default文件添加要匹配的url路徑
格式:
location 要匹配的路徑{ root 映射到服務器文件的父路徑 }
laction
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, locationlocation 有兩種匹配路徑的方式:前綴字符串匹配(prefix string)和正則表達式匹配(regular expression)
默認會優先前綴字符串匹配,再進行正則表達式匹配
符號的使用:
若 正則表達式匹配成功 則使用其匹配結果
否則 使用前綴字符串匹配的結果
此時 前綴字符串匹配也失敗的話 就會報錯- ~* 對大小寫不敏感
- ~ 對大小寫敏感
- = / 精確匹配 (用於常用的路徑匹配,能增加匹配速度)
- ^~ 不匹配正則表達式
ex:
```
location = / {
[ configuration A ]
}location / {
[ configuration B ]
}location /documents/ {
[ configuration C ]
}location ^~ /images/ {
[ configuration D ]
}location ~* .(gif|jpg|jpeg)$ {
[ configuration E ]
}```
- “/” match configuration A
- “/index.html” match configuration B
- “/documents/document.html” match configuration C
- “/images/1.gif” match configuration D
- /documents/1.jpg match configuration E
root
Syntax: root path;
Default:
root html;
Context: http, server, location, if in location
Sets the root directory for requests.ex:
location /i/ { root /data/w3; }
/data/w3/i/top.gif文件將被發送以響應“/i/top.gif”請求。
參考:nginx wiki
Nginx配置靜態資源