Upstream closing down connections for uwsgi, Flask and Nginx stack

空扰寡人 提交于 2020-04-18 03:57:21

问题


I am trying to run a basic Flask app using Nginx 1.14.0 on Ubuntu Server 18.04.

The app itself runs fine in test environment but I am trying to deploy it now with uwsgi and nginx and am just getting either the default nginx landing page or a 502 Bad Gateway.

I removed the nginx default config from /etc/nginx/sites-available and deleted the symlink from /etc/nginx/sites-enabled.

I set replacements for my site as below in /etc/nginx/sites-available.

What am I missing in terms of config to make nginx redirect to my site?

server {
            listen 80;
            server_name www.myserver.com myserver.com;
            root /srv/server/myserver/;
            index index.html;

            location /static {
                alias /srv/server/myserver/static;
            }

            location / {
                include uwsgi_params;
                uwsgi_pass unix:/srv/server/myserver/myserver.sock;
                uwsgi_read_timeout 30;
                uwsgi_connect_timeout 30;
            }

        }

I created a symlink sudo ln -s /etc/nginx/sites-available/myserver/etc/nginx/sites-enabled

/srv/server is owned by www-data using sudo shown -R www-data:www-data /srv/server

and this is is myserver.ini

[uwsgi]
http = 0.0.0.0:80
harakiri = 30
module = wsgi:app

master = true
processes = 5

binary-path = /srv/server/myserver/venv/bin/uwsgi
virtualenv = /srv/server/myserver/myserverenv
module = myserver:app

uid = www-data
gid = www-data

socket = myserver.sock
chmod-socket = 0775
vacuum = true

die-on-term = true

myserver.service

[Unit]
Description=uWSGI instance for myserver

[Service]
User=www-data
Group=www-data
After=network.target
WorkingDirectory=/srv/server/myserver
Environment="PATH=/srv/server/myserver/myserverenv/bin"
ExecStart=/srv/server/myserver/myserverenv/bin/uwsgi --ini myserver.ini

[Install]
WantedBy=multi-user.target

As this is on my local machine I have added the below to /etc/hosts in order to access via FQDN in the browser while I test and I have allowed for http and https with ufw.

127.0.0.1 www.myserver.com myserver.com

I have stopped, started, restarted etc via sudo systemctl restart nginx

Error logs from /etc/nginx/error.log

2020/04/17 15:42:24 [error] 26747#26747: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: www.myserver.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/srv/server/myserver/myserver.sock:", host: "www.myserver.com"

EDIT:

I tried restarting uwsgi and got teh below error when running either as www-data and via sudo:

3therk1ll@3therk1ll:/var/log/nginx$ sudo -u www-data systemctl status uwsgi
● uwsgi.service - uWSGI instance for myserver
   Loaded: loaded (/etc/systemd/system/uwsgi.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-04-17 16:30:42 BST; 5s ago
  Process: 27147 ExecStart=/srv/server/myserver/myserverenv/bin/uwsgi --ini myserver.ini (code=exited, status=1/FAILURE)
 Main PID: 27147 (code=exited, status=1/FAILURE)

3therk1ll@3therk1ll:/var/log/nginx$ sudo systemctl status uwsgi
● uwsgi.service - uWSGI instance for myserver
   Loaded: loaded (/etc/systemd/system/uwsgi.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-04-17 16:30:42 BST; 1min 10s ago
  Process: 27147 ExecStart=/srv/server/myserver/myserverenv/bin/uwsgi --ini myserver.ini (code=exited, status=1/FAILURE)
 Main PID: 27147 (code=exited, status=1/FAILURE)

Apr 17 16:30:42 3therk1ll uwsgi[27147]: dropping root privileges as early as possible
Apr 17 16:30:42 3therk1ll uwsgi[27147]: your processes number limit is 7645
Apr 17 16:30:42 3therk1ll uwsgi[27147]: your memory page size is 4096 bytes
Apr 17 16:30:42 3therk1ll uwsgi[27147]: detected max file descriptor number: 1024
Apr 17 16:30:42 3therk1ll uwsgi[27147]: lock engine: pthread robust mutexes
Apr 17 16:30:42 3therk1ll uwsgi[27147]: thunder lock: disabled (you can enable it with --thunder-lock)
Apr 17 16:30:42 3therk1ll uwsgi[27147]: error removing unix socket, unlink(): Permission denied [core/socket.c line 198]
Apr 17 16:30:42 3therk1ll uwsgi[27147]: bind(): Address already in use [core/socket.c line 230]
Apr 17 16:30:42 3therk1ll systemd[1]: uwsgi.service: Main process exited, code=exited, status=1/FAILURE
Apr 17 16:30:42 3therk1ll systemd[1]: uwsgi.service: Failed with result 'exit-code'.

回答1:


Both nginx and uwsgi try to bind port 80, so try to change uwsgi's port to something different value or just delete the http = 0.0.0.0:80 line from uwsgi config, since nginx talking with uwsgi by unix socket



来源:https://stackoverflow.com/questions/61274428/upstream-closing-down-connections-for-uwsgi-flask-and-nginx-stack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!