![Nginx 索引不在项目根目录](https://linux22.com/image/590594/Nginx%20%E7%B4%A2%E5%BC%95%E4%B8%8D%E5%9C%A8%E9%A1%B9%E7%9B%AE%E6%A0%B9%E7%9B%AE%E5%BD%95.png)
我有一个 index.html 页面,它与我的其他客户端资源 ( /opt/django/media/index.html
) 一起存储。我可以让 nginx 将该页面作为仅针对域名的请求的索引,但它会将该页面视为位于项目根目录中,而不是位于媒体目录中。这意味着,以前可以在页面中访问的图像等内容现在images/123.png
必须通过media/images/123.png
我的 index.html。我应该只更新页面中的资源路径,还是有更好的方法?我的配置如下:
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;
}
答案1
这是一份工作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;
}
}