我在 Ubuntu 22.04.4 服务器中使用 Django+Gunicorn+Nginx 作为我的 Web 应用程序。我一直遵循本指南直到最后,我的网络正在生产中运行。然而,静态文件不起作用(css、图像和js),我觉得我已经尝试了一切。
仅当我运行 django 开发服务器并在本地主机上激活“调试”时,我的静态文件才有效。
我的项目位于/home/user/visor/prod/visor_scs/。我的静态文件位于 /var/www/myweb.com/static/ 中,我也尝试将其放在 /home/user/visor/prod/visor_scs/static/ 中,但没有结果(在相应调整我的 nginx 配置后)。
这是我与静态文件相关的settings.py :
INSTALLED_APPS = [
...,
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/myweb.com/static/'
这是我的gunicorn服务:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/visor/prod/visor_scs
ExecStart=/home/user/visor/prod/visor_scs/prod-envisor/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
visor_scs.wsgi:application
[Install]
WantedBy=multi-user.target
这是我在 /var/nginx/sites-available/visor_scs 中的nginx 配置:
server {
listen 80;
server_name myweb.com;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /var/www/myweb.com/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
我还尝试使用别名而不是 root 并将静态文件放置在项目目录中。每次更改后,我都会重新启动 nginx 和 Gunicorn 服务并运行collectstatic。我还向 www-data 用户授予了静态目录和项目目录的权限和所有权。
我检查了 Gunicorn 和 nginx 的日志,除了所有静态文件的 404 之外,没有任何东西引起我的注意。
最后,这是浏览器开发控制台中的错误:
GET
http://myweb.com/static/visor/styles.css
GET
http://myweb.com/static/visor/leaflet_style.css
GET
http://myweb.com/static/visor/img/logo_vertical_mediano.png
[HTTP/1.1 404 Not Found 105ms]
El recurso de “http://myweb.com/static/visor/styles.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
El recurso de “http://myweb.com/static/visor/leaflet_style.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
GET
http://myweb.com/static/visor/styles.css
GET
http://myweb.com/static/visor/leaflet_style.css
El recurso de “http://myweb.com/static/visor/styles.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
El recurso de “http://myweb.com/static/visor/leaflet_style.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
GET
http://myweb.com/static/visor/img/apple-touch-icon.png
[HTTP/1.1 404 Not Found 33ms]
GET
http://myweb.com/static/visor/img/favicon-16x16.png
[HTTP/1.1 404 Not Found 24ms]
我觉得我正在用头撞墙,我将提供任何相关信息/日志。如果您已经读到这里,感谢您的阅读。