A
A
Alexey Poloz2019-03-04 13:22:53
Nginx
Alexey Poloz, 2019-03-04 13:22:53

NGINX + Gunicorn + Flask Why do I get 500 Internal Server Error when I make POST requests?

I use Flask with NGINX & Gunicorn.
When I start the standard FLask server, everything is ok, all requests work.
When I connected gunicorn POST requests stopped working for me. But I solved the problem by starting with 3 workers.
gunicorn --bind 0.0.0.0:5000 wsgi:app -w 3
Farther. I connect NGINX.
My configs:

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        gzip on;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/myproject

server {
    listen 80;
    server_name 157.230.103.16 www.157.230.103.16;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/root/blend/myproject.sock;
    }
}

/etc/systemd/system/myproject.service

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/root/blend
Environment="PATH=/home/root/blend/flask/bin"
ExecStart=/home/root/blend/flask/bin/gunicorn --bind unix:myproject.sock wsgi:app -w 3

[Install]
WantedBy=multi-user.target

All GET requests work correctly. POST requests do not come at all. Runs also with 3 workers.
Logs:
access log

177.54.88.78 - - [04/Mar/2019:00:28:25 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7"
195.19.247.81 - - [04/Mar/2019:00:42:24 +0000] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537$
195.19.247.81 - - [04/Mar/2019:00:58:37 +0000] "GET / HTTP/1.1" 200 3278 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/53$
195.19.247.81 - - [04/Mar/2019:00:58:41 +0000] "GET /members/ HTTP/1.1" 500 291 "http://157.230.103.16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) C$
195.19.247.81 - - [04/Mar/2019:01:00:01 +0000] "GET / HTTP/1.1" 200 3278 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/53$
...
195.19.247.81 - - [04/Mar/2019:01:38:21 +0000] "GET /members/ HTTP/1.1" 500 291 "http://157.230.103.16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) C$
95.213.177.126 - - [04/Mar/2019:01:41:00 +0000] "POST http://check.proxyradar.com/azenv.php?auth=155166365977&a=PSCMN&i=2649122576&p=80 HTTP/1.1" 404 580 "https://proxyradar.com/" "Mozilla/4.0 ($
152.249.19.127 - - [04/Mar/2019:01:41:08 +0000] "GET / HTTP/1.1" 200 8623 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
46.236.65.228 - - [04/Mar/2019:01:46:03 +0000] "GET / HTTP/1.1" 200 8623 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
...

error log

2019/03/04 01:00:12 [alert] 11248#11248: *2 open socket #11 left in connection 4
2019/03/04 01:00:12 [alert] 11248#11248: aborting
2019/03/04 01:01:06 [crit] 849#849: *1 connect() to unix:/home/root/blend/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 195.19.247.81, server: 157.23$
2019/03/04 01:01:54 [alert] 849#849: *2 open socket #11 left in connection 4
2019/03/04 01:01:54 [alert] 849#849: aborting
2019/03/04 01:31:46 [alert] 1547#1547: *1 open socket #3 left in connection 3
...

On the /members page, a POST request is made to the same server /. But there is no query in the logs. Although some left POST request then comes later, they can still be accepted. From another server, I also cannot make a POST request to this one.
Why is this happening. How to properly configure NGINX?
There are many guides and the problem is frequent, but nothing helped me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Poloz, 2019-03-09
@kosyachniy

You need to set up logging

import logging
logging.basicConfig(filename='error.log',level=logging.DEBUG)

The error turned out to be that the global environment was launched, not the virtual environment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question