nginxredirect规则不能按预期工作(redirect到错误的站点)

我有3个站点,domain1.com,domain2.com和domain3.com。

domain1.com没有ssl

domain2.com只是一个redirect到domain1

domain3.com需要ssl

这是我的设置:

domain1.nginx

upstream domain1 { server localhost:3009; } server { listen 80; server_name www.domain1.com return 301 http://domain1.com$request_uri; } server { root /domain1/priv/static; listen 80; server_name domain1.com; location / { proxy_pass http://domain1; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } 

domain2.nginx

 server { server_name www.domain2.com domain2.com; return 301 http://domain1.com$request_uri; } 

domain3.nginx

 upstream domain3 { server localhost:3019; } server { listen 80; server_name www.domain3.com domain3.com; return 301 https://domain3.com$request_uri; } server { root /domain3/priv/static; listen 443 ssl; server_name domain3.com; ssl_certificate /etc/letsencrypt/live/domain3.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/domain3.com/privkey.pem; location / { proxy_pass http://domain3; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } 

究竟发生了什么:

domain1.com工作正常

www.domain2.com和domain2.comredirect到domain1.com完全没问题

www.domain1.comredirect到domain3.com时,它应该只是去domain1.com

当domain3.comredirect到domain3.com的ssl版本时,domain3.com会redirect到domain1.com

www.domain3.comredirect到domain3.com的ssl版本

我究竟做错了什么?

这里有一些略有不同的设置的要点,但有相同的问题: https : //gist.github.com/anonymous/6ba8865593c7c3e57f2035bafa3105b7