2016-10-17 6 views
0

私は単一のドメインのためにこれらのルールを実装しようとしていると私はそれをオンラインで行うための適切な方法を見つけるように見えることはできません。Nginxでプロキシとしてリダイレクトするwwwとwwwとhttpとを正しく処理する方法

  • ユーザーがHTTPをヒットした場合、場合https://www.domain.com
  • にリダイレクトユーザーがポートで特定のIPにhttps://www.domain.comハンドルプロキシをヒットした場合、ユーザーは

はこれまでのところ、これは私が持っているものであるWWW

  • にリダイレクトする、非WWWを打つ:

    server { 
         listen 80; 
         server_name domain.io www.domain.io; 
         return 301 https://www.domain.io$request_uri; 
    } 
    
    
    server { 
         listen 443 ssl; 
         server_name www.domain.io; 
    
         ssl_certificate /etc/letsencrypt/live/domain.io/fullchain.pem; 
         ssl_certificate_key /etc/letsencrypt/live/domain.io/privkey.pem; 
    
         #MORE SSL BLOCKS 
    
    
         #remnants, should I remove this? 
         root /var/www/html; 
         index index.html index.htm index.nginx-debian.html; 
         server_name _; 
    
         location/{ 
           proxy_pass http://IP_ADDR:3000; 
           proxy_http_version 1.1; 
           proxy_set_header Upgrade $http_upgrade; 
           proxy_set_header Connection 'upgrade'; 
           proxy_set_header Host $host; 
           proxy_cache_bypass $http_upgrade; 
         } 
    
    } 
    

    これを2つのサーバーブロックに追加しようとしました。

    server { 
         listen 443; 
         server_name domain.io; 
         return 301 https://www.domain.io$request_uri; 
    } 
    

    これはwww以外でSSLを破棄します。

    最初のブロックが動作しますが、それは私が考えた限りです。プロトコルを指定しないとhttpsにリダイレクトされますが、www以外のものはwww以外のものにリダイレクトされます。

    ネットワーキングは私の専門ではありませんので、間違った用語を教えてください。

  • +1

    「domain.io」にSSLが有効になっていません。 'domain.io'の証明書を持っていますか? –

    +0

    @リチャードスミスはい、私はSSLブロックがどこにあるかを宣言しました。それは、暗号化を使用しています。現在はhttpsとwwwの場合に動作します。 – Brian

    +0

    @リチャードスミス私はあなたが何を意味するかを見ます。リダイレクトする前にsslプロパティを追加するとうまくいくようです。それを考えなかった。 – Brian

    答えて

    0

    解決策が必要な人には、リチャード・スミスがコメントに提案したことを行うことで問題を解決しました。自分のサイトでLet's Encryptキーを使用したので、www以外のブロックのlisten 443のssl証明書ラインを複製するだけで済みます。

    server { 
         listen 80; 
         server_name domain.io www.domain.io; 
    
         return 301 https://www.domain.io$request_uri; 
    } 
    
    server { 
         listen 443; 
         server_name domain.io; 
    
         ssl_certificate /etc/letsencrypt/live/domain.io/fullchain.pem; 
         ssl_certificate_key /etc/letsencrypt/live/domain.io/privkey.pem; 
    
         #SSL_BLOCK 
    
         return 301 https://www.domain.io$request_uri; 
    } 
    
    server { 
         listen 443 ssl; 
         server_name www.domain.io; 
    
         ssl_certificate /etc/letsencrypt/live/domain.io/fullchain.pem; 
         ssl_certificate_key /etc/letsencrypt/live/domain.io/privkey.pem; 
    
         #SSL_BLOCK 
    
    
         root /var/www/html; 
    
         index index.html index.htm index.nginx-debian.html; 
    
         server_name _; 
    
         location/{ 
           proxy_pass http://IP_ADDR:PORT; 
           proxy_http_version 1.1; 
           proxy_set_header Upgrade $http_upgrade; 
           proxy_set_header Connection 'upgrade'; 
           proxy_set_header Host $host; 
           proxy_cache_bypass $http_upgrade; 
         } 
    
    } 
    
    関連する問題