2017-03-16 113 views
1

我想从运行Nginx的Fedora框上的教程中设置一个简单的Python Web服务器;我希望Nginx能够反向代理Python服务器。但是,我必须做出错误的事情,因为当我运行服务器并尝试通过Nginx加载页面时,Nginx会将502返回到浏览器并将以下内容打印到日志中:连接被拒绝 - Nginx到Python BaseHTTPServer

2017/03/16 00:27:59 [错误] 10613#0:* 5284 connect()失败(111: 连接被拒绝),同时连接到上游,客户端: 76.184.187.130,server:tspi.io,请求:“GET/leaderboard/index.html的HTTP/1.1" ,上游: “http://127.0.0.1:8063/leaderboard/index.html”, 主机: “tspi.io”

这是我的蟒服务器:

#!/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提供。

当我谷歌,我看到吨的反向代理的东西PHP没有PHP-FPM配置正确,因为我没有使用PHP似乎不大可能。我也看到关于配置uwsgi的东西,我不知道这是否是一个问题。我不知道BaseHTTPServer是否使用uswgi;当我尝试寻找uswgi时,它看起来像是一组完全不同的类以及另一种编写python服务器的方式。

任何帮助将非常感谢!

回答

1

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

我也建议发送主机和远程地址值到您的内部应用程序,以防出现需要。

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

谢谢!还注意到,除了这个错字,我的python没有正确处理url路径中的“排行榜”前缀。在原始文章中更新了代码。 –