2017-03-16 109 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路徑中的「排行榜」前綴。在原始文章中更新了代碼。 –