我如何使用 NGINX 反向代理到本地反应应用程序

How do i use NGINX to reverse proxy to local react app

我正在尝试使用 nginx 对我的本地 React 应用程序做一个简单的反向代理。我无法理解这是如何工作的。我需要 location /test 中的根变量还是别名?因为 nginx 正在寻找错误的地址。 (我 运行 我的 React 应用在本地 localhost:3001)

已经尝试在“location /test”-block

中使用 rewrite /test(.*) / break

这是我的 nginx.conf:

server {
    listen 81 ;
    server_name app1.localhost;

    location / {  
        root   html;
        index  index.html index.htm;
    }
    location /test {
        proxy_pass   http://localhost:3001;
    }
}

这是我尝试输入 app1.localhost:81/test 时的控制台日志:

只需使用两个 server 块:

server {
    listen 81 default_server;

    location / {  
        root html;
        index index.html index.htm;
    }
}

server {
    listen 81;
    server_name app1.localhost;

    location / {  
        proxy_pass http://localhost:3001;
    }
}