具有多个子域的 Nginx 多节点应用程序
Nginx multiple node apps with multiple subdomains
我有一个私人 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 这样的子域时,它也在同一个端口工作。我想为此子域指定另一个端口,甚至提供静态页面。看起来无论我使用哪个子域,它总是使用默认的“/”位置。如何实现?
每个侦听 port/network 接口的可用服务器块之一始终充当默认服务器,捕获该 port/interface 上的所有传入请求,无论 HTTP Host
header 值。默认服务器可以使用 listen
指令的 default_server
标志显式定义,否则它将是第一个监听 IP/port 组合的服务器块。阅读 this 文档页面以查找详细信息。
截至目前,您在端口 80 上侦听的唯一服务器块充当服务任何 HTTP 请求的默认服务器块,无论它包含 johndoe.com
、blog.johndoe.com
、statichtml.johndoe.com
还是任何other Host
header(或者如果它包含 Host
header)。这是您可以用于特定示例的配置:
# 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;
}
}
我有一个私人 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 这样的子域时,它也在同一个端口工作。我想为此子域指定另一个端口,甚至提供静态页面。看起来无论我使用哪个子域,它总是使用默认的“/”位置。如何实现?
每个侦听 port/network 接口的可用服务器块之一始终充当默认服务器,捕获该 port/interface 上的所有传入请求,无论 HTTP Host
header 值。默认服务器可以使用 listen
指令的 default_server
标志显式定义,否则它将是第一个监听 IP/port 组合的服务器块。阅读 this 文档页面以查找详细信息。
截至目前,您在端口 80 上侦听的唯一服务器块充当服务任何 HTTP 请求的默认服务器块,无论它包含 johndoe.com
、blog.johndoe.com
、statichtml.johndoe.com
还是任何other Host
header(或者如果它包含 Host
header)。这是您可以用于特定示例的配置:
# 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;
}
}