我有两个 node.js 实例在 nginx 代理后面运行,我还有一个重写规则将所有内容推送到 https。
我的问题是,我想拆分 dev.domain.tld 以转到第二个 node.js 实例,并将其余所有内容拆分为第一个 node.js 实例。
这是我的配置文件
server {
listen 80;
server_name domain.tld;
rewrite ^(.*) https://www.domain.tld$1 permanent;
}
server {
listen 443 ssl;
client_max_body_size 100M;
client_body_buffer_size 128k;
server_name www.domain.tld;
root /var/empty;
ssl_certificate /etc/ssl/certs/2015/bundle.crt;
ssl_certificate_key /etc/ssl/certs/2015/key.pem;
error_log /tmp/nginx-error.log info;
location ~ ^/(images|css|font|js) {
root c;
expires 365d;
add_header Cache-Control public;
gzip on;
gzip_vary on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
server {
listen 443 ssl;
client_max_body_size 100M;
client_body_buffer_size 128k;
server_name dev.domain.tld;
root /var/empty;
ssl_certificate /etc/ssl/certs/2015/bundle.crt;
ssl_certificate_key /etc/ssl/certs/2015/key.pem;
error_log /tmp/nginx-error.log info;
location ~ ^/(images|css|font|js) {
root /var/www/domain.tld/public;
expires 365d;
add_header Cache-Control public;
gzip on;
gzip_vary on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
我研究了使用正则表达式如何让 dev.*.domain.com 在 nginx 上运行问题/答案,但我不确定在哪里添加规则?
另外,我想重新使用该location ~ ^/(images|css|font|js) { ...
块 - 这可能吗?
任何建议都非常感谢
答案1
为了将 HTTP 重定向到 HTTPS,我使用以下形式:
server {
listen 80;
server_name sub.domain.tld;
return 301 https://$server_name$request_uri;
}
要使实例默认,请使用default_server
关键字。即:
server {
listen 443 default_server ssl;
server_name domain.tld;
}
希望这能帮助您实现您需要的目标。