Назад | Перейти на главную страницу

Индекс Nginx не в корне проекта

У меня есть страница 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;
}

Это работа для 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;
    }
}