我在 Angular 6 上创建了一个项目并设置 proxy.conf.json,如下所示:
{
"/test-service": {
"target": "http://myhost:8080/test-service/",
"secure": true,
"pathRewrite": {
"^/test-service": ""
},
"logLevel": "debug",
"changeOrigin": true
}
}
在我的电脑上运行得非常顺利。但当我这样做时:
ng build --prod --optimization=false
并将构建的文件复制到服务器(apache2/var/www/html) 代理通行证不起作用 我尝试配置 apache 代理通行证:
ProxyPass /test-service/ http://localhost:8080/test-service/
ProxyPassReverse http://localhost:8080/test-service/ /test-service/
但还是没有运气。
我是否需要更改一些 apache 配置或以某种方式将 proxy.conf.json 包含到构建中?
请您参考如下方法:
proxy.conf.json 不包含在生产文件中,因此需要不同类型的设置。
两种方法可能是:
将新创建的生产文件包含在服务器“公共(public)”文件夹中,以便将它们用作服务器的 UI。
express :
app.use('/', express.static(path.join(__dirname, './path/to/client/build/files')));将前端 UI 与服务器分开提供服务,同时允许 CORS 专门指向客户端来源(例如:
localhost:4200或www.example.com)并确保 API 调用指向您构建的服务器。这可以通过environment动态完成根据服务模式提供不同值的变量(prod:true/false);当使用 build --prod 时,environment.prod.ts使用值 那么您需要在所有API调用字符串中包含environment.APIEndpoint(例如:this.http.get(environment.APIEndpoint + '/route/somewhere')
继续使用 proxy.conf.json 进行开发并为产品做好准备:
在开发模式下:environment.APIEndpoint = 'test-service'
在生产模式下:environment.APIEndpoint = 'https://www.example.com/api' (or https://api.example.com')

