[英]Nginx reverse proxy for port 3001
I have an express server running on port 3001 which serves a React app.我有一个在端口 3001 上运行的快速服务器,它为 React 应用程序提供服务。
Lets say that my domain name is example.com;假设我的域名是 example.com; What I am trying to achieve is:
我想要实现的是:
The possibility to call https://example.net/api/getUsers可以调用https://example.net/api/getUsers
Redirecting from http://1.2.3.4:3001/ with port to https://example.net/从http://1.2.3.4:3001/重定向到https://example.net/
Basically redirecting all HTTP calls (whether as IP or domain) to https://example.net/基本上将所有 HTTP 调用(无论是作为 IP 还是域)重定向到https://example.net/
Could anyone help with setting up that Nginx config?任何人都可以帮助设置 Nginx 配置吗? This is what I currently have under /etc/nginx/sites-available:
这是我目前在 /etc/nginx/sites-available 下的内容:
server {
server_name 1.2.3.4:3001;
return 301 https://example.net;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 default_server ssl;
listen [::]:443 default_server ssl;
server_name example.net www.example.net;
return 301 https://example.net$request_uri;
}
server {
listen 80;
server_name example.net www.example.net;
location / {
proxy_pass http://localhost:3001;
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/example.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.net/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
}
Looks like your app is returning redirect with Location: http://1.2.3.4:3001/ You can rewrite it with proxy_redirect and reduce redundant stuff.看起来您的应用正在返回重定向位置: http://1.2.3.4:3001/您可以使用 proxy_redirect 重写它并减少冗余内容。
server { listen 80 default_server; return 301 https://example.net$request_uri; } server { listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/example.net/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_name example.net www.example.net; location / { proxy_pass http://localhost:3001; 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; proxy_redirect http://1.2.3.4:3001/ $scheme://$host/; } }
Yes, you can add the following redirect:是的,您可以添加以下重定向:
server {
listen 1.2.3.4:3001;
return 301 https://example.net;
}
But note your react app.但请注意您的反应应用程序。 locally listens on localhost:3001
本地监听 localhost:3001
proxy_pass http://localhost:3001;
so ensure react app.所以确保反应应用程序。 is not listening on 1.2.3.4:3001 socket too.
也没有在 1.2.3.4:3001 套接字上监听。 Otherwise, you will get
Address already in use
error and nginx will fail to start.否则,您将收到
Address already in use
错误,nginx 将无法启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.