iT邦幫忙

0

Django+ uwsgi+ nginx on ubuntu 出現 502 Bad Gateway

  • 分享至 

  • xImage

請教各位高手下設定列狀態不知為何會出現 502 Bad Gateway 感謝回覆
目前系統狀態如下:
/etc/nginx/sites-available/default
https://ithelp.ithome.com.tw/upload/images/20221102/20124313fKeSeXRg8w.png
uwsgi.ini
https://ithelp.ithome.com.tw/upload/images/20221102/20124313KyaVubaqVJ.png
下netstat -natp顯示如下
https://ithelp.ithome.com.tw/upload/images/20221102/20124313bDWzzYs9jK.png
django專案目錄
https://ithelp.ithome.com.tw/upload/images/20221102/20124313AoPHgK1WDx.png

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2022-11-03 10:41:53 檢舉
1. 請善用程式碼區塊,不要用圖片上傳。
2. location / 區塊內的 root 和 index 那兩行拿掉,重啟nginx試試。
jim_lai iT邦新手 5 級 ‧ 2022-11-03 11:01:54 檢舉
謝謝回覆,我立馬修正了default,得到結果還是一樣

目前環境是想把對外58080對應至docker 8080,Django project 安裝在docker 內

1. docker 上一層下 netstat -natp如下

(No info could be read for "-p": geteuid()=1002 but you should be root.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:58080 0.0.0.0:* LISTEN -
tcp6 0 0 :::443 :::* LISTEN -
tcp6 0 0 :::58080 :::* LISTEN -

2. 訪問58080 curl http://xx.xx.xx.xx:58080 得到結果如下
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
3. docker 內下 netstat -natp如下

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3177/nginx: master
tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN 3188/uwsgi
tcp6 0 0 :::8080 :::* LISTEN 3177/nginx: master

4.訪問docker 內 8080 curl http://localhost:8080得到結果如下
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
jim_lai iT邦新手 5 級 ‧ 2022-11-03 11:05:21 檢舉
5. docker 內訪問8088 curl http://localhost:8088結果如下:
curl: (52) Empty reply from server
jim_lai iT邦新手 5 級 ‧ 2022-11-03 11:09:52 檢舉
6. 若docker 不採用nginx 改用tomcat 訪問對外的58080及docker 內的8080都是正常可開啟網頁
froce iT邦大師 1 級 ‧ 2022-11-03 11:41:57 檢舉
看起來像是uwsgi和django之間的連結問題。
你nginx和uwsgi以及django都在同一容器?
方便貼一下uwsgi.log嗎?
jim_lai iT邦新手 5 級 ‧ 2022-11-03 13:19:46 檢舉
感謝回覆,uwsgi.log 如下
*** Starting uWSGI 2.0.21 (64bit) on [Thu Nov 3 05:17:14 2022] ***
compiled with version: 11.2.0 on 02 November 2022 02:11:27
os: Linux-3.10.0-1160.24.1.el7.x86_64 #1 SMP Thu Apr 8 19:51:47 UTC 2021
nodename: bb4574f7f05b
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /opt/awebIC/awebIC
writing pidfile to uwsgi.pid
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /opt/awebIC
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8088 fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]
Python main interpreter initialized at 0x555b76bdc410
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 416880 bytes (407 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x555b76bdc410 pid: 3260 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3260)
spawned uWSGI worker 1 (pid: 3261, cores: 2)
spawned uWSGI worker 2 (pid: 3262, cores: 2)
spawned uWSGI worker 3 (pid: 3264, cores: 2)
spawned uWSGI worker 4 (pid: 3266, cores: 2)
jim_lai iT邦新手 5 級 ‧ 2022-11-03 13:20:36 檢舉
Django+ uwsgi+ nginx 都在同一個容器
froce iT邦大師 1 級 ‧ 2022-11-03 15:14:12 檢舉
看uwsgi log又沒啥不對勁的地方...怪事。
nginx的log呢?
jim_lai iT邦新手 5 級 ‧ 2022-11-03 15:26:30 檢舉
謝謝回覆,下列是nginx error.log
2022/11/03 06:56:36 [error] 3178#3178: *40 connect() failed (111: Unknown error) while connecting to upstream, client: xx.xx.xx.xx, server: oo.oo.oo.oo, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8088", host: "00.00.00.00:58080"
jim_lai iT邦新手 5 級 ‧ 2022-11-03 15:37:18 檢舉
我在自己的mac是正常的移植到 GCP就不行
froce iT邦大師 1 級 ‧ 2022-11-04 08:26:08 檢舉
看來錯誤應該就是nginx到uwsgi這一塊了。
試試看不要用TCP連線,用unix socket連?
jim_lai iT邦新手 5 級 ‧ 2022-11-04 15:00:17 檢舉
感謝引導,有發現問題出在哪,主要是出在這小小訊息沒看到ModuleNotFoundError: No module named 'tzdata'
XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答