【问题标题】:Django + nginx + uwsgi doesn't show staticDjango + nginx + uwsgi 不显示静态
【发布时间】:2014-11-16 19:23:08
【问题描述】:

我有一个 Django 项目并尝试使用 nginx 和 uwsgi 部署它(从现在开始一周)。 我可以访问服务器,但它不显示图片和 css。 nginx 提供静态文件(例如 localhost:8000/static/images/image.png 有效)但在 localhost:8001/ 上什么也没有(此页面应显示 image.png)。

这是我的 nginx.conf

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    #server unix:///home/serveur/www/gems/GEMS/gems.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 192.168.0.119; # substitute your machine's IP address or FQ
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/serveur/www/gems/GEMS/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/serveur/www/gems/GEMS/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/serveur/www/gems/uwsgi_params; # the uwsgi_params file you installed
    }
}

settings.py:

TEMPLATE_DIRS = ('/home/serveur/www/gems/GEMS/templates'),
STATIC_URL = '/static/' #'/home/serveur/www/gems/GEMS/static/'
STATIC_ROOT = "/home/serveur/www/gems/GEMS/static/" #os.path.join(BASE_DIR, "static/")
MEDIA_ROOT = '/home/serveur/www/gems/GEMS/static/media'
STATICFILES_DIR = ('/home/serveur/www/gems/GEMS/static')
import django.contrib.auth
django.contrib.auth.LOGIN_URL = '/'

wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GEMS.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我通过以下方式启动 uwsgi:

    uwsgi --http :8001 --wsgi-file GEMS/wsgi.py --chdir /home/serveur/www/gems/GEMS --virtualenv /home/serveur/www/gems --chmod-socket=666

有人有想法吗?

谢谢

【问题讨论】:

  • 你跑了 manage.py collectstatic?
  • 是的,我做到了,但还是不行
  • 你能从 nginx 错误日志中为静态 url 发布一行吗?
  • 2014/11/16 19:06:53 [错误] 4437#0: *11 open() "/home/server/www/gems/GEMS/static" 失败(13:权限被拒绝),客户端:192.168.0.24,服务器:192.168.0.119,请求:“GET /static HTTP/1.1”,主机:“192.168.0.119:8000” 但是我更改了权限,因为我可以访问。奇怪的是,当我连接到端口 8001 时,gems.access.log 中没有出现任何内容

标签: python django nginx static uwsgi


【解决方案1】:

从外观上看,Nginx 配置似乎是使用上游 django 配置设置的,该配置指向 gems.sock,这是一个 Unix 套接字文件。

uWSGI 服务器似乎通过端口 8001 上的 http 提供服务。uWSGI 服务器应该通过在 Nginx conf 中为上游 Django 指令设置的相同 Unix 套接字文件 gems.sock 提供服务。

尝试使用选项 --socket gems.sock 而不是 --http :8001。确保您位于 gems.sock 所在的当前目录中,或者为选项提供 gems.sock 的完整路径

【讨论】:

  • 我评论了这一行:服务器 unix:/// 但是当我使用套接字文件时它返回:无法从 GEMS/wsgi.py 加载配置
  • 可能是wsgi.py的相对路径不对。也许尝试将其设置为绝对路径,看看是否可行。
  • 我,deos不工作。这很奇怪,上周它有效(但我遇到了另一个问题)
猜你喜欢
  • 2017-06-05
  • 2015-04-28
  • 1970-01-01
  • 2015-02-19
  • 2012-08-13
  • 2017-09-02
  • 2014-05-31
  • 2016-11-24
  • 2016-02-02
相关资源
最近更新 更多