[英]How to configure Angular proxy on the Server prod?
I am testing a new aproach in my front end App build with Angular, and i want to hide the API url from the browser Network.我正在使用 Angular 在我的前端应用程序构建中测试一种新方法,并且我想从浏览器网络中隐藏 API url。 form example: to call login on api.url.dz/login i want to call front.url.dz/login on the front then redirecte this to api.url.dz/login.
form example: to call login on api.url.dz/login i want to call front.url.dz/login on the front then redirecte this to api.url.dz/login. here is my proxy.config.json file:
这是我的 proxy.config.json 文件:
{
"http://localhost:4200":{
"target":"http://localhost:9999/",
"secure":false,
"changeOrigin": true,
"logLevel":"debug",
"pathRewrite": {"^/authenticate": "authenticate",
"^/authenticate-refresh": "authenticate-refresh",
"^/refresh-token": "refresh-token",
"^/auth-logout": "auth-logout"
}
}
}
this works on localhost because i start project with this command:这适用于本地主机,因为我使用以下命令启动项目:
ng serve --proxy-config proxy.config.json
but in production server when executing:但在生产服务器中执行时:
ng build
it doesn't work它不起作用
this is the config in prod server:这是产品服务器中的配置:
{
"https://front.domaine.dz":{
"target":"https://back.domaine.dz:9999",
"secure":false,
"changeOrigin": true,
"logLevel":"debug",
"pathRewrite": {"^/authenticate": "authenticate",
"^/authenticate-refresh": "authenticate-refresh",
"^/refresh-token": "refresh-token",
"^/auth-logout": "auth-logout"
}
}
}
i want to know if there is an alternative to execute with proxy option in ng build command?我想知道在 ng build 命令中是否有使用代理选项执行的替代方法?
angular.json: angular.json:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"argon-dashboard-angular": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss",
"src/assets/scss/argon.scss"
],
"scripts": [
"node_modules/chart.js/dist/Chart.min.js",
"node_modules/clipboard/dist/clipboard.min.js"
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "argon-dashboard-angular:build"
},
"configurations": {
"production": {
"browserTarget": "argon-dashboard-angular:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "argon-dashboard-angular:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"argon-dashboard-angular-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "argon-dashboard-angular:serve"
},
"configurations": {
"production": {
"devServerTarget": "argon-dashboard-angular:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "argon-dashboard-angular",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"cli": {
"analytics": false
}
}
Angular proxy is not meant to be used in production. Angular 代理不适用于生产。
You can circumvent this by running ng serve
with your proxy configuration in production but that's bad practice.您可以通过在生产中使用代理配置运行
ng serve
来规避这种情况,但这是不好的做法。
You have to configure your web server (apache/nginx/other) to proxy requests according to your proxy.config.json
(as in, use the relevant directives to get the same behavior as your proxy.config.json
).您必须根据您的
proxy.config.json
配置您的 web 服务器(apache/nginx/other)以代理请求(如,使用相关指令获得与您的proxy.config.json
相同的行为)
For instance, you can use ProxyPass
and ProxyPassReverse
with Apache.例如,您可以将
ProxyPass
和ProxyPassReverse
与 Apache 一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.