Nginx

nginx http重定向到https無限循環

  • March 11, 2017

我有我的配置文件。這是一個舊的。

server {
     listen 80;

     server_name  .lala.mana.lan;

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

     proxy_conect_timeout  600;
     proxy_send_timeout    600;
     proxy_read_timeout    600;
     send_timeout          600;

     location / {
       proxy_pass    http://lala
       proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;

       proxy_redirect off;
   }

     location ~* /web/static/{
       proxy_cache_valid 200 60m;
       proxy_buffering    on;
       proxy_pass http://lala;
   }
   }

現在我正在嘗試從一個站點重定向到另一個站點,從 http 到 https。這是我編輯的配置文件:

server {
 listen 80;
 listen 443 ssl;

 ssl_certificate /etc/ssl/mana.crt;
 ssl_certificate_key /etc/ssl/mana.key;

 server_name  .lala.mana.lan;
 return 301
 https://lala.mana.lt$request_uri;

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

 proxy_conect_timeout  600;
 proxy_send_timeout    600;
 proxy_read_timeout    600;
 send_timeout          600;

 location / {
   proxy_pass    http://lala
   proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;

   proxy_redirect off;
}

 location ~* /web/static/{
   proxy_cache_valid 200 60m;
   proxy_buffering    on;
   proxy_pass http://lala;
}

client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
}

您需要為 http 和 https 設置單獨的塊,以便從一個重定向到另一個。

因此,您的配置應如下所示:

server {
   listen 80;
   server_name lala.man.lt;

   return 301 https://lala.man.lt$request_uri;
}

server {
   listen 443 ssl;
   server_name lala.man.lt;

   ssl_certificate /etc/ssl/mana.crt;
   ssl_certificate_key /etc/ssl/mana.key;

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

   proxy_conect_timeout  600;
   proxy_send_timeout    600;
   proxy_read_timeout    600;
   send_timeout          600;

   location / {
       proxy_pass    http://lala
       proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;

       proxy_redirect off;
   }

   location /web/static/ {
       proxy_cache_valid 200 60m;
       proxy_buffering    on;
       proxy_pass http://lala;
   }

   client_header_buffer_size 64k;
   large_client_header_buffers 4 64k;
}

我還修復了您的/web/static位置塊以使用簡單的前綴匹配,不需要涉及正則表達式匹配。

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