weixin_45907466 2023-03-06 17:35 采纳率: 57.1%
浏览 26
已结题

react打包后代理失效

在react中引入了setupProxy作为代理解决跨域问题,在本地可以做到了转发并访问外部接口,然后在把项目打包之后,放入nginx服务器上,再次请求外部接口,就会出现无法访问的情况,服务器上的报错是403。
PS:接口使用的人比较多,应该不会出现问题

img

img

  • 写回答

2条回答 默认 最新

  • 凉城┓.〆 2023-03-06 17:43
    关注

    在生产环境,proxy是无效的
    建议生产环境使用nginx解决跨域

    下边是ant-design-pro里边proxy的说明

    /**
     * 在生产环境 代理是无法生效的,所以这里没有生产环境的配置
     * The agent cannot take effect in the production environment
     * so there is no configuration of the production environment
     * For details, please see
     * https://pro.ant.design/docs/deploy
     */
    export default {
      dev: {
        '/api/': {
          target: 'https://preview.pro.ant.design',
          changeOrigin: true,
          pathRewrite: {
            '^': '',
          },
        },
      },
      test: {
        '/api/': {
          target: 'https://preview.pro.ant.design',
          changeOrigin: true,
          pathRewrite: {
            '^': '',
          },
        },
      },
      pre: {
        '/api/': {
          target: 'your pre url',
          changeOrigin: true,
          pathRewrite: {
            '^': '',
          },
        },
      },
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月6日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line