引言:大家好,我是熱愛coding,崇尚開源,樂于分享的【皮卡丘的貓】
相信大家在使用nginx程序中,nginx.conf 里面的location 配置可能還一知半解,本篇文章盡可能讓你熟悉如何去配置使用location之妙法,
話不多說,先上原始的server塊配置
server {
listen 89;
server_name localhost;
location / {
root html;
index test.html;
}
}
首先大家肯定知道,在地址欄輸入http://xx:89 ,默認的是尋找html目錄下的test.html檔案,假如服務器路徑為:/home/test/files/img/test.png,可以修改配置如下
server {
listen 89;
server_name localhost;
location /test {
root /home;
}
}
在地址欄輸入的是:http:xx:89/test/xx.png,定位到服務器尋找的拼接目錄是從/home/test 尋找靜態資源 ,為了對前端進行透明,root 可以換種別名的方式來處理,再比如:
server {
listen 89;
server_name localhost;
location /static {
alias /home/test;
}
}
這樣,通過別名的方式前端就不知道后端靜態資源所在目錄位置了,
接下來講一下location的匹配規則決議
-
空格:默認匹配,普通匹配
location / { root /home; } -
=:精確匹配
location = /test/img/face1.png { root /home; } -
~*:匹配正則運算式,不區分大小寫
# 符合圖片的顯示 location ~* .(GIF|jpg|png|jpeg) { root /home; } -
~:匹配正則運算式,區分大小寫
# GIF必須大寫才能匹配到 location ~ .(GIF|jpg|png|jpeg) { root /home; } -
^~:以某個字符路徑開頭
location ^~ /test/img { root /home; }
至此,location的用法就講完了,大家可以去實操一下吧,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291440.html
標籤:其他
上一篇:Js-RPC微博登陸案例
下一篇:【Express】—路由配置
