2017-06-22 74 views
0

我有/var/www/vhosts/example.com/app/public和/var/www/vhosts/example.com/app/static中的靜態文件,我希望nginx檢入。 IE瀏覽器。 domain.com/photo.png可以位於公共目錄或靜態目錄中。Nginx的try_files多個目錄

這是我的nginx配置,但與try_files一樣,我的整個網站給出了nginx 500內部服務器錯誤。沒有這一行,我的nodejs表示應用程序加載正常,但是當然沒有靜態文件可以訪問。我有什麼錯誤?

location ~/{ 
    root /var/www/vhosts/domain.com/app; 
    try_files /public/$uri /static/$uri; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_pass http://localhost:3000; 
} 
+0

你打算用這個既反向代理和服務中的文件? 另外,如果你在linux上,請粘貼'nginx -t'的輸出 – Yadvendar

+0

是的。輸出nginx -t: nginx:配置文件/etc/nginx/nginx.conf語法正常 nginx:配置文件/etc/nginx/nginx.conf測試成功 –

回答

0

首先,你應該告訴我們localhost:3000做什麼後端:

  1. 它作爲後備使用時在兩個/static/public位置沒有找到文件;
  2. 或者它被用作HTTP API來處理用戶對某些URL,方法等的請求。

在這兩種情況下,您應該將您的配置分成兩個不同的位置。

在第一種情況下,您應該使用try_filesproxy_pass作爲後備。

location/{ 
    root /var/www/vhosts/domain.com/app; 
    try_files /public/$uri /static/$uri @fallback; 
} 

location @fallback { 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_pass http://localhost:3000; 
} 

在第二種情況下,你應該分裂的URL地址,這樣的事情

location/{ 
    root /var/www/vhosts/domain.com/app; 
    try_files /public/$uri /static/$uri @fallback; 
} 

location /api { 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_pass http://localhost:3000; 
}