【问题标题】:Nginx multiple node apps with multiple subdomainsNginx 具有多个子域的多节点应用程序
【发布时间】:2021-03-05 09:12:36
【问题描述】:

我有一个私有 VPS,并希望使用 nginx 基于子域托管多个节点应用程序(或静态网站)。

我想实现这样的目标:

johndoe.com -> node app 1 (port 5000)
blog.johndoe.com -> node app 2 (port 5001)
statichtml.johndoe.com -> static html from defined path

现在我在 sites-available/default 文件中有这种配置。

server {
    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;
    server_name www.johndoe.com johndoe.com; # managed by Certbot


    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://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 ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/johndoe.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/johndoe.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.johndoe.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


    listen 80 ;
    listen [::]:80 ;
    server_name www.johndoe.com johndoe.com;
    return 404; # managed by Certbot
}

现在在 johndoe.com 上,来自 port:5000 的应用程序已托管并且运行正常。当我进入 blog.johndoe.com 之类的子域时,它也可以在同一个端口上工作。我想为这个子域指定另一个端口,甚至提供静态页面。看起来无论我使用什么子域,它总是使用默认的“/”位置。如何做到这一点?

【问题讨论】:

  • 您的证书是通配符证书(为johndoe.com*.johndoe.com 颁发)还是要为您的子域使用不同的证书?
  • 目前子域根本不使用证书。即使没有证书,我也只想在该子域上托管另一个应用程序。

标签: node.js nginx nginx-reverse-proxy


【解决方案1】:

每个侦听端口/网络接口的可用服务器块之一始终充当默认服务器,捕获该端口/接口上的所有传入请求,无论 HTTP Host 标头值如何。默认服务器可以使用listen 指令的default_server 标志显式定义,否则它将是侦听该IP/端口组合的第一个服务器块。阅读this 文档页面以查找详细信息。

到目前为止,您唯一侦听端口 80 的服务器块充当服务任何 HTTP 请求的默认服务器块,无论它是否包含 johndoe.comblog.johndoe.comstatichtml.johndoe.com 或任何其他 Host 标头(或者如果它完全包含Host 标头)。这是您可以用于特定示例的配置:

# server blocks for incoming HTTP requests
server {
    # server block for 'johndoe.com', 'www.johndoe.com' domains
    listen 80;
    listen [::]:80;
    server_name johndoe.com www.johndoe.com;
    # redirect any HTTP request to HTTPS
    return 301 https://$http_host$request_uri;
}
server {
    # server block for 'blog.johndoe.com' domain
    listen 80;
    listen [::]:80;
    server_name blog.johndoe.com;

    location / {
        proxy_pass http://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;
    }
}
server {
    # server block for 'statichtml.johndoe.com' domain
    listen 80;
    listen [::]:80;
    server_name statichtml.johndoe.com;
    root /your/root/path;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
}
server {
    # server block for all the other requests
    # this block will be a default server block listening on port 80
    listen 80 default_server;
    listen [::]:80 default_server;
    # close the connection immediately
    return 444;
}

# server blocks for incoming HTTPS requests
server {
    # server block for 'johndoe.com', 'www.johndoe.com' domains
    listen [::]:443 ssl;
    listen 443 ssl;
    server_name johndoe.com www.johndoe.com;

    # SSL configuration by certbot
    ssl_certificate /etc/letsencrypt/live/johndoe.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/johndoe.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

    location / {
        proxy_pass http://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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2021-10-01
    • 2017-01-10
    • 2021-04-05
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多