【发布时间】: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