【问题标题】:Too many redirect nginx and varnish太多重定向 nginx 和清漆
【发布时间】:2018-10-25 11:00:37
【问题描述】:

我正在使用 varnish 缓存服务器和 nginx。我试图从http重定向到https。我在 varnish 服务器中编写了将 http 重定向到 https 的配置。

default.vcl

 sub vcl_recv {
    if (client.ip != "127.0.0.1" && req.http.host ~ "groundforce.cloud") {
      set req.http.x-redir = "https://groundforce.cloud" + req.url;
      return(synth(850, ""));
    }
   }
sub vcl_synth {
 if (resp.status == 850) {
     set resp.http.Location = req.http.x-redir;
     set resp.status = 302;
     return (deliver);
 }
}

我的nginx配置文件:

server {
   listen  443 ssl;
   listen  [::]:443 ssl;
   server_name  my_server;
   port_in_redirect off;

   ssl                  on;
   ssl_certificate      /etc/ssl/my_server.crt;
   ssl_certificate_key  /etc/ssl/my_server.key;

   ssl_session_cache   shared:SSL:20m;
   ssl_session_timeout 60m;

   add_header Strict-Transport-Security "max-age=31536000";
   add_header X-Content-Type-Options nosniff;

   location / {
     proxy_pass http://127.0.0.1:80;
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto https;
     proxy_set_header HTTPS "on";
     }
}

server {
   listen 8080;
   listen [::]:8080;
   server_name  my_server;
   root /var/www/html;
   index index.php;
   port_in_redirect off;

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

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       include fastcgi_params;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS on;
       fastcgi_pass unix:/var/run/php7.0-fpm.sock;
   }
}

注意:我删除了上面的 default.vcl 代码,然后 http 和 https 都可以正常工作。我关注了以下article

【问题讨论】:

    标签: nginx https varnish


    【解决方案1】:

    相关信息:https://info.varnish-software.com/blog/rewriting-urls-with-varnish-redirection(即,可以使用PROXY协议,带上支持吧。

    这看起来像一个无限循环,可能是因为来自 nginx 的请求并不完全来自 127.0.0.1,所以 client.ip != "127.0.0.1" 是真的。

    尝试查看varnishlog 以了解client.ip 的实际含义,或者将其添加到vcl_synth 的响应标头中,并通过curl -v https://groundforce.cloud/ 检查它的值(当然,也可以使用任何您喜欢的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2013-03-13
      • 1970-01-01
      • 2021-11-28
      • 2016-06-26
      • 2022-11-18
      • 1970-01-01
      相关资源
      最近更新 更多