简体   繁体   English

Nginx没有为Django提供静态文件

[英]Nginx not serving static files for Django

I have set up a django server, using this guide: 我使用本指南设置了django服务器:

https://gist.github.com/evildmp/3094281 https://gist.github.com/evildmp/3094281

The uwsgi is serving correctly, but the static files are obviously missing as they are the responsibility of nginx. uwsgi服务正常,但静态文件显然缺失,因为它们是nginx的责任。 So it seems nginx is my issue. 所以似乎nginx是我的问题。 My nginx config is the same as what is on that guide. 我的nginx配置与该指南中的配置相同。

But here is my base.py settings file: 但这是我的base.py设置文件:

########## PATH CONFIGURATION
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))

# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)

BASE_DIR = dirname(dirname(abspath(__file__)))

# Site name:
SITE_NAME = "sasite"

# Add our project to our pythonpath, this way we don't need to type our project
# name in our dotted import paths:
path.append(DJANGO_ROOT)
########## END PATH CONFIGURATION

########## MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
########## END MEDIA CONFIGURATION

########## UPLOAD CONFIGURATION

UPLOAD_ROOT = join(BASE_DIR, 'uploads')

UPLOADS_URL = '/uploads/'

FILE_UPLOAD_HANDLERS = (
    "django.core.files.uploadhandler.MemoryFileUploadHandler",
    "django.core.files.uploadhandler.TemporaryFileUploadHandler",
)

########## END UPLOAD CONFIGURATION

########## STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
STATICFILE_ROOT = normpath(join(SITE_ROOT, 'static'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    normpath(join(SITE_ROOT, 'static')),
)

My config as I said is the same as in that guide I linked, the paths to the directories are correct, but I also have that assets directory which has some static files that support certain javascript plugins and such as I am using a theme. 我说的配置与我链接的指南相同,目录的路径是正确的,但我也有资产目录,其中有一些静态文件支持某些javascript插件,比如我正在使用主题。 I assume adding another entry in my nginx.conf for assets would help with that, but nonetheless, I added a file to /media/ called 1.txt. 我假设在我的nginx.conf中为资产添加另一个条目会有所帮助,但是,我在/ media /中添加了一个名为1.txt的文件。 When I go to the domainname.com:8001 (where uwsgi is serving) it comes up with no css, no javascript, etc. When I try to reach domainname.com:8000/media/1.txt (which nginx is serving) I get errors, such as empty response or it just does not work. 当我去domainname.com:8001(uwsgi正在服务)时,它没有css,没有javascript等。当我尝试访问domainname.com:8000/media/1.txt(nginx正在服务)我收到错误,例如空响应或者它不起作用。

Since uWSGI is obviously serving the site, and no static files are being served, how can I fix nginx so that it is correctly serving the static files, and so that when I visit domainname.com:8000 it will show the full website with static files and all, since uWSGI is just supposed to be behind the nginx reverse-proxy. 由于uWSGI显然是在为站点提供服务,并且没有提供静态文件,我如何修复nginx以便它正确地提供静态文件,因此当我访问domainname.com:8000时,它将显示带有静态的完整网站文件和所有文件,因为uWSGI应该只是在nginx反向代理之后。

I could use some help here, I understand how it all works but I just cant seem to get it working properly. 我可以在这里使用一些帮助,我理解这一切是如何工作的但我似乎无法让它正常工作。

Thanks for any assistance you can provide. 感谢您提供的任何帮助。

EDIT: 编辑:

nginx.conf nginx.conf

# nginx.conf
upstream django {
    # connect to this socket
    # server unix:///tmp/uwsgi.sock;    # for a file socket
    server 127.0.0.1:8001;      # for a web port socket
    }

server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .cshenkan.com;   # substitute your machine's IP address or FQDN
    charset     utf-8;

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

    # Django media
    location /media  {
            alias /home/ubuntu/sasite-rewrite/media;      # your Django pro$
    }

    location /static {
            alias /home/ubuntu/sasite-rewrite/static;     # your Django pro$
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # or the uwsgi_params you installe$
        }
    }

and the command I use to run uwsgi: 和我用来运行uwsgi的命令:

uwsgi --http :8001 --chdir /home/ubuntu/sasite-rewrite --wsgi-file /home/ubuntu/sasite-rewrite/sasite/wsgi.py

and the wsgi.py file: 和wsgi.py文件:

from os.path import abspath, dirname
from sys import path
from django.core.wsgi import get_wsgi_application

SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sasite.settings.production")

application = get_wsgi_application()

I have tried using unix sockets instead, changing the nginx config to use the unix sockets rather than web sockets, and by changing my uwsgi command to 我尝试使用unix套接字,更改nginx配置以使用unix套接字而不是web套接字,并将我的uwsgi命令更改为

uwsgi --socket /tmp/uwsgi.sock --chdir /home/ubuntu/sasite-rewrite --wsgi-file /home/ubuntu/sasite-rewrite/sasite/wsgi.py uwsgi --socket /tmp/uwsgi.sock --chdir / home / ubuntu / sasite-rewrite --wsgi-file /home/ubuntu/sasite-rewrite/sasite/wsgi.py

But when I do that it does not work at all when I navigate to domainname.com:8000 and since I am no longer using the web socket for port 8001, I cannot test uwsgi alone by going to domainname.com:8001. 但是当我这样做时,当我导航到domainname.com:8000并且因为我不再使用端口8001的web套接字时它根本不起作用,我不能通过访问domainname.com:8001来单独测试uwsgi。 Again it seems nginx is not working, and I'm not sure how to test uwsgi alone with unix sockets. 再次看来nginx不起作用,我不知道如何用unix套接字单独测试uwsgi。

Any idea what I am doing wrong? 知道我做错了什么吗? I followed a few guides, and tried several methods, none seem to work, the closest I got is the gist I linked a the top. 我跟着几个指南,尝试了几种方法,似乎都没有工作,我得到的最接近的是我把顶部联系起来的要点。 But when I tried to change to unix sockets (as I said) it seems to not work at all. 但是,当我尝试更改为unix套接字时(正如我所说)它似乎根本不起作用。

EDIT 2: 编辑2:

So now my new base.py settings file looks like this: 所以现在我的新base.py设置文件如下所示:

STATIC_ROOT = normpath(join(SITE_ROOT, 'static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    normpath(join(SITE_ROOT, 'static')),
    normpath(join(SITE_ROOT, 'assets')),
)

Will this configuration work with the way I have it set up? 这种配置是否与我设置的方式一致? I have static/ as my typical django static directory, but assets/ is for the theme I am using and I need static files served from there as well. 我有静态/作为我典型的django静态目录,但assets /是我正在使用的主题,我也需要从那里提供静态文件。 If I add it to STATICFILES_DIRS will that do the trick? 如果我将它添加到STATICFILES_DIRS会做的伎俩吗? Do I need to add anynthing to nginx.conf? 我需要在nginx.conf中添加任何东西吗? For example craete something for assets in the nginx.conf the same way I set up static? 例如,我在nginx.conf中为资产设置了一些东西,就像我设置静态一样?

eg add this to nginx.conf: 例如,将其添加到nginx.conf:

location /assets  {
        alias /home/ubuntu/sasite-rewrite/assets
} 

Is this the correct approach? 这是正确的方法吗? Unfortunately I'm having trouble getting nginx to work at all for static files, so I cant test it. 不幸的是我无法让nginx完全适用于静态文件,所以我无法测试它。 Just curious if I am doing it the correct way . 只是好奇我是不是以正确的方式做到了。

Your STATIC_ROOT is ../assets, not ../static, so that is where you should point your nginx location alias for /static. 你的STATIC_ROOT是../assets,而不是../static,所以你应该在这里为/ static指出你的nginx位置别名。

You have also defined a STATICFILE_ROOT setting to point to ../static, but that is not a Django setting so it does not do anything. 您还定义了STATICFILE_ROOT设置以指向../static,但这不是Django设置,因此它不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM