简体   繁体   English

nginx:如何将服务器端应用程序访问重定向到正确的域(同一个 vps 上的多个域)

[英]nginx : how redirect server side application access to correct domain ( multiple domains on same vps )

I'm facing an issue on my nginx configuration:我的 nginx 配置遇到问题:


Context:语境:

  • I have a VPS, and multiple domains on it.我有一个 VPS,上面有多个域。
  • I host 2 web applications on it.我在上面托管了2 个web 应用程序。
  • I use let's encrypt through certbot to handle https.我使用 let's encrypt through certbot 来处理 https。

Each web app is composed of:每个 web 应用程序由以下部分组成:

  • a static build for front-end served on one domain ( http redirects to https )一个 static 构建用于在一个域上服务的前端( http 重定向到 https )
  • a https served node.js backend ( port:5000/graphql for app1 and:5001/graphql for app2 )一个 https 服务于 node.js 后端(port:5000/graphql for app1 and:5001/graphql for app2)

I can access to each backend if I specify the port, using any of the domain I have:如果我指定端口,我可以使用我拥有的任何域访问每个后端:

  • https//domain1:5000/graphql is ok (:5000 is for the backend of app1 served on domain1 ) https//domain1:5000/graphql 可以(:5000 用于在 domain1 上提供的 app1 的后端)
  • https//domain2:5000/graphql is also ok ( complaining not secure, since:5000 is for app1 ) https//domain2:5000/graphql 也可以(抱怨不安全,因为:5000 用于 app1)

My question:我的问题:

How can I configure properly nginx to redirect all request to:5000 to https://domain1:5000 and all request on:5001 to https://domain2:5001 ?如何正确配置 nginx 以将所有请求重定向到:5000 到https://domain1:5000和所有请求:5001 到Z5E056C500A1C4B6A7110B50D80BADE:域 ( and doing the same for the other port) (并对另一个端口做同样的事情)


My nginx conf我的 nginx 配置

( both apps have same config expect for the domain and the port ) (两个应用程序对域和端口具有相同的配置期望)

server {
        server_name domain1.com www.domain1.com;
        root /path/to/client/build
        index index.html;

        access_log /var/log/nginx/domain1.com.access.log;
        error_log /var/log/nginx/domain1.com.error.log;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost:5000;
                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;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        server_name domain1.com www.domain1.com;
    listen 80;
    return 404; # managed by Certbot

}

Thanks !谢谢 !

Perhaps something like this ( not tested - please experiment with the proxy_ssl_xxx directives from the manual ):也许是这样的(未经测试- 请尝试使用手册中的proxy_ssl_xxx指令):

server {
        server_name .domain1.com;
        root /path/to/client/build
        index index.html;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost:5000;
                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;
        }


    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
        server_name .domain2.com;
        root /path/to/client/build
        index index.html;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost:5001;
                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;
        }


    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain2.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    server_name .domain1.com .domain2.com;
    listen 80;
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
  server_name .domain1.com;
  listen 5000 ssl;

  location / {
                proxy_pass https://localhost:5000/;
                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;
  }
}

server {
  server_name .domain2.com;
  listen 5001 ssl;

  location / {
                proxy_pass https://localhost:5001/;
                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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM