nginx重定向到错误的网站

时间:2018-08-28 08:38:43

标签: nginx nginx-location nginx-reverse-proxy

我配置了两个在nginx后面运行的节点应用程序。 nginx用作反向代理,一个域在其默认URL上重定向到错误的应用程序,

app1端口3000 域http://www.site1.comhttps://www.site1.com

app2端口3001 域http://www.site2.com

http://www.site1.comhttps://www.site1.com可以正常运行并投放app1

但是当http://www.site2.com重定向到https://www.site2.com并服务器app1

但是当请求http://www.site2.com/someurl时,它会处理app2

这是nginx的配置

site1

CNAME

site1 conf

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name www.site1.com;
        return 301 https://$server_name$request_uri;
}
server {

  listen 443 ssl;

  server_name www.site.com;
  ssl on;
  ssl_certificate /certificate.crt;
  ssl_certificate_key /psa.rsa;
  location / {
      proxy_pass http://localhost:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
      proxy_cookie_path / "/; HTTPOnly; Secure";
  }
}

默认配置

server {
        listen 80;
        server_name www.site2.com;
        location / {
                proxy_pass http://localhost:3001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection ‘upgrade’;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

1 个答案:

答案 0 :(得分:1)

您只有一个https服务器配置,它将代理对app1(端口3000)的所有请求,它将不会为您提供https上的app2。

通过检查请求的Host标头,有条件地

代理到app2或app1,例如:

server {
    listen 443 ssl;
    ...
    location / {
        if ($host = 'www.site1.com') {
            proxy_pass http://localhost:3000;
        }
        if ($host = 'www.site2.com') {
            proxy_pass http://localhost:3001;
        }
    }
}