简体   繁体   English

如何在服务器产品上配置 Angular 代理?

[英]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.例如,您可以将ProxyPassProxyPassReverse与 Apache 一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Angular和NodeJS配置ngnix(代理服务器)? - How to configure ngnix (proxy server) with Angular and NodeJS? 如何在 Angular CLI 中配置代理 - How to configure a proxy in Angular CLI 如何在prod服务器上部署带有服务器端呈现的Angular 4应用程序 - How to deploy an Angular 4 app with server-side rendering on prod server 如何配置AppJS以与Node-http-proxy服务器一起使用 - How to configure AppJS to work with node-http-proxy server 如何以角度配置服务器以获取文件? - How to configure the server in angular to get files? 我应该如何配置nginx以在TCP服务器之前作为反向代理Websocket服务器运行? - How should I configure nginx to run as a reverse proxy websocket server in front of a tcp server? 如何在prod中正确地将请求代理到/ api / ressource? - how can I properly proxy requests to /api/ressource in prod? 如何配置 --public-host 以在 nginx 反向代理后面运行 angular 通用应用程序? - How to configure the --public-host to run angular universal app behind nginx reverse proxy? Angular,Express 和 http-proxy-middleware。 如何访问远程服务器? - Angular, Express & http-proxy-middleware. How to reach remote server? Angular 2,Node js Error npm run server:prod - Angular 2, Node js Error npm run server:prod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM