连接被拒绝——Nginx 到 Python BaseHTTPServer

connection refused -- Nginx to Python BaseHTTPServer

我正在尝试根据 Fedora box 运行ning Nginx 上的教程设置一个简单的 Python Web 服务器;我想让 Nginx 反向代理 Python 服务器。不过,我一定是做错了什么,因为当我 运行 服务器并尝试通过 Nginx 加载页面时,Nginx returns 向浏览器发送 502 并将以下内容打印到日志中:

2017/03/16 00:27:59 [error] 10613#0: *5284 connect() failed (111: Connection refused) while connecting to upstream, client: 76.184.187.130, server: tspi.io, request: "GET /leaderboard/index.html HTTP/1.1", upstream: "http://127.0.0.1:8063/leaderboard/index.html", host: "tspi.io"

这是我的 python 服务器:

#!/bin/env python
# with special thanks to the good folks at
# https://fragments.turtlemeat.com/pythonwebserver.php
# who generous taught me how to do all this tonight

import cgi

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from os import curdir, sep

class BaseServer (BaseHTTPRequestHandler):

    def do_GET (self):
        try:
            print ('Serving self.path=' + self.path)
            if 'leaderboard' in self.path:
                self.path = self.path[12:]
                print ('self.path amended to:' + self.path)

            if self.path == '/':
                self.path = '/index.html'

            if self.path.endswith ('.html'):
                # maybe TODO is wrap this in a file IO exception handler
                f_to_open = curdir + sep + self.path
                f = open (f_to_open)
                s = f.read()
                f.close()

                self.send_response (200)
                self.send_header ('Content-type', 'text/html')
                self.end_headers ()
                self.wfile.write (s)

            return

        except IOError:
            self.send_error (404, 'File Not Found: ' + self.path)

    def do_POST (self):
        try:
            cytpe, pdict = cgi.parse_header(self.headers.getheader ('content-type'))
            if ctype == 'multipart/form-data':
                query=cgi.parse_multipart (self.rfile, pdict)
            self.send_response (301)

            self.endheaders()


        except:
            pass # What *do* you do canonically for a failed POST?

def main():
    try:
        server = HTTPServer (('', 8096), BaseServer)
        print ('Starting BaseServer.')
        server.serve_forever ()
    except KeyboardInterrupt:
        print ('Interrupt recieved; closing server socket')
        server.socket.close()

if __name__ == '__main__':
    main()

还有我的 nginx.conf:

server {
    listen 443 ssl;
    server_name tspi.io;
    keepalive_timeout 70;

    ssl_certificate /etc/letsencrypt/live/tspi.io/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/keys/0000_key-certbot.pem;
    ssl_protocols TLSv1.2;

    location / {
        root /data/www;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }

    location /leaderboard {
        proxy_pass http://localhost:8063;
    }
}

我正在尝试使用 proxy_pass 将任何进入 tspi.io/leaderboard 的流量传递到 Python 服务器,同时允许基础 html 位于 /data/www 下的页面将由 Nginx 正常提供服务。

当我 google 时,我看到大量关于反向代理 PHP 没有正确配置 PHP-FPM 的东西,因为我没有使用 PHP这似乎不太可能。我还看到有关配置 uwsgi 的内容,我不知道这是否是一个问题。不知道BaseHTTPServer有没有用uswgi;当我尝试查找 uswgi 时,它似乎是一组完全不同的 类 和另一种编写 python 服务器的方法。

非常感谢任何帮助!

您的 python 代码中的端口号与您的 nginx 反向代理配置中提供的端口号混合匹配。

我还建议将主机和远程地址值发送到您的内部应用程序,以备不时之需。

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;