如何为生产中的网站设置代理设置

时间:2017-09-08 18:37:36

标签: angular proxy http-proxy-middleware

我开发了一个角网站,并在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服务器上托管此网站,或者如果我将其托管在非Windows服务器上,如何设置这些代理设置,如何在IIS中设置这些代理设置?

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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/
}
相关问题