I have written a bare minimal Python fastcgi script webapp.py
for test:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys, os, traceback
from html import escape
from flup.server.fcgi import WSGIServer
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
yield 'hello world'
WSGIServer(app, bindAddress=('127.0.0.1',3001)).run()
I can start the script ./webapp.py
no problem. I can also establish connection to localhost:3001 with telnet.
I then create an Nginx default configuration like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass localhost:3001;
}
}
With this bare minimal configuration on my local machine, I started nginx and try to access the http://localhost. Nginx default site fails (502 Bad Gateway). In the log message, I can only see error like this repeatedly:
2017/01/05 01:23:07 [error] 30464#30464: *3 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:3001", host: "localhost"
What is going wrong with my settings or code?
If you, like I did, followed the Python3 official HOWTO: Use Python in the web, installed flup-py3 1.0.2-1 and got confused. Don't be. It is not your mistake.
There is nothing wrong with the code and settings above. The issue was how I installed flup-py3. This is how I installed it:
sudo pip3 install flup-py3
which you SHOULD NOT do.
The answer to your question (and mine) is simple. The "stable" version 1.0.2-1 doesn't work on Python 3 at all. So the official HOWTO fxxked up in a big way.
Until the official stable version on pypi is updated, you should install your flup-py3 by either:
sudo pip3 install hg+https://hg.saddi.com/flup-py3.0/#egg=flup-py3
or:
sudo pip3 install git+https://github.com/pquentin/flup-py3.git#egg=flup-py3
The first one (hg) would be the development version of flup-py3 of the original developer Allan Saddi. While the second one (github version) is a copy created by Quentin Pradet. Both source include patches in 2012 - 2014 that fixes flup's compatibility issue.
The latest version of flup-py3 (>=1.0.3) has already fixed this issue. You can install that with pip normally.