2015-04-04 71 views
0

Nginx設置爲將我的節點服務器9200代理到端口80,根設置爲服務mypath/to/node_build。我想上傳一些靜態文件到/projects從位置/項目直接請求到不同的根,Nginx?

所以我做location /projects { root /mypath/to/static_projects}但我得到一個404錯誤。

server { 
     listen 80; 

     root /var/www/example.io/public_html/dist; 
     index index.html index.htm; 

     # Make site accessible from http://example.io 
     server_name example.io; 
     server_name app.example.io; 

     location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
      expires 1y; 
     } 

     location/{ 
      proxy_pass http://127.0.0.1:9200; 
      proxy_http_version 1.1; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection 'upgrade'; 

      proxy_set_header Host $host; 
      proxy_cache_bypass $http_upgrade; 
     } 

     location ^~ /blog { 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_set_header X-NginX-Proxy true; 

      proxy_pass http://127.0.0.1:9200; 
      proxy_redirect off; 
     } 

     location /projects { 
      root /var/www/example.io/public_html/projects; 
     } 

     gzip on; 
     gzip_proxied any; 
     gzip_types text/plain text/xml text/css application/x-javascript; 
     gzip_vary on; 
     gzip_disable 「msie6″; 


     #error_page 404 /404.html; 

} 

回答

1

當你有一個位置塊匹配/ ... nginx的將繼續匹配其他塊 - 見:nginx docs on "location"

location/{ 
    # matches any query, since all queries begin with /, but regular 
    # expressions and any longer conventional blocks will be 
    # matched first. 
    [ configuration B ] 
} 

location /documents/ { 
    # matches any query beginning with /documents/ and continues searching, 
    # so regular expressions will be checked. This will be matched only if 
    # regular expressions don't find a match. 
    [ configuration C ] 
}  

所以,如果你有以上的匹配靜態文件的另一個塊(\.(js|css|png|jpg|jpeg|gif|ico)$ ),這些將最終使用您的默認root

你可以做的是把位置擋你/projects塊來處理靜態文件或創建匹配與開始/projects路徑的靜態文件的正則表達式的新位置塊內。

location /projects { 
    root /var/www/example.io/public_html/projects; 
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
     expires 1y; 
     try_files $uri =404; 
    } 
} 

# or: 

location ~* ^/projects/.+\.(js|css|png|jpg|jpeg|gif|ico)$ { 
    root /var/www/example.io/public_html/projects; 
    expires 1y; 
    try_files $uri =404; 
} 
+0

哦沒關係,所以這是問題,呃。我會很快回到配置並回傳結果和檢查。 – 2015-04-05 05:16:39

相關問題