Nginx

帶有 proxy_pass 和 $http + CloudFlare 的 nginx 重定向循環

  • July 17, 2018

在我添加強制 HTTPS 的重定向之前,我的 nginx 配置執行良好。重定向之前的工作配置是:

server {
 listen 80;
 listen 443 default_server ssl;
 server_name my-domain.com www.my-domain.com;
 client_max_body_size 5M;

 location / {
   proxy_pass http://localhost:3000;
 }
}

我正在伺服器的埠 3000 上執行一個應用程序,我想在我的域中的埠 80 和埠 443(http 和 https)上提供它。但是,當使用者嘗試通過 HTTP 訪問 HTTPS 時,我還想將他們重定向到 HTTPS。我用這個答案來設置它:

server {
 listen 80;
 server_name my-domain.com www.my-domain.com;
 client_max_body_size 5M;
 return 301 https://$server_name$request_uri;
}

server {
 listen 443 default_server ssl;
 server_name my-domain.com www.my-domain.com;
 client_max_body_size 5M;

 location / {
   proxy_pass http://localhost:3000;
 }
}

但這會導致重定向循環。我承認我對 nginx 比較陌生,所以如果有人能給我提供可以解釋這一點的資源,那將不勝感激。我的猜測是它與位置和/或 proxy_pass 的使用有關,但深入研究文件並沒有帶來任何更深入的見解。

另一件需要注意的是,這個應用程序是通過 CloudFlare 發送的。

這解決了。我堅持使用我原來的 nginx 配置並使用 CloudFlare 頁面規則來強制使用 HTTPS。

使用的規則是:

http://*gistbook.io/*=> 始終使用 HTTPS

引用自:https://serverfault.com/questions/648081