![](/img/trans.png)
[英]django+gunicorn+nginx gives 404 while serving static files
[英]django+gunicorn+nginx 404 serving static files
我在 192.168.1.81:3000 上运行 django+gunicorn+nginx。 Web 应用程序不会提供任何静态文件; 它返回 404 错误。 这表明 nginx 虚拟服务器配置文件存在问题。 我已经尝试了几种关于堆栈溢出的解决方案,但都没有成功。 nginx虚拟服务器文件有什么问题?
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
location /static/ {
alias /home/pi/door_site/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
在服务器部分内的 Nginx 虚拟服务器 conf 文件中,您需要添加到位置部分,以便 Nginx 可以为 /static/ 和 /media/ 文件夹中的文件提供服务:
location /media {
alias /path/to/your/folder/media;
}
location /static {
alias /path/to/your/folder/static;
}
在测试 Nginx 配置之后:
sudo nginx -t
并重新加载 Nginx:
sudo nginx -s reload
(或重新启动 - sudo /etc/init.d/nginx restart
)
使用服务器根目录和 try_files 尝试此配置
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
root /path/to/root # place your static directories in root i.e /path/to/root/static
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
这将尝试找到您的静态文件,然后移至您的应用服务器。
确保 nginx 以正确的用户身份运行以访问您的文件,并且您的静态文件权限可能已正确设置:
chmod -R u=rwX,g=rwX,o=rX static_dir
我的情况是静态目录的权限问题,它在分配适当的权限后工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.