2017-06-22 3 views
0

Ich habe statische Dateien in /var/www/vhosts/example.com/app/public und /var/www/vhosts/example.com/app/static, dass ich nginx einchecken möchte. IE. domain.com/photo.png könnte entweder in den öffentlichen oder statischen Verzeichnissen sein.Nginx try_files für mehrere Verzeichnisse

Das ist meine Nginx-Konfiguration, aber mit den try_files wie, meine gesamte Website gibt den Nginx 500 internen Server Fehler. Ohne diese Zeile ist meine express-App für nodejs problemlos geladen, aber auf statische Dateien kann natürlich nicht zugegriffen werden. Was habe ich falsch?

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

Beabsichtigen Sie, diese zu verwenden, um sowohl Reverse-Proxy und die Dateien dienen? Bitte fügen Sie auch die Ausgabe von 'nginx -t', wenn Sie auf Linux sind – Yadvendar

+0

Ja, ich tue. Ausgabe von nginx -t: nginx: Die Konfigurationsdatei /etc/nginx/nginx.conf Syntax ist in Ordnung nginx: Konfigurationsdatei /etc/nginx/nginx.conf Test ist erfolgreich –

Antwort

0

Zuerst sollten Sie uns sagen, welche Backend localhost:3000 tut:

  1. Es wird als Ausweichlösung verwendet wird, wenn die Datei nicht an beiden /static und /public Standorten;
  2. Oder es wird als HTTP API verwendet, um Benutzeranfragen mit bestimmten URLs, Methoden und so weiter zu bearbeiten.

In beiden Fällen sollten Sie Ihre Konfiguration in zwei verschiedene Standorte aufteilen. Im ersten Fall sollten Sie try_files mit proxy_pass als Fallback verwenden.

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; 
} 

Im zweiten Fall, dass Sie Standorte von URL geteilt sollten, so etwas wie diese

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; 
} 
Verwandte Themen