我正在尝试将我的 React 应用程序从 nginx 上的自托管迁移到铁路。
在 nginx 上一切都工作得很好,但是我在使用 http-server 将其部署到铁路上时遇到了问题。
我使用的 Nginx 配置,一切都是 gucci:
location / {
root /var/www/html;
try_files $uri /index.html;
}
#Reverse proxy to express
location /api/ {
proxy_pass http://127.0.0.1:3001;
}
铁路上 我的构建命令设置为“
npm run build
”,启动命令“npx http-server ./build --proxy localhost:$PORT/? -p $PORT -a ::
”
根据 http-server 文档,-P 参数应该执行以下操作:
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
我的主网址按预期工作(https://client-product-70d6.up.railway.app/) - 即使您单击导航栏按钮。但是,如果您尝试直接从网址栏访问 https://client-product-70d6.up.railway.app/random,则请求会被卡住,直到超时(HTTP ERROR 404)
上一篇:https://gyazo.com/a78f41059137b9eae69b9998a60e3463
我尝试过调整 --proxy 设置(例如
--proxy localhost:$PORT/?
、--proxy localhost:$PORT?
--proxy localhost?
、--proxy localhost:$PORT/index.html?
,使用 -P 而不是 --proxy 等)
还尝试将 React 应用程序主页更改为
.
.*.
/
https://client-production-70d6.up.railway.app/
但这里没有运气。
我在这里缺少什么?任何帮助将不胜感激。
设法修复它!显然代理需要以 http:// facepalm
开头这个启动命令对我有用
npx http-server ./build --proxy http://localhost:$PORT/index.html? -p
$PORT -a ::
确保设置正确的主页。