Nginx之server和location配置使用


配置server代碼段:

server {
     server_name www.meiduo.site;
     listen 8080;
     root /home/python/Desktop/meiduo_mall_admin/dist;
     index index.html;
}

其中:

server_name : 監聽的域名

listen : 監聽的端口

root : 網站的根路徑

index : 默認訪問的文件

配置location代碼段:

server {
     server_name www.meiduo.site;
     listen 8080;
     location / {
         root /home/python/Desktop/meiduo_mall_admin/dist;
         index index.html;
    }
 }

其中:

location字段需要寫在server字段內,一個server字段可以包含多個location字段。

其余屬性意思同上。

注意:

location _ {} :這里的_表示匹配規則,加上=_表示嚴格匹配,不加_表示不嚴格匹配。

例如:

server {
    server_name www.meiduo.site;
    listen 8080;
    location / {
        root /home/python/Desktop/meiduo_mall_admin/dist;
        index index.html;
     }
    # 表示只有輸入www.meiduo.site/image/時,才走這個location
     location =/image/ {
         root /home/python/Desktop/meiduo_mall_admin/dist;
         index index.html;
     }
 }

 在配置完配置文件以后,需要測試一下語法是否出錯和重新加載配置文件。

sudo nginx -t
sudo nginx -s reload


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM