我正在尝试在单独的端口上运行单个 React 应用程序,并使用从 nginx 到位置 '/app' 的代理传递
"http://localhost/app" -> React 应用程序
应用程序使用其生成的构建在 PM2 下运行,命令为:'pm2 serve build 3002'。
我的想法是我在本地进行所有这些测试,然后将其移植到我自己的域中。
我已经将“localhost”换成了“127.0.0.1”,以防与它有任何关系。
根据在线教程,我在我的 nginx“../sites-available/my_domain”文件中得到了以下内容
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /var/www/my_domain/html;
index index.html index.htm index.nginx-debian.html;
try_files $uri $uri/ =404;
}
location /app {
proxy_pass http://localhost:3002;
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;
}
}
如果我在 url 中输入“http://localhost:3002”它工作正常但是“http://localhost/app”不起作用并显示 404 错误。
旁注:“http://localhost”工作正常并指向存储在根目录下的 index.html。
在您的配置中,您在服务器地址之后缺少 / ,例如: 位置/应用{ proxy_pass http://localhost:3002/;