2017-09-25 9 views
0

私のhttpトラフィックをすべてhttpsにリダイレクトするようにリダイレクトしたいと思います。私はletsencryptを使用しています。私はreturn 301 https://$server_name$request_uri;が私のウェブサイトへのすべてのトラフィックをhttpsにリダイレクトするが、代わりにERR_TOO_MANY_REDIRECTSという結果になることをオンラインで読んだ。nginxでERR_TOO_MANY_REDIRECTS

上記の記述がなくても問題なく動作しますが、具体的にはURLにhttpsと指定する必要があります。ここに私の/etc/nginx/sites-available/defaultファイルがあります:

server { 
     listen 80 default_server; 
     listen 443 ssl default_server; 

     ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem; 
     ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem; 

     root /home/website/mywebsite/public; 

     index index.html index.htm index.php; 

     server_name mywebsite.me www.mywebsite.me; 

     return 301 https://$server_name$request_uri; 

     location/{ 
       try_files $uri $uri/ /index.php$is_args$args; 
     } 

     location ~ \.php$ { 
       include snippets/fastcgi-php.conf; 
       fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; 
     } 
} 

私は間違っていますか?

答えて

3
あなたの現在の設定がhttpsにHTTPとHTTPSの両方にリダイレクト

server { 
     listen 80 default_server; 
     server_name mywebsite.me www.mywebsite.me; 

     return 301 https://$server_name$request_uri; 
} 

server { 

     listen 443 ssl default_server; 

     ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem; 
     ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem; 

     root /home/website/mywebsite/public; 

     index index.html index.htm index.php; 

     server_name mywebsite.me www.mywebsite.me; 

     location/{ 
       try_files $uri $uri/ /index.php$is_args$args; 
     } 

     location ~ \.php$ { 
       include snippets/fastcgi-php.conf; 
       fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; 
     } 
} 

の下に自分の設定を変更し

。したがって、return文のために無限ループになります。接続がhttpである場合のみreturn文が必要です。したがって、2つのサーバーブロックに分割します。

0

nginxプロキシと同じ状況です。私はproxy_pass http://またはhttps://

server { 
    listen 80; 
    listen [::]:80; 

    root /var/www/tomato.mysite.com/html; 
    index index.html index.htm index.nginx-debian.html; 

    server_name tomato.mysite.com; 

    location/{ 
     proxy_pass http://tomato.mysite.com:4000/; # meteor server at port 4000 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
     proxy_set_header Host $http_host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forward-Proto http; 
     proxy_set_header X-Nginx-Proxy true; 
     proxy_redirect off; 
    } 

    listen 443 ssl; 
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; 
    include /etc/letsencrypt/options-ssl-nginx.conf; 
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 

    if ($scheme != "https") { 
     return 301 https://$host$request_uri; 
    } # managed by Certbot 
} 
+0

と混同しています。最初の解決策に示すように、httpとhttpsを別々のブロックに入れてください。それは私のために働いた。 –

+0

@PritamBohraは動作しません。ご覧のとおり、別のポートと同じサーバー名のプロキシが必要です。私の設定は、sslが強制されていない背後のアプリを強制したときにうまく機能します'http:4000'が動作するとき、 'https:'は正常に動作します。そうでなければ、 'http:4000'自身が 'https:'にリダイレクトされると、設定は機能しません。これは私が多く議論したMeteorアプリケーションの一般的な問題です。おそらく、nginxだけでなく、sslを使用するように流星アプリケーションを設定する必要があります。 – sitexa

関連する問題