Nginx的索引不在项目的根目录下

我有一个index.html页面与其余的客户端资源( /opt/django/media/index.html )一起存储。 我可以让nginx将页面作为请求的索引提供给域名,但是它的作用就好像它位于项目根目录下,而不是在媒体目录中。 这意味着像images/123.png的页面中可以访问的images/123.png现在必须通过index.html中的media/images/123.png 。 我应该只更新页面中的资源path还是有更好的方法呢? 我的configuration如下:

 server { listen 80; server_name localhost; access_log /opt/django/logs/nginx/vc_access.log; error_log /opt/django/logs/nginx/vc_error.log; # no security problem here, since / is alway passed to upstream root /opt/django/; location = / { index media/index.html; } # serve directly - analogous for static/staticfiles location /media/ { # if asset versioning is used if ($query_string) { expires max; } } location /static/ { # if asset versioning is used if ($query_string) { expires max; } } location /metro/ { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 10; proxy_pass http://localhost:8080/; } location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 10; proxy_pass http://localhost:8000/; } # what to serve if upstream is not available or crashes error_page 500 502 503 504 /media/50x.html; } 

这是一个try_files的工作。

像这样的东西应该让你开始:

 server { # ... root /opt/django; location @django { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 10; proxy_pass http://localhost:8000/; } # what to serve if upstream is not available or crashes error_page 500 502 503 504 /media/50x.html; location / { try_files /media$uri $uri $uri/ @django; } }