NGINX容器对接uWSGI容器出现104错误及502 Bad Gateway问题排查
问题根源
你当前的核心错误是NGINX使用uwsgi_pass试图通过uWSGI协议连接Nautobot的HTTPS(HTTP协议)端口,两者协议不匹配,导致连接被重置。
uWSGI的http/https参数是让它以HTTP/HTTPS协议监听端口,而uwsgi_pass要求后端是uWSGI原生协议的socket端口,这两种协议无法互通。
修正方案
1. 修改uwsgi.ini配置
添加uWSGI原生协议的socket监听端口,保留http/https作为测试端口(生产环境可移除):
[uwsgi] # uWSGI原生协议端口,供NGINX反向代理使用 socket = 0.0.0.0:8001 # 以下为原有HTTP/HTTPS测试端口(可选保留) http = 0.0.0.0:8080 https = 0.0.0.0:8443,/opt/nautobot/nautobot.crt,/opt/nautobot/nautobot.key strict = true master = true enable-threads = true vacuum = true single-interpreter = true die-on-term = true need-app = true disable-logging = true log-4xx = true log-5xx = true http-keepalive = 1 processes = 3 listen = 128 buffer-size = 4096
2. 修改NGINX配置
将uwsgi_pass指向Nautobot的uWSGI原生socket端口:
server { listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; server_name _; ssl_certificate /etc/ssl/certs/nautobot.crt; ssl_certificate_key /etc/ssl/private/nautobot.key; client_max_body_size 25m; location / { include uwsgi_params; # 改为uWSGI原生socket端口 uwsgi_pass nautobot:8001; uwsgi_param Host $host; uwsgi_param X-Real-IP $remote_addr; uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for; uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto; } } server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri; }
3. 可选优化(生产环境建议)
由于NGINX已经处理了前端HTTPS,Nautobot容器内无需再开启HTTPS服务,可移除https = ...配置项,减少资源消耗。
验证步骤
- 重启Nautobot和NGINX容器
- 访问NGINX的443端口,确认502错误消失
- 若需测试Nautobot直接访问,仍可通过8080端口的HTTP服务验证
内容的提问来源于stack exchange,提问作者ccie50268
相关产品推荐
相关产品推荐

