Nginx到Python BaseHTTP的连接被拒绝

2024-10-01 17:37:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个关于运行Nginx的Fedora设备的教程中设置一个简单的pythonweb服务器;我希望Nginx反向代理Python服务器。不过,我一定是做错了什么,因为当我运行服务器并尝试通过Nginx加载页面时,Nginx会向浏览器返回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公司公司名称:

^{pr2}$

我正在尝试使用proxy_pass来传递传入的任何流量tspi.io/排行榜在Python服务器上,同时允许Nginx正常地为/data/www下的基本html页面提供服务。在

当我在google上搜索时,我看到大量关于反向代理PHP没有正确配置PHP-FPM的东西,而且由于我根本没有使用PHP,这看起来不太可能。我也看到了一些关于配置uwsgi的东西,我不知道这是否是个问题。我不知道BaseHTTPServer是否使用uswgi;当我尝试查找uswgi时,它似乎是一组完全不同的类和编写python服务器的另一种方式。在

任何帮助都将不胜感激!在


Tags: topathioself服务器sendindexif
1条回答
网友
1楼 · 发布于 2024-10-01 17:37:53

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

我还建议将主机和远程地址值发送到内部应用程序,以备需要。在

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

相关问题 更多 >

    热门问题