gpt4 book ai didi

reactjs - 如何为生产 react 应用程序设置代理?

转载 作者:行者123 更新时间:2023-12-05 01:44:53 25 4
gpt4 key购买 nike

我正在开发一个 React 应用程序,它使用在另一台服务器上运行的 REST API。现在,在开发中,我通过在 package.json 中添加代理字段来使用代理。

我想知道如何为生产应用程序配置代理,以便我的 REST 请求可以路由到其他服务器。有什么想法吗?

最佳答案

package.js 文件中的代理变量仅在开发中使用。我假设您正在使用 create-react-app,here is the doc .

我在我的 React 应用程序中路由所有 API 请求的方式是通过 Nginx 代理所有请求。当然,这实际上取决于您使用的网络服务器。

考虑到您正在向/API 端点发出所有请求,您可以像这样代理所有请求:

location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control public;
expires 1d;
}

location /api {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://yourapiserver:port/;
}

关于reactjs - 如何为生产 react 应用程序设置代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com