2017-03-16 14 views
1

Nginxを実行しているFedoraボックスのチュートリアルから簡単なPython Webサーバーを設定しようとしています。 NginxにPythonサーバーのプロキシをリバースさせたい。私は、サーバーを実行し、nginxの経由ページをロードしようとすると、nginxのは、ブラウザに502を返し、ログに次のように出力するので、私は、しかし、何か間違ったことをやっている必要があります。接続が拒否されました - NginxからPython BaseHTTPServer

2017年3月16日00:27:59 [エラー] 10613#0:* 5284 connect()failed(111: 接続が拒否されました)上流への接続中にクライアント: 76.184.187.130、server:tspi.io、request: "GET/leaderboard/index.htmlにHTTP/1.1" 、上流: "http://127.0.0.1:8063/leaderboard/index.html"、 ホスト: "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; 
    } 
} 

ベースHTMLを可能にしながら、私は、Pythonのサーバーにtspi.io/leaderboardに着信トラフィックを渡すためにproxy_passを使用しようとしていますNginxが通常提供する/ data/wwwの下にあるページ。

私がGoogleになったとき、逆プロキシのPHPについて、正しく設定されていないPHP-FPMがたくさんあることがわかりました。 uwsgiの設定についての情報もありますが、それは問題なのかどうかわかりません。 BaseHTTPServerがuswgiを使用しているかどうかはわかりません。私がuswgiを見てみると、それはPythonサーバーを書くための全く別のクラスと全く別のもののようでした。

ご協力いただければ幸いです。

答えて

1

ポート番号は、Pythonコードとnginxリバースプロキシ設定で提供されているものとが混在しています。

また、ホストとリモートアドレスの値を必要に応じて内部アプリケーションに送信することをお勧めします。

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

ありがとうございます!また、そのtypoに加えて、私のpythonがurlパスの "leaderboard"接頭辞を正しく処理していないことにも気付きました。元の投稿のコードを更新しました。 –

関連する問題