为什么nginx不会在子文件夹中提供我的静态文件?

时间:2014-08-06 00:02:05

标签: nginx

以下是配置:

server {
    listen       80;
    server_name  turfmetrics.com.au www.turfmetrics.com.au;
    access_log   /var/log/nginx/turfmetrics.com.au.access.log;
    error_log    /var/log/nginx/turfmetrics.com.au.error.log;
    root /home/turfmetrics/production/;

    auth_basic "In development: Please log in";
    auth_basic_user_file /home/turfmetrics/htpasswords;

    location  /assets/ {
        alias  /home/turfmetrics/production/static_collected/assets/;
    }

    location  /static/ {
        alias  /home/turfmetrics/production/static_collected/;
    }

    location  /media/ {
        alias  /home/turfmetrics/production/media/;
    }

    location  = /favicon.ico {
        alias  /home/turfmetrics/production/media/favicon.ico;
    }

    location  = /robots.txt {
        alias  /home/turfmetrics/production/media/robots.txt;
    }

    location  / {
        proxy_pass            http://127.0.0.1:8000;
        proxy_redirect        off;
        proxy_set_header      Host             $host;
        proxy_set_header      X-Real-IP        $remote_addr;
        proxy_set_header      X-Forwarded-For  $proxy_add_x_forwarded_for;
        client_max_body_size  10m;
    }
    allow all;
}

如果我访问http://turfmetrics.com.au/static/assets/img/up.png,我会看到图片,但如果我访问http://turfmetrics.com.au/assets/img/up.png,我会收到404。

这个文件只是一个例子。我需要资产'快捷方式'因为各种javascript文件都使用资产路径。

1 个答案:

答案 0 :(得分:1)

我通过使位置指令更加明确来解决这个问题。如果找不到资产,静态或媒体文件,我不想在任何其他地方插入网址,我希望它失败。因此,我用每个静态文件查找前缀为^〜前缀:

server {
    listen       80;
    server_name  turfmetrics.com.au www.turfmetrics.com.au;
    access_log   /var/log/nginx/turfmetrics.com.au.access.log;
    error_log    /var/log/nginx/turfmetrics.com.au.error.log;
    root /home/turfmetrics/production/;

    auth_basic "In development: Please log in";
    auth_basic_user_file /home/turfmetrics/htpasswords;

    location  ^~ /assets/ {
        alias  /home/turfmetrics/production/static_collected/assets/;
    }

    location  ^~ /static/ {
        alias  /home/turfmetrics/production/static_collected/;
    }

    location  ^~ /media/ {
        alias  /home/turfmetrics/production/media/;
    }

    location  = /favicon.ico {
        alias  /home/turfmetrics/production/media/favicon.ico;
    }

    location  = /robots.txt {
        alias  /home/turfmetrics/production/media/robots.txt;
    }

    location  / {
        proxy_pass            http://127.0.0.1:8000;
        proxy_redirect        off;
        proxy_set_header      Host             $host;
        proxy_set_header      X-Real-IP        $remote_addr;
        proxy_set_header      X-Forwarded-For  $proxy_add_x_forwarded_for;
        client_max_body_size  10m;
    }
    allow all;
}

这看起来无论如何都是静态文件查找的更好实践,因为我不希望它们进入常规处理(没有以/ static /开头且与文件不对应的有效URL在static_collected文件夹中。)

我不确定没有这个设置静态是如何工作的,但事实确实如此。我猜测我的资产中找不到的404是由location /指令下的夹层生成的。

对于那里的Django开发者;我将此添加到我的urls.py中,以便让本地django-admin.py runserver获取相关文件:

# Serve static media during development so things look right
if settings.DEBUG:
    urlpatterns += patterns(
        '',
        (r'^{0}/(?P<path>.*)$'.format(settings.MEDIA_URL.strip('/')),
         'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
        (r'^{0}/(?P<path>.*)$'.format(settings.ASSET_URL.strip('/')),
         'django.views.static.serve', {'document_root': settings.ASSET_ROOT}))