带有 django swagger rest api 的 Nginx 静态文件 404 [英] Nginx static files 404 with django swagger rest api

查看:25
本文介绍了带有 django swagger rest api 的 Nginx 静态文件 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Django rest API 应用程序,并且 swagger 在本地运行良好.我正在尝试使用容器对其进行配置并将其部署在 ECS 上.

现在,当我构建并运行容器时,应用程序运行良好(我的意思是出现了 swagger UI).当我将应用程序负载平衡器附加到它时,在浏览器上它给了我 404 个未找到 js 和 CSS 文件的文件.

这是网页浏览器的控制台输出.

这是我的 Nginx 配置文件

# nginx.defaultadd_header X-Content-Type-Options nosniff;包括/etc/nginx/mime.types;add_header X-XSS-Protection "1;模式=块";服务器 {听8020;access_log/var/log/nginx/access.log;error_log/var/log/nginx/error.log;地点/{proxy_pass http://127.0.0.1:8010;proxy_set_header 主机 $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}位置/静态{root/home/app/codebase;}}

docker容器内静态文件夹的路径是/home/app/codebase/static/

还在 Django Settings.py 文件中添加了以下几行.

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

不确定我错过了什么.任何线索将不胜感激.我调查了这些在 ALB 的基于路径的路由中,最后一条路由(当请求未找到任何路由时流量使用该路由)将流量定向到一个虚拟目标组.一旦我改变了目标群体,一切都会变得很有吸引力.

工作正常

专业提示:始终在最后一个 ALB 规则中使用应用程序中的任何目标组.

I have Django rest API application and swagger for that is working fine locally. I am trying to configure it using containers and deploy it on ECS.

Now when I build and run the container the application works fine (I mean the swagger UI is appearing). When I attach the application load balancer to it, on the browser it is giving me 404 files not found for js and CSS files.

Here is the console output from the web browser.

Here is my Nginx config file

# nginx.default
add_header X-Content-Type-Options nosniff;
include /etc/nginx/mime.types;
add_header X-XSS-Protection "1; mode=block";

server {
    listen 8020;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    
    location / {
        proxy_pass http://127.0.0.1:8010;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        root /home/app/codebase;
    }
}

THe path of static folder inside docker container is /home/app/codebase/static/

Also added the following lines in the Django Settings.py file.

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

Not sure what am I missing. Any leads will be appreciated. I have looked into these questions. The problem is something similar but not sure what I'm missing.

解决方案

Posting the solution in case anyone still facing the same kind of issue. I provisioned all the resources using IaC (Cloud Formation) so while provisioning Load Balancer I created the one target group but at that time the ECS services were not created, so I created a dummy target group that drops the traffic and was not routing the traffic to any service. And in ALB's path-based routing, the last route (which the traffic uses when the request does not find any route) was directing traffic to a dummy target group. As soon as I changed the target group everything works like a charm.

Working Fine

Pro Tip: Always use any of the target groups from your application in the last ALB rule.

这篇关于带有 django swagger rest api 的 Nginx 静态文件 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆