nginx http重定向到https无限循环

nginx http重定向到https无限循环

我有我的配置文件。这是一个旧的。

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;
}

答案1

您需要为 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位置块以使用简单的前缀匹配,不需要涉及正则表达式匹配。

相关内容