【发布时间】: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