【问题标题】:How to setup proxy settings for a website in production如何为生产中的网站设置代理设置
【发布时间】:2017-09-08 18:37:35
【问题描述】:

我已经开发了一个 Angular 网站,并且在 proxy.conf.js 文件中有以下代理设置。

const proxyConfig = [
  {
    context: '/web/api/webclients/**',
    target: 'https://10.109.102.109',
    changeOrigin: true,
    secure: false
  },
  {
    context: '/restful/**',
    target: 'https://10.109.110.190',
    changeOrigin: true,
    secure: false
  },
  {
    context: '/api/**',
    target: 'http://10.109.105.107',
    changeOrigin: true,
    secure: false
  }
];

proxy.conf.js 在开发模式下按预期工作。

我在 package.json 文件中有这些用于启动和构建。

"build": "ng build --aot --prod",
"start": "ng serve --proxy-config proxy.conf.js -o",

在我运行“npm run build”并使用生成的文件在 IIS 8 上托管网站后,需要使用代理设置的页面不起作用。

比如我的请求https://localhost/web/api/webclients/authentication应该去https://10.109.102.109/web/api/webclients/authentication

如果我将此网站托管在 Windows 服务器上,如何在 IIS 中设置这些代理设置,或者如果我将其托管在非 Windows 服务器上,如何设置这些代理设置?

【问题讨论】:

    标签: angular proxy http-proxy-middleware


    【解决方案1】:

    我猜你现在已经得到了答案,但我还是会尝试回答这个问题,因为这是谷歌搜索“角度代理 iis”时的第一个结果。

    proxy.conf.js 文件仅在以开发模式为应用程序提供服务时使用。在为生产构建时,开发服务器不包含在输出中(因此 proxy.conf.js 也不是输出的一部分)。对于生产,您需要再次配置您的 Web 服务器(nginx、apache、IIS 等)以代理这些路径。

    对于 IIS,您需要先安装 ARR 模块,然后才能为后端设置代理规则。查看here 获取有关如何正确设置的详细教程。

    【讨论】:

      【解决方案2】:

      proxy.config 在 prod 模式下不起作用。参考https://stackoverflow.com/questions/48920937/proxy-config-json-is-not-working-in-angular-4-5-when-i-use-ng-build/49260885#49260885

      中间件 Web 服务器作为反向代理适用于您的场景。如果你不知道使用哪个 web 服务器,我推荐 Nginx

      您可以通过将其配置到您的 nginx 配置中来做代理后端。

      nginx.conf

      location /api/ {
        proxy_pass      http://127.0.0.1:8087; 
        #all incoming http request with /api/ will be forwarded to http://127.0.0.1:8087/api/
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        • 2011-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多