使用nginx加载django admin静态文件时出现500错误,但是在向网址

时间:2016-10-12 03:20:06

标签: django unix nginx django-staticfiles

我正在将django网络应用程序部署到运行ubuntu的nginx服务器

当我尝试登录使用默认django auth系统的应用程序时,我收到“服务器错误(500)”消息

当我查看/var/log/nginx/error.log时,它会显示以下消息:

2016/10/11 22:35:53 [error] 16325#0: *42863 open()     "/path_to_app/app_name/static/admin/js/jquery.min.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/jquery.min.js HTTP/1.1", host: "domain_name.com", referrer: "http://nu-tab.me/accounts/login/?next=/"
2016/10/11 22:35:53 [error] 16325#0: *42865 open() "/path_to_app/app_name/static/admin/js/jquery.init.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/jquery.init.js HTTP/1.1", host: "domain_name.com", referrer: "http://nu-tab.me/accounts/login/?next=/"
2016/10/11 22:35:53 [error] 16325#0: *42866 open() "/path_to_app/app_name/static/admin/js/admin/RelatedObjectLookups.js" failed (2: No such file or directory), client: ip, server: _, request: "GET /static/admin/js/admin/RelatedObjectLookups.js HTTP/1.1", host: "domain_name.com", referrer: "domain_name.com/accounts/login/?next=/"

但是,当我加载domain_name.com/static/admin/js/admin/RelatedObjectLookups.js时,文件会正确加载。

还值得注意的是,error.log文件尚未针对最近的请求进行更新。

对设置服务器不是很熟悉所以请告诉我这里是否有重要的信息我将要离开。

nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

在sites-enabled / django中:

    upstream app_server { 
        server 127.0.0.1:9000 fail_timeout=0; 
    } 

    server { 
        listen 80 default_server; 
        listen [::]:80 default_server ipv6only=on; 

        root /usr/share/nginx/html; 
        index index.html index.htm; 

        client_max_body_size 4G; 
        server_name _; 

        keepalive_timeout 5; 

        # your Django project's static files - amend as required 
        location /static/ { 
            alias /path_to_app/app_name/static/;  
        } 

        # your Django project's static files - amend as required
        location /media/ {
            alias /path_to_app/app_name/media/;
        }

        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
        }
    }

相关的Django设置:

STATIC_URL = '/static/'
STATIC_ROOT = '/path_to_app/app_name/static/'

MEDIA_URL = '/media/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'app_name', 'static'),
)

0 个答案:

没有答案