2017-01-27 10 views
0

私は、それぞれが5つのサイトを実行する2つの仮想ホスト(マルチテナント)を持っています。サーバーには3つのIPアドレスがあります。 2人は公表され、もう1人は社内である。ロードバランサを追加したときにDjangoマルチテナントサイトのリダイレクトループ

公開されているWebサイトにはいずれもSSL証明書があります。 1つのサイトは私のステージングサイトで、暗号化されたSSL証明書を持ち、もう1つはライブサイトであり、godaddy SSL証明書を持っています。

1つのノード(クラウドインスタンス)を持つRackspaceロードバランサを最初に設定し、証明書とキーをサーバーからコピーし、次のnginx設定を使用して、ロードバランサを使用して自分のサイトを余談として内部に向けIP

upstream django { 
    server unix:///run/uwsgi/app/introtest/socket; 
} 

# configuration of the server, first redirect http to https... 
server { 
    listen  10.181.104.195:80; 
    if ($http_x_forwarded_proto = "http") { 
     return 302 https://$http_host$request_uri; 
    } 

    # the domain name it will serve for 
    charset  utf-8; 

    # max upload size 
    client_max_body_size 75M; # adjust to taste 

    # Django media 
    location /media { 
     alias /srv/test/media; 
    } 

    location /static { 
     alias /srv/test/static; 
    } 


    # Finally, send all non-media requests to the Django server. 
    location/{ 
     if (-f /srv/maintenance_test.html) { 
      return 503; 
     } 
     uwsgi_pass django; 

     uwsgi_param QUERY_STRING  $query_string; 
     uwsgi_param REQUEST_METHOD  $request_method; 
     uwsgi_param CONTENT_TYPE  $content_type; 
     uwsgi_param CONTENT_LENGTH  $content_length; 

     uwsgi_param REQUEST_URI  $request_uri; 
     uwsgi_param PATH_INFO   $document_uri; 
     uwsgi_param DOCUMENT_ROOT  $document_root; 
     uwsgi_param SERVER_PROTOCOL $server_protocol; 
     uwsgi_param REQUEST_SCHEME  $scheme; 
     uwsgi_param HTTPS    $https if_not_empty; 

     uwsgi_param REMOTE_ADDR  $remote_addr; 
     uwsgi_param REMOTE_PORT  $remote_port; 
     uwsgi_param SERVER_PORT  $server_port; 
     uwsgi_param SERVER_NAME  $server_name; 
     uwsgi_param X-Real-IP   $remote_addr; 
     uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for; 
     uwsgi_param X-Forwarded-Host $server_name; 

    } 

    # Error pages 
    error_page 503 /maintenance_test.html; 
    location = /maintenance_test.html { 
     root /srv; 
    } 

} 

を務める私のWebサーバー、私はそれを助けることができれば永久的なリダイレクトを使用しないと、ステージングサーバーではなかったん。ライブサーバーは既に設定されており、httpsへの永続的なリダイレクトを行っていましたが、ライブサイトを常にSSLにリダイレクトしたいので、リダイレクトは301になります。

私のルートドメインロードバランサにサイトをステージングすると、これは正常に機能しました。

tail -f /var/log/nginx/access.log 

は、ロードバランサの内部IPアドレスから要求が送信され、ページが正しく配信されていることを示しています。

私はすべてを元に戻しました(つまり、nginx confとステージングルートドメインのDNSエントリです)、ウェブサーバーから提供される準備が整っています。次に、godaddy SSL証明書情報をロードバランサにコピーしました。そして、ライブサーバーに対して次のnginxの構成で:

upstream intro_live { 
    server unix:///run/uwsgi/app/introsites/socket; 
} 

server { 
    listen <SERVER PUBLIC IP>:80; 
    listen <SERVER PUBLIC IP>:443; 

    location/{ 
     return 503; 
    } 
    error_page 503 /interruption.html; 
    location = /interruption.html { 
     root /srv; 
    } 
} 


# configuration of the server 
server { 
    # the port your site will be served on 
    listen 10.181.104.195:80; 
    # reidrect http to https from load balancer 
    if ($http_x_forwarded_proto = "http") { 
     set $http_test S${http_host}; 
    } 

    if ($http_test = 'Sintrotravel.com') { 
     rewrite^https://www.introtravel.com$request_uri; 
    } 
    if ($http_test = 'Sozintro.com') { 
     rewrite^https://www.ozintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Sbalintro.com') { 
     rewrite^https://www.balintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Sthaintro.com') { 
     rewrite^https://www.thaintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Svietnamintro.com') { 
     rewrite^https://www.vietnamintro.com$request_uri permanent; 
    } 

    charset  utf-8; 

    # max upload size 
    client_max_body_size 75M; # adjust to taste 

    # Django media 
    location /media { 
     alias /srv/intro/media; 
     expires 7d; 
     add_header Pragma public; 
     add_header Cache-Control "public"; 
    } 

    location /static { 
     alias /srv/intro/static; 
     expires 1d; 
     add_header Pragma public; 
     add_header Cache-Control "public"; 
    } 


    # Finally, send all non-media requests to the Django server. 
    location/{ 
     if (-f /srv/maintenance_on.html) { 
      return 503; 
     } 
     uwsgi_pass intro_live; 

     uwsgi_param QUERY_STRING  $query_string; 
     uwsgi_param REQUEST_METHOD  $request_method; 
     uwsgi_param CONTENT_TYPE  $content_type; 
     uwsgi_param CONTENT_LENGTH  $content_length; 

     uwsgi_param REQUEST_URI  $request_uri; 
     uwsgi_param PATH_INFO   $document_uri; 
     uwsgi_param DOCUMENT_ROOT  $document_root; 
     uwsgi_param SERVER_PROTOCOL $server_protocol; 
     uwsgi_param REQUEST_SCHEME  $scheme; 
     uwsgi_param HTTPS    $https if_not_empty; 

     uwsgi_param REMOTE_ADDR  $remote_addr; 
     uwsgi_param REMOTE_PORT  $remote_port; 
     uwsgi_param SERVER_PORT  $server_port; 
     uwsgi_param SERVER_NAME  $server_name; 
     uwsgi_param X-Real-IP   $remote_addr; 
     uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for; 
     uwsgi_param X-Forwarded-Host $server_name; 

    } 

    # Error pages 
    error_page 503 /maintenance_on.html; 
    location = /maintenance_on.html { 
     root /srv; 
    } 
} 
  • 最初upstream djangoポイント私のuwsgi設定します。
  • 2番目のサーバーの設定は、サーバーのパブリックアドレスにリッスンします。そのため、ユーザーのWebサイトのDNSエントリが更新されていない場合、サーバーがメンテナンス中であることを示す静的ページが表示されます。
  • 第3のサーバー構成は、内部アドレスのポート80をリッスンし、ステージングサーバーがhttp_x_forwarded_protoに設定されているかどうかを確認し、ドメインにテスト変数を設定しているかどうかを確認します。私は特に、5つのサイトからのhttpトラフィックをhttpsにリダイレクトします。
  • 最後の503件のファイルはファイルの存在を検出し、存在する場合は保守モードになります。

あり/etc/nginx/conf.d/には何もありませんし、私の/etc/nginx/nginx.confは次のようになります。私は5のためのDNSのAレコードを設定すると

user www-data; 
worker_processes 20; 
pid /run/nginx.pid; 

events { 
     worker_connections 768; 
     # multi_accept on; 
} 

http { 

     ## 
     # Basic Settings 
     ## 

     sendfile on; 
     tcp_nopush on; 
     tcp_nodelay on; 

     keepalive_timeout 65; 
     types_hash_max_size 2048; 

     # server_tokens off; 
     # server_names_hash_bucket_size 64; 
     # server_name_in_redirect off; 

     include /etc/nginx/mime.types; 
     default_type application/octet-stream; 

     ## 
     # SSL Settings 
     ## 

     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 
     ssl_prefer_server_ciphers on; 

     ## 
     # Logging Settings 
     ## 

     access_log /var/log/nginx/access.log; 
     error_log /var/log/nginx/error.log; 

     ## 
     # Gzip Settings 
     ## 

     gzip on; 
     gzip_disable "msie6"; 

     gzip_vary on; 
     gzip_proxied any; 
     gzip_comp_level 6; 
     gzip_buffers 16 8k; 
     gzip_http_version 1.1; 
     gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 

     ## 
     # Virtual Host Configs 
     ## 

     include /etc/nginx/conf.d/*.conf; 
     include /etc/nginx/sites-enabled/*; 
} 

サイトがロードバランサを指すようにするには、httpsへのリダイレクトは問題ありませんが、各ページでリダイレクトループが発生します。ですから、設定からすべてのリダイレクトを取り出しました。つまり、

if ($http_test = 'Sintrotravel.com') { 
     rewrite^https://www.introtravel.com$request_uri; 
    } 
    if ($http_test = 'Sozintro.com') { 
     rewrite^https://www.ozintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Sbalintro.com') { 
     rewrite^https://www.balintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Sthaintro.com') { 
     rewrite^https://www.thaintro.com$request_uri permanent; 
    } 
    if ($http_test = 'Svietnamintro.com') { 
     rewrite^https://www.vietnamintro.com$request_uri permanent; 
    } 

とnginxを再起動しました。私はまだリダイレクトループを持っていました。なぜ???私は書き換えが永久的だと理解していますが、これはreturn 302rewriteの違いではありません。すべてリダイレクトを取ると、サーバーはまだ無限リダイレクトループになっています。私は明らかな理由のためにライブサイトを実験するのに多くの時間を費やすことはできません。本当に私は10-15分で横断する必要があります誰でも助言がありますか?

答えて

0

これについて考えてみると、それは問題であったDjangoアプリケーションでなければならないことに気付きました。 nginxとは関係ありません。

SECURE_SSL_REDIRECT = True 

これはdjangoをhttpsにリダイレクトする(ステージングではfalse)。これはロードバランサがhttpsトラフィックを受信するためロードバランサを追加すると問題になりますが、ポート80のセキュリティで保護されていないトラフィックはWebサーバー/アプリケーションにのみ配信されます。このため、nginx設定で、ロードバランサによって設定されている

if ($http_x_forwarded_proto = "http") {... 

がリダイレクトされています。このようにリダイレクトをnginxから削除するだけでは、問題は解決されませんでした。私はdjangoから完全に削除する必要がありました。

分散した性質のために急いでいる場合や、お金を稼ぐサイトがダウンしている場合、DNSがどれほど経験を積んでいても混乱します。あなたは急いでいます。

0

Djangoはこの経由していることに対処することができます SECURE_PROXY_SSL_HEADER =( 'HTTP_X_FORWARDED_PROTO'、 'HTTPS')

をそして必ず、あなたのプロキシが適切なヘッダを防ぐことができます設定すること:

https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-SECURE_PROXY_SSL_HEADER

追加リダイレクトループ。何が起こるのは、あなたのプロキシが接続が安全であることをDjangoに伝えていないので、すでに安全ですが、安全なバージョンにリダイレクトされます。ヘッダーを提供することは、これに対処する方法の1つです。

+0

このリンクは質問に答えるかもしれませんが、回答の重要な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 - [レビューから](/レビュー/低品質の投稿/ 17677422) –

関連する問題