端口3001的Nginx反向代理

问题描述 投票:0回答:1

我有一个运行在端口3001上的Express服务器,该服务器为React应用提供服务。

假设我的域名为example.com;我想要达到的是:

  1. 呼叫https://example.net/api/getUsers的可能性

  2. 从带有端口的http://1.2.3.4:3001/重定向到https://example.net/

  3. 基本上将所有HTTP调用(无论是IP还是域)重定向到https://example.net/

有人可以帮助设置该Nginx配置吗?这是我目前在/ 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

}
node.js express nginx proxy reverse
1个回答
0
投票

看起来您的应用正在返回位置为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/;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.