[英]nginx : how redirect server side application access to correct domain ( multiple domains on same vps )
I'm facing an issue on my nginx configuration:我的 nginx 配置遇到问题:
Context:语境:
Each web app is composed of:每个 web 应用程序由以下部分组成:
I can access to each backend if I specify the port, using any of the domain I have:如果我指定端口,我可以使用我拥有的任何域访问每个后端:
My question:我的问题:
How can I configure properly nginx to redirect all request to:5000 to https://domain1:5000 and all request on:5001 to https://domain2:5001 ?如何正确配置 nginx 以将所有请求重定向到:5000 到https://domain1:5000和所有请求:5001 到Z5E056C500A1C4B6A7110B50D80BADE:域? ( and doing the same for the other port)
(并对另一个端口做同样的事情)
My nginx conf我的 nginx 配置
( both apps have same config expect for the domain and the port ) (两个应用程序对域和端口具有相同的配置期望)
server {
server_name domain1.com www.domain1.com;
root /path/to/client/build
index index.html;
access_log /var/log/nginx/domain1.com.access.log;
error_log /var/log/nginx/domain1.com.error.log;
location / {
try_files $uri /index.html;
}
location /graphql {
proxy_pass https://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; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain1.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.domain1.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = domain1.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name domain1.com www.domain1.com;
listen 80;
return 404; # managed by Certbot
}
Thanks !谢谢 !
Perhaps something like this ( not tested - please experiment with the proxy_ssl_xxx
directives from the manual ):也许是这样的(未经测试- 请尝试使用手册中的
proxy_ssl_xxx
指令):
server {
server_name .domain1.com;
root /path/to/client/build
index index.html;
location / {
try_files $uri /index.html;
}
location /graphql {
proxy_pass https://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 http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain1.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 {
server_name .domain2.com;
root /path/to/client/build
index index.html;
location / {
try_files $uri /index.html;
}
location /graphql {
proxy_pass https://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;
}
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain2.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 {
server_name .domain1.com .domain2.com;
listen 80;
location / {
return 301 https://$host$request_uri;
}
}
server {
server_name .domain1.com;
listen 5000 ssl;
location / {
proxy_pass https://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;
}
}
server {
server_name .domain2.com;
listen 5001 ssl;
location / {
proxy_pass https://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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.