为什么 https://www.foobar.com 会重定向到其自身?

为什么 https://www.foobar.com 会重定向到其自身?

我没有任何规则建议https://www.foobar.com重定向到https://www.foobar.com。但是它为什么会这样做呢?

这是我的curl输出:

curl -Ik https://www.foobar.com
HTTP/1.1 301 Moved Permanently
Content-Length: 184
Content-Type: text/html
Date: Wed, 11 Feb 2015 07:22:11 GMT
Location: https://www.foobar.com/
Server: nginx/1.4.7
Connection: keep-alive

Nginx 配置:

upstream unicorn_www.foobar.com {
 server unix:/srv/www/foobar/shared/sockets/unicorn.sock fail_timeout=0;
}

server {
  listen 80;
  server_name foobar.com;
  return 301 https://www.foobar.com$request_uri;
}
server {
  listen 80;
  server_name www.foobar.com;
  return 301 https://www.foobar.com$request_uri;
}
server {
  listen 80;
  server_name beta.foobar.com;
  return 301 https://www.foobar.com$request_uri;
}
server {
  listen 443;
  server_name foobar.com;
  return 301 https://www.foobar.com$request_uri;

  ssl on;
  ssl_certificate /etc/nginx/ssl/www.foobar.com.crt;
  ssl_certificate_key /etc/nginx/ssl/www.foobar.com.key;
}
server {
  listen 443;
  server_name beta.foobar.com;
  return 301 https://www.foobar.com$request_uri;

  ssl on;
  ssl_certificate /etc/nginx/ssl/www.foobar.com.crt;
  ssl_certificate_key /etc/nginx/ssl/www.foobar.com.key;
}

server {
  listen   443;
  server_name www.foobar.com foobar_staging pantherinae;
  access_log /var/log/nginx/www.foobar.com-ssl.access.log;

  ssl on;
  ssl_certificate /etc/nginx/ssl/www.foobar.com.crt;
  ssl_certificate_key /etc/nginx/ssl/www.foobar.com.key;

  keepalive_timeout 5;

  root /srv/www/foobar/current/public/;


  location / {
    try_files $uri/index.html $uri/index.htm @unicorn;
  }

  location @unicorn {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_read_timeout 60;
    proxy_send_timeout 60;

    # If you don't find the filename in the static files
    # Then request it from the unicorn server
    if (!-f $request_filename) {
      proxy_pass http://unicorn_www.foobar.com;
      break;
    }
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /srv/www/foobar/current/public/;
  }
}

答案1

添加尾部斜杠是重定向:从www.example.comwww.example.com/
手动的

如果位置由以斜杠字符结尾的前缀字符串定义,并且请求由 proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass 或 memcached_pa​​ss 之一处理,则执行特殊处理。对于 URI 等于此字符串但不带尾部斜杠的请求,将返回带有代码 301 的永久重定向到附加了斜杠的请求 URI

我认为,proxy_pass to unicorn 是触发这一现象的原因。

您的配置中有一行location /。您使用 发出请求(请注意请求中curl -Ik https://www.foobar.com缺少尾部斜杠)。缺少斜杠会导致重定向到与位置指令匹配的“正确”URL 。/https://www.example.com/

相关内容