简体   繁体   English

Django + Nginx在提供静态文件时给出了404

[英]Django+Nginx gives 404 while serving static files

Whenever I load my site it gives me a 404 error while loading the images and css. 每当我加载网站时,加载图像和CSS时都会出现404错误。

My nginx config: 我的nginx配置:

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 media files - amend as required
location /media  {
    alias /home/django/django_project/django_project/media;
}

# your Django project's static files - amend as required
location /static/ {
    alias /home/django/django_project/csite/static/csite/;
    autoindex on;
}

# Proxy the static assests for the Django Admin panel
location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_buffering off;

        proxy_pass http://app_server;
}

}

My settings.py : 我的settings.py

STATIC_ROOT = os.path.join(BASE_DIR, "csite/static")

STATIC_URL = '/static/'

You can view the directory structure here 您可以在这里查看目录结构

My app name is csite . 我的应用程序名称为csite

Try this one 试试这个

location /static/ {
    root /home/django/django_project/csite/;
}

This is an answer according to my case, but you should find an answer to your issue. 根据我的情况,这是一个答案,但是您应该找到问题的答案。 In my case, I have : 就我而言,我有:

My Django IP server : 172.30.10.92 我的Django IP服务器:172.30.10.92

My Nginx IP server : 172.30.10.93 我的Nginx IP服务器:172.30.10.93

1- Install and Configure wsgi (located on Django server) 1-安装和配置wsgi(位于Django服务器上)

WSGI is a file created with your Django project. WSGI是使用Django项目创建的文件。

The file is located in /path/to/your/project/Myproject/wsgi.py 该文件位于/path/to/your/project/Myproject/wsgi.py

We have to edit this file like this : 我们必须像这样编辑该文件:

import os
from django.core.wsgi import get_wsgi_application

import sys sys.path.append('/var/www/html/Myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Myproject.settings")
application = get_wsgi_application()

2- Install and Configure gunicorn/supervisor (located on Django server) 2-安装和配置gunicorn / supervisor(位于Django服务器上)

In order to install gunicorn/supervisor, you have to execute in your shell : 为了安装gunicorn / supervisor,您必须在shell中执行:

pip install gunicorn
pip install supervisor

Then, you have to create a new file in /etc/supervisor/conf.d/Myproject.conf which looks like this : 然后,您必须在/etc/supervisor/conf.d/Myproject.conf创建一个新文件,如下所示:

[program:Myproject]
command = /home/valentin/.virtualenvs/MyprojectEnv/bin/gunicorn Myproject.wsgi:application --name "Myproject" --workers=4 --bind=0.0.0.0:8080 -- user="valentin" --group="valentin" ; Command to start app
user = username #You have to replace by your username
stdout_logfile = /var/log/supervisor/supervisor.log
redirect_stderr = true
log
environment=LANG=fr_FR.UTF-8,LC_ALL=fr_FR.UTF-8

I specified port 8080 which is the communication port between my application server and my web server. 我指定了端口8080 ,这是我的应用程序服务器和Web服务器之间的通信端口。

3- Edit hosts file on nginx server (located on nginx server) 3-编辑Nginx服务器上的主机文件(位于Nginx服务器上)

You have to edit your hosts file located to /etc/hosts and add a new entry to your Django server : 您必须编辑位于/etc/hosts hosts文件,并将新条目添加到Django服务器:

127.0.0.1 localhost 
127.0.1.1 valentin 
172.30.10.92 Myproject

# The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

4- New config file in nginx repository (located on nginx server) 4- Nginx存储库中的新配置文件(位于Nginx服务器上)

This new file should be placed in /etc/nginx/sites-available/Myproject.conf 这个新文件应该放在/etc/nginx/sites-available/Myproject.conf

 server {
    listen 8080;
    server_name Myproject;

    root /var/www/html/Myproject/;

    location /static/ {
        root /var/www/html/;
    }

    location / {
        include proxy_params;
        proxy_pass http://172.30.10.92:8080;
        }       
}

The IP address corresponds to my Django server address. 该IP地址对应于我的Django服务器地址。 I specified the listen port (8080), the path to my Django project & static directory. 我指定了侦听端口(8080),即我的Django项目和静态目录的路径。

Then, you have to create a symbolic link to sites-enabled . 然后,您必须创建一个指向sites-enabled的符号链接。

After ths operation, restart nginx service : 操作之后,重新启动nginx服务:

sudo service nginx restart

5- Allow nginx IP address in Django (located on Django server) 5-在Django中允许nginx IP地址(位于Django服务器上)

You have to edit your settings.py file in order to allow nginx IP address in ALLOWED_HOSTS : 您必须编辑settings.py文件,以便在ALLOWED_HOSTS中允许nginx IP地址:

ALLOWED_HOSTS = ['localhost', '172.30.10.93', '127.0.0.1', '[::1]']

6- Finally execute gunicorn (located on Django server) 6-最后执行gunicorn(位于Django服务器上)

Finally, you have to start gunicorn. 最后,您必须开始使用gunicorn。 You should be inside your Django root project and execute : 您应该在Django根项目中并执行:

gunicorn Myproject.wsgi:application --bind 172.30.10.92:8080

Now, in your browser, try to connect to your nginx server with the port : 现在,在浏览器中,尝试使用端口连接到您的nginx服务器:

http://172.30.10.93:8080

It works ! 有用 !

EDIT 编辑

Your static directory haven't be placed in the same directory than your django project. 您的静态目录未与django项目放置在同一目录中。 Create a new static directory, specify the path in your settings.py file and then, make python manage.py collectstatic . 创建一个新的静态目录,在settings.py文件中指定路径,然后使python manage.py collectstatic

Don't forget to edit your nginx files. 不要忘记编辑您的nginx文件。

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

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