nginx 靜態目錄配置規則

一、子目錄匹配 
以下配置 javascript

location / {  
    root /data/www;  
}  

  訪問http://127.0.0.1/時,配匹配/data/www 
訪問http://127.0.0.1/images時,配匹配/data/www/images 
訪問http://127.0.0.1/images/1.jpg時,配匹配/data/www/images/1.jpg 
也就是說,地址欄裏"/"後的路徑是直接匹配目錄data/www/下的路徑 html

 

location /images/ {  
    root /data/www;  
}

  訪問http://127.0.0.1/images時,配匹配/data/www/images 
也就是說,地址欄裏/images,直接匹配了/data/www的子目錄. 
常常出問題的是,location裏的url隨意配了一個名字,如/xxx,可是對應的/data/www目錄 
下並無該/data/www/xxx子目錄,一訪問就404
 java

 

二、重複路徑匹配規則 nginx

server {  
    location / {  
        root /data/www;  
    }  
  
    location /images/ {  
        root /data;  
    }  
}  

  訪問URL http://localhost/images/example.png,將會匹配第二個/images/規則, 
雖然也能夠匹配location /規則,但nginx默認會選擇最長前綴去匹配當前URL,也就是 
第二個配置會生效,訪問/data/images/目錄,而不是/data/www/images/目錄 url

 

 

 

 server {
        listen       8888;
        server_name  localhost;
	
	    
		
        location / {
          root E:/nginx-1.13.6/html/dist;
		  try_files $uri $uri/ /index.html;
		  index  index.html index.htm;		 
        }
		
		location /server/ {
		  proxy_pass http://xxxxxxx:8080/;
        }}
		
相關文章
相關標籤/搜索