【问题标题】:Bare minimal flup fastcgi server not working with Nginx裸露的最小flup fastcgi服务器无法与Nginx一起使用
【发布时间】:2017-05-19 02:20:39
【问题描述】:

我已经编写了一个简单的 Python fastcgi 脚本webapp.py 进行测试:

#!/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()

我可以启动脚本./webapp.py 没问题。我也可以通过 telnet 建立到 localhost:3001 的连接。

然后我创建一个像这样的 Nginx 默认配置:

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;
    }
}

在我的本地机器上使用这个最基本的配置,我启动了 nginx 并尝试访问http://localhost。 Nginx 默认站点失败(502 Bad Gateway)。在日志消息中,我只能重复看到这样的错误:

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"

我的设置或代码出了什么问题?

【问题讨论】:

    标签: python python-3.x nginx fastcgi


    【解决方案1】:

    我的错误

    如果你和我一样,跟着 Python3 官方 HOWTO: Use Python in the web,安装了 flup-py3 1.0.2-1 并感到困惑。不要这样。这不是你的错。

    上面的代码和设置没有问题。问题是我如何安装flup-py3。我是这样安装的:

    sudo pip3 install flup-py3
    

    不应该这样做。

    解决方案

    您(和我的)问题的答案很简单。 “稳定”版本 1.0.2-1 根本不适用于 Python 3。所以官方的 HOWTO 大变样了。

    在 pypi 上的官方稳定版本更新之前,您应该通过以下任一方式安装您的 Flup-py3:

    sudo pip3 install hg+https://hg.saddi.com/flup-py3.0/#egg=flup-py3
    

    或:

    sudo pip3 install git+https://github.com/pquentin/flup-py3.git#egg=flup-py3
    

    第一个 (hg) 是原开发者 Allan Saddi 的 Flup-py3 开发版本。而第二个(github 版本)是由Quentin Pradet 创建的副本。这两个来源都包含 2012 - 2014 年修复 Flup 兼容性问题的补丁。

    相关链接

    2018 年更新 **

    最新版本的flup-py3 (>=1.0.3) 已经修复了这个问题。可以正常使用 pip 安装。

    【讨论】:

      猜你喜欢
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多