为什么nginx不显示Django和gunicorn的静态内容?

2024-03-28 21:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用gunicorn 19.19.0和nginx1.10.3在一个带有python3.5.3和django 2.1.7的raspberry pi3(raspian 9)上运行django管理应用程序。错误日志似乎是空的。但应用程序不会显示任何静态内容。在

我检查了nginx.conf公司文件。在

我运行collectstatic检查所有文件都在那里。在

我可以将浏览器指向192.168.1.20/static,它会显示正确的目录。在

我也可以浏览所有的文件。在

我试着沿着nginx.conf公司用“/”归档

管理应用程序的所有功能都可以正常工作。只是没有静态内容。在

我在谷歌上搜索并阅读/尝试了我能找到的所有论坛修复。在

我还运行了python开发服务器(python管理.py运行服务器)。在那个配置中,静态内容显示得很好。在

在nginx.conf公司文件

events{}

http {
    server {
            listen       80;
            server_name  localhost;

            location /static {
                    autoindex on; 
                    alias /home/pi/DigitalClock/dcvenv/static;
            }
            location / {
                    error_log /home/pi/DigitalClock/dcvenv/nginx_err.log;
                    access_log /home/pi/DigitalClock/dcvenv/nginx_acc.log;
                    proxy_pass http://127.0.0.1:8000;
            }
    }
}

gunicorn启动命令

^{pr2}$

django项目设置文件

^{3}$

ngnix的最后一个条目_会计记录(*_错误日志为空)

192.168.1.10--[2019年2月18日:12:45:43-0800]“发布/管理/登录/?next=/admin/HTTP/1.1“302 0”http://192.168.1.20/admin/login/?next=/admin/“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/71.0.3578.98 Safari/537.36” 192.168.1.10---[18/Feb/2019:12:45:43-0800]“GET/admin/HTTP/1.1”200 4944“http://192.168.1.20/admin/login/?next=/admin/”“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像壁虎一样)Chrome/71.0.3578.98 Safari/537.36” 192.168.1.10---[18/Feb/2019:12:45:59-0800]“GET/admin/auth/group/HTTP/1.1”200 3500“http://192.168.1.20/admin/”“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/71.0.3578.98 Safari/537.36” 192.168.1.10---[18/Feb/2019:12:45:59-0800]“GET/admin/jsi18n/HTTP/1.1”200 3185“http://192.168.1.20/admin/auth/group/”“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/71.0.3578.98 Safari/537.36”


Tags: 文件loghttpmozillaadminwindowsnginxchrome
1条回答
网友
1楼 · 发布于 2024-03-28 21:24:11

将此代码放入设置.py,那么您就有了collectstatic,同时测试DEBUG = True

ROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # specify static root

在url项目中添加

^{pr2}$

更新在您的项目中尝试此方法。

urlpatterns = patterns('',
....urls......
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

在你的设置.py在

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
REPOSITORY_ROOT = os.path.dirname(BASE_DIR)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(REPOSITORY_ROOT, 'static/')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'media/')

相关问题 更多 >