我有一个 django 项目。
设置.py:
STATIC_URL = 'static/'
STATIC_ROOT = 'static'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
我已经做了
collectstatic
,一切都很好。现在我的服务器上的静态文件夹包含所有文件。
Nginx:
我的 nginx 遇到了问题,因为我在
sites_available
中更改了名称,它开始出现错误,所以我重新安装了它,现在日志显示一切正常:
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2024-05-01 14:47:13 UTC; 11min ago
Docs: man:nginx(8)
Process: 6843 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_proce>
Process: 6844 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (c>
Main PID: 6845 (nginx)
Tasks: 2 (limit: 1026)
Memory: 3.1M
CPU: 60ms
CGroup: /system.slice/nginx.service
├─6845 "nginx: master process /usr/sbin/nginx -g daemon on; master>
└─6846 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "">
May 01 14:47:13 23210 systemd[1]: nginx.service: Deactivated successfully.
May 01 14:47:13 23210 systemd[1]: Stopped A high performance web server and a r>
May 01 14:47:13 23210 systemd[1]: Starting A high performance web server and a >
May 01 14:47:13 23210 systemd[1]: Started A high performance web server and a r>
~
~
~
~
~
~
ESCOC
server and a reverse proxy server
/nginx.service; enabled; vendor preset: enabled)
2024-05-01 14:47:13 UTC; 11min ago
/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
inx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
ss /usr/sbin/nginx -g daemon on; master_process on;"
ss" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
service: Deactivated successfully.
d A high performance web server and a reverse proxy server.
ng A high performance web server and a reverse proxy server...
d A high performance web server and a reverse proxy server.
~
~
~
~
~
~
ESCOD
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2024-05-01 14:47:13 UTC; 11min ago
Docs: man:nginx(8)
Process: 6843 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_proce>
Process: 6844 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (c>
Main PID: 6845 (nginx)
Tasks: 2 (limit: 1026)
Memory: 3.1M
CPU: 60ms
CGroup: /system.slice/nginx.service
├─6845 "nginx: master process /usr/sbin/nginx -g daemon on; master>
└─6846 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "">
May 01 14:47:13 23210 systemd[1]: nginx.service: Deactivated successfully.
May 01 14:47:13 23210 systemd[1]: Stopped A high performance web server and a r>
May 01 14:47:13 23210 systemd[1]: Starting A high performance web server and a >
May 01 14:47:13 23210 systemd[1]: Started A high performance web server and a r>
~
~
~
~
~
~
lines 1-18/18 (END)
[2]+ Stopped systemctl status nginx
^Z
但是我不知道为什么会出现错误。
Nginx 配置:
server {
listen 80;
server_name 77.777.77.77;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/portfolio-server;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Django 没有用于提供静态文件的内置解决方案,至少在生产环境中,当 DEBUG 必须为 False 时。
我们必须使用第三方解决方案来完成此任务。
安装白噪音
pip install whitenoise
修改Settings.py
MIDDLEWARE = [
'django...............Middleware',
'django...............Middleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django...............Middleware',
'django...............Middleware',
]
然后收集静态文件
python manage.py collectstatic