[英]django nginx static files 404
这是我的设置:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'
这是我的 nginx 配置:
server {
server_name 77.241.197.95;
access_log off;
location /static/ {
alias /home/django-projects/tshirtnation/staticfiles/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
我已经运行python manage.py collectstatic
并复制了所有静态文件。 我用 gunicorn_django --bind:my-ip:8001 运行我的服务器,除了静态文件外,一切似乎都在工作。
编辑:我跑了
sudo tail /var/log/nginx/error.log
并且似乎没有找不到静态文件的错误:/
我遇到了同样的问题,并通过删除/static/
location中的尾随/
来修复我的nginx配置。
location /static { # "/static" NOT "/static/"
# ...
}
尝试将^~
前缀修饰符添加到静态位置以跳过检查正则表达式:
location ^~ /static/ {
alias /home/django-projects/tshirtnation/staticfiles/;
}
您的问题是始终使用location /
块,即使在location /static/
一个之后,所以一切都将是proxy_pass
ed。
这里的解决方案是使用一些try_files
魔法。
server {
server_name 77.241.197.95;
access_log off;
location / {
try_files $uri @django;
}
location /static/ {
alias /home/django-projects/tshirtnation/staticfiles/;
try_files $uri =404;
# here we use =404 because there is no need to pass it to gunicorn.
}
location @djago {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
在你的settings.py中,输入:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
STATIC_ROOT = "/home/django-projects/tshirtnation/staticfiles/"
STATIC_URL = '/static/'
你不需要这个:
STATICFILES_DIRS = ...
我认为浏览器试图找到你的静态:
http://127.0.0.1:8001/static/
虽然nginx默认工作在80端口上。
您需要在nginx配置中定义8001端口或在80端口上运行django服务器。
检查一下这个
1 nginx是否可以访问静态旧版本,我的意思是文件夹权限。
2或者这样做
替换这个:
STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'
有了这个
STATIC_ROOT =''
并在设置中添加此项
STATICFILES_DIRS = (
'/home/django-projects/tshirtnation/staticfiles/',
)
不要忘记重新加载nginx服务器。
希望这有效。
我看起来像这样,它有效:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), '..', 'static'),
)
在nginx配置中,我的位置/ static /位于该位置之上/像这样,
location /static {
//
}
# Finally, send all non-media requests to the Django server.
location / {
//
}
还有一件事,我不知道这是否重要,但我确实在服务器{}中有'监听'。 我不确定它是否可以提供帮助。
在settings.py中尝试这个:
import os
ROOT_PATH = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(ROOT_PATH,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
和配置文件在nginx(或您的服务器设置):
location /static {
alias /home/djangohome/static; # your Django project's static files - amend as required
}
settings.py:
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/static/'
MEDIA_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/media/'
MEDIA_URL = '/media/'
在nginx配置中(/ etc / nginx / sites-enabled / default)
server {
server_name localhost;
access_log off;
location /static/ {
alias /home/calosh/PycharmProjects/Proyecto_AES/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
然后重启nginx服务器:
sudo service nginx restart
并运行gunicorn:
gunicorn PFT.wsgi
在localhost或整个本地网络上(在端口80上)提供应用程序。
http://127.0.0.1/
我遇到了这个问题并通过将这些行添加到项目urls.py
文件来解决它:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
] += staticfiles_urlpatterns()
还有另一个原因导致这种问题,它位于/etc/nginx/nginx.conf
nginx 主配置文件中。 确保此文件包含以下行:
include /etc/nginx/mime.types;
default_type application/octet-stream;
尝试删除nginx设置中的斜杠,然后是静态,例如它应该是“/ static”而不是“/ static /” ,如果您的设置很好,那么尝试在本地计算机上重新加载服务器并尝试在远程计算机上重新启动。 我遇到了类似但重新启动机器后修复了问题。
将STATIC_URL与域一起使用。 这一点很重要!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.