1

Apache プロキシ Web サーバーと Django SSL に問題があります。次のエラーの後に、SSL 用の Django settings.py と apache server.conf ファイル、django バージョン 1.6.8が続きます。

  ----------------------------------------
  [10/Jan/2015 09:11:33] code 400, message Bad request syntax ('\x16\x03\x00\x00?
  Exception happened during processing of request from ('5.5.0.46', 38141)
  Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
   self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
   self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 126, in __init__
   super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
   self.handle()
  File "/usr/lib/python2.7/wsgiref/simple_server.py", line 117, in handle
   if not self.parse_request(): # An error code has been sent, just exit
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 286, in parse_request
    self.send_error(400, "Bad request syntax (%r)" % requestline)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 368, in send_error
    self.send_response(code, message)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 385, in send_response
    self.log_request(code)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 422, in log_request
    self.requestline, str(code), str(size))
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 138, in log_message
   msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xf9 in position 12: ordinal not in    range(128)
  ----------------------------------------

設定.py

   ...... 

   # secure proxy SSL header and secure cookies
   SECURE_SSL_REDIRECT = True
   SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
   SESSION_COOKIE_SECURE = True
   CSRF_COOKIE_SECURE = True

   # session expire at browser close
   SESSION_EXPIRE_AT_BROWSER_CLOSE = True

   # wsgi scheme
   os.environ['wsgi.url_scheme'] = 'https'
   ......

Apache server.conf

 <IfModule mod_ssl.c>
    <VirtualHost *:80>
            ServerName mywebsite.com
            WSGIScriptAlias / /var/www/manage/manage/wsgi.py
    </VirtualHost>
    <VirtualHost _default_:443>
            ServerName mywebsite.com
            WSGIScriptAlias / /var/www/manage/manage/wsgi.py
            SSLEngine on
            SSLCertificateFile      /etc/apache2/ssl/apache.crt
            SSLCertificateKeyFile /etc/apache2/ssl/apache.key
            redirect permanent / https://5.5.0.38:8080
    </VirtualHost>
  </IfModule>

また、django wsgi.pyでHTTPSを有効にしました

  ......
  os.environ['HTTPS'] = "on"
  ..............
4

2 に答える 2

1

不正なリクエスト構文 ('\x16\x03\x00\x00?

これは、HTTP トラフィックが予期される HTTPS トラフィックです。これは、apache.conf の次の行が原因であると想定しています。

redirect permanent / https://5.5.0.38:8080

これにより、代わりに指定された URL (おそらく Django サーバー) にアクセスするようにブラウザーに指示します。リクエストを Django サーバーに転送するのではなく (おそらく意図したとおり)、代わりにブラウザーに新しいリクエストを作成し、Django サーバーからリソースを直接取得するように指示します。つまり、前に apache はありません。別のサーバーの前で Apache を使用する場合は、代わりに ProxyPass や ProxyPassReverse などを使用する必要があると思います。

ポート 8080 が実際に https に使用されるのは非常にまれです。通常、これは http のみに使用されます。したがって、Django サーバー自体はプレーンな http しか話していないと思います。

os.environ['HTTPS'] = "オン"

これは Django から HTTPS サーバーを作成するわけではありませんが、すべてのリンクを https リンクとして作成するよう Django に指示するだけです。これは、Django サーバー自体が単純な http のみを実行するという私の仮定を裏付けています。

于 2015-01-10T10:22:38.660 に答える