3

我将 django 与 nginx 和 gunicorn 一起使用。nginx 应该提供静态内容,但浏览器中没有加载 css、图像和 js 文件。这是为什么?

我已将 Django 项目的名称替换为domain.

/etc/nginx/sites-enabled/domain.tld

server {
    listen 80;
    server_name 127.0.0.1;

    access_log /srv/domain/access.log;
    error_log /srv/domain/error.log;

    location /static {
        alias /srv/domain/collected_static;
    }

    location / {
        proxy_pass http://127.0.0.1:8888;
   }
}

/etc/nginx/conf/nginx.conf

user http;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
include /etc/nginx/sites-enabled/*;

}

gunicorn.conf.py

bind = "127.0.0.1:8888"
logfile = "/srv/domain/gunicorn.log"
loglevel = "info"
workers = 3

摘自 Django 设置

DEPLOY_PATH = os.path.dirname(os.path.realpath(__file__))

STATIC_ROOT = os.path.join(DEPLOY_PATH, 'collected_static')

STATIC_URL = '/static/'

编辑: 机器的输出(链接到 pastebin):

ps辅助| grep nginx

ls -l *.log

4

1 回答 1

2

您的配置看起来正确。只要文件真的被收集起来,Django 和 Gunicorn 配置就与静态文件服务无关。我想到了以下可能性:

  • 文件尚未收集到您的collected_static 目录中(./manage.py collectstatic
  • Nginx 对文件没有读取权限
  • 您使用的旧 nginx 版本与当前配置存在问题。您应该使用当前的 1.x 版本,如果您使用的是 Debian,请使用来自 nginx.org 的 Deb 存储库。

如果权限不是问题,请检查 nginx 访问文件以查看请求是否真的到达 Nginx。然后检查 nginx 错误日志以查看是否记录了任何错误。

作为旁注(但不相关),我建议在您的/位置配置中放置一些代理标头并将应用服务器配置移动到单独的部分,例如:

upstream app_server {
    server localhost:8888 fail_timeout=0;
}


server {
    ...
    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;
        break;
    }
}
于 2012-04-09T17:41:47.340 回答