Viv*_*ams 4 nginx node.js nginx-reverse-proxy
我有一个私有 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
Run Code Online (Sandbox Code Playgroud)
现在我在站点可用/默认文件中有这种配置。
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
}
Run Code Online (Sandbox Code Playgroud)
现在,在 johndoe.com 上,来自端口 5000 的应用程序已托管并且工作正常。当我输入像 blog.johndoe.com 这样的子域时,它也可以在同一端口上工作。我想为此子域指定另一个端口,甚至提供静态页面。看起来无论我使用哪个子域,它总是使用默认的“/”位置。如何实现这一目标?
每个侦听端口/网络接口的可用服务器块之一始终充当默认服务器,捕获该端口/接口上的所有传入请求,无论 HTTPHost标头值如何。默认服务器可以使用指令default_server标志显式定义listen,否则它将是侦听该 IP/端口组合的第一个服务器块。阅读此文档页面以查找详细信息。
到目前为止,您在端口 80 上侦听的唯一服务器块充当服务任何 HTTP 请求的默认服务器块,无论它是否包含johndoe.com、blog.johndoe.com或statichtml.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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5228 次 |
| 最近记录: |