更新角度代理配置 URL 而不重新启动应用程序

pra*_*jha 6 angular angular8

我的 Angular 应用程序中的 API 代理配置如下。

const proxyConfig = [
  {
    context: ['**', '!'],
    target: 'https://example.com',
    secure: false,
    changeOrigin: true
  }
];
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个无需重新启动应用程序即可更新目标 URL 的选项。想知道是否可能。

请告诉我是否有人有任何解决方案或解决方法?

Wel*_*lsh 0

我最终部署了一个 haproxy 容器,将其转发到我想要的位置,然后将我的代理配置指向该容器。

我的代理将指向http://localhost:4444我的容器命令是:

docker run -d --name haproxy -p 4444:80 -v /Path/To/Folder/hap-conf:/usr/local/etc/haproxy:ro --sysctl net.ipv4.ip_unprivileged_port_start=0 haproxy:2.3
Run Code Online (Sandbox Code Playgroud)

hap-conf这需要您在计算机上的某个位置创建一个文件夹,其中包含一个haproxy.cfg包含以下内容的文件:

global
    ssl-default-bind-options ssl-min-ver TLSv1.2

defaults
    mode http
    maxconn          10000
    timeout connect  5000
    timeout check    5000
    timeout client   300000
    timeout server   300000

frontend http-in
    bind *:80
    mode http
    log global
    log 127.0.0.1 local0
    option httplog

    default_backend main

backend main
    default-server inter 3s fall 3 rise 2
    server server-name server-name:8443 check ssl verify none

Run Code Online (Sandbox Code Playgroud)

然后我只需更改server-name以重新指向事物,如果我需要指向本地计算机上的某些内容,我只需使用如下行:

server localhost host.docker.internal:8443 check ssl verify none
Run Code Online (Sandbox Code Playgroud)

然后只需更新haproxy.cfg、重新启动容器并刷新浏览器即可。

不像 Angular 的热重载代理设置那么方便,但对于我自己来说,我必须开发一个需要 10 分钟才能启动的 Angular 应用程序,它节省了大量时间。