URL 错误地重定向到托管在同一服务器 (nginx) 上的其他网站

URL 错误地重定向到托管在同一服务器 (nginx) 上的其他网站

我有一个运行 nginx 的 Ubuntu VM,我正在对它进行部分 SSL 配置。

我定义了一些用于开发的自定义本地域,存储在我的 hosts 文件中(在主机上,即 OSX):

192.168.50.50   foo.dev
192.168.50.50   www.foo.dev

192.168.50.50   bar.dev
192.168.50.50   www.bar.dev

每个都有自己的配置文件,都用于将所有流量重定向到非 www SSL 域。

foo.dev 的 nginx 配置如下:

server {
  listen 80;
  listen 443 ssl;

  server_name www.foo.dev;

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

  return 301 https://foo.dev$request_uri;
}
server {
  listen 80;

  server_name foo.dev;

  return 301 https://foo.dev$request_uri;
}
server {
  listen 443 ssl;

  server_name foo.dev;

  root /var/www/foo;

  ssl_certificate /etc/ssl/foo.crt;
  ssl_certificate_key /etc/ssl/foo.key;
}

这是 bar.dev 的 nginx 配置:

upstream bar_upstream {
  server 127.0.0.1:4000;
}
server {
  listen 80;
  listen 443 ssl;

  server_name www.bar.dev;

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

  return 301 https://bar.dev$request_uri;
}
server {
  listen 80;

  server_name bar.dev;

  return 301 https://bar.dev$request_uri;
}
server {
  listen 80;
  server_name bar.dev;
  root /var/www/bar/priv/static/;
  location / {
    proxy_pass http://bar_upstream;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
  }
  include /etc/nginx/h5bp/location/expires.conf;
  include /etc/nginx/h5bp/location/cross-domain-fonts.conf;
  include /etc/nginx/h5bp/location/protect-system-files.conf;
}

问题是,在我的浏览器中访问 bar.dev 会导致重定向到 foo.dev。这在所有浏览器中都会发生,我尝试过刷新缓存、重新启动 nginx 并进行了配置测试。

我仔细检查了这两个文件,查找拼写错误和不正确的引用。

我是 nginx 的新手,如果这是显而易见的事情,请原谅我?

答案1

我不确定这是否值得为其他人保留,但它最终是一个明显的错误,bar.dev没有它的listen 443 ssl;,它有两个listen 80;

因此,一个有效的 bar.dev 配置:

upstream bar_upstream {
  server 127.0.0.1:4000;
}
server {
  listen 80;
  listen 443 ssl;

  server_name www.bar.dev;

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

  return 301 https://bar.dev$request_uri;
}
server {
  listen 80;

  server_name bar.dev;

  return 301 https://bar.dev$request_uri;
}
server {
  listen 443 ssl;
  server_name bar.dev;
  root /var/www/bar/priv/static/;
  location / {
    proxy_pass https://bar_upstream;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
  }
  include /etc/nginx/h5bp/location/expires.conf;
  include /etc/nginx/h5bp/location/cross-domain-fonts.conf;
  include /etc/nginx/h5bp/location/protect-system-files.conf;
}

相关内容