在Nginx上以及其他Nginx反向代理后面运行的Angular App [英] Angular App running on nginx and behind an additional nginx reverse proxy

查看:225
本文介绍了在Nginx上以及其他Nginx反向代理后面运行的Angular App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为两个Angular应用创建反向代理. 我希望应用程序都可以通过启用SSL的docker主机的443端口访问(例如 https://192.168.xx /app1 https://192.168.xx/app2 ),这样用户就不会不必键入每个应用程序的端口号.

I'm currently trying to create a reverse proxy for two Angular apps. I want the apps to be both accessible through the 443 port of the docker host with SSL enabled (like https://192.168.x.x/app1 and https://192.168.x.x/app2), so that the users don't have to type in the port numbers for each app.

我的设置是,应用程序的每个部分都在其自己的Docker容器中运行: -容器1:Angular App 1(端口80暴露给端口8080上的主机) -容器2:Angular App 2(端口80暴露给端口8081上的主机) -容器3:反向代理(端口443暴露)

My setting is, that every part of the application runs within its own Docker container: - Container 1: Angular App 1 (Port 80 exposed to host on port 8080) - Container 2: Angular App 2 (Port 80 exposed to host on port Port 8081) - Container 3: Reverse Proxy (Port 443 exposed)

Angular应用程序和反向代理都在nginx上运行.这些应用程序的构建方式如下:ng build --prod --base-href /app1/ --deploy-url /app1/

Both Angular apps and the reverse proxy are running on nginx. The apps are build like that: ng build --prod --base-href /app1/ --deploy-url /app1/

应用程序的nginx设置如下:

The nginx setting of the apps is like that:

server {
  listen 80;
  sendfile on;

  default_type application/octet-stream;

  gzip on;
  gzip_http_version 1.1;
  gzip_disable      "MSIE [1-6]\.";
  gzip_min_length   256;
  gzip_vary         on;
  gzip_proxied      expired no-cache no-store private auth;
  gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_comp_level   9;

  root /usr/share/nginx/html;

  index index.html index.htm;

  location / {
    try_files $uri $uri/ /index.html =404;
  }
}

反向代理的nginx配置如下:

The nginx configuration of the reverse proxy is like that:

server {
  listen 443;
  ssl on;
  ssl_certificate /etc/nginx/certs/domaincertificate.cer;
  ssl_certificate_key /etc/nginx/certs/domain.key;

  location /app1/ {
    proxy_pass http://192.168.x.x:8080;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;

  }
  location /app2/ {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_cache_bypass $http_upgrade;
     proxy_pass http://192.168.x.x:8081;
  }
}

如果我尝试在网址" https://192.168.xx/app1 "上打开应用, ,应用已到达,但我收到所有静态文件未捕获的SyntaxError:意外令牌<"的错误消息: 来自Chrome的错误消息

If I try to open the app on the url 'https://192.168.x.x/app1', the app is reached, but I get error messages for all static files 'Uncaught SyntaxError: Unexpected token <': Errormessages from chrome

似乎,返回的不是应用程序的index.html,而是静态的js和css文件.我认为这是应用程序本身的nginx配置的问题.

It seems, that instead of the static js and css files, the index.html of the app is returned. I believe that this is a problem of the nginx config of the apps themselves.

我花了很长时间试图找出解决这个问题的方法,但是还没有运气.我希望这里有人可以帮助我.

I have spent quite a time trying to figure out how to solve that problem, but no luck yet. I hope that someone here can help me with that.

推荐答案

首先,我更喜欢一种服务方法,即一个使用nginx提供重定向的容器.它还使用 Docker化的Nginx反向代理几乎自动覆盖了https (带有证书).如果需要,您还可以使用解密证书.您可以将Angular应用程序部署到

Firstly I prefer the approach one service, one container providing redirection with nginx. It also covers almost automatically https using a dockerized nginx reverse proxy with certificates. You can also use letsencrypt certificates if you want. You can deploy your Angular applications into containers behind the reverse proxy.

以下,我描述了您的docker部署的步骤.我还包括 portainer ,它是用于容器部署的GUI:

Below I describe the steps for your docker deployment. I also include portainer which is a GUI for your container deployment:

  • 使用选项-v $ HOME/certs:/etc/nginx/certs在共享端口为您的.domain生成的证书的端口443中运行nginx.将证书放在主机上的某个位置,例如/etc/nginx/certs .服务器重新引导时,需要选项-restart = always 自动运行容器:
  • Run the nginx in the port 443 sharing the certificates generated for your.domain using the option -v $HOME/certs:/etc/nginx/certs. Put the certificates somewhere on the host for instance /etc/nginx/certs. Option --restart=always is needed to automatically run the container when the server reboot:
docker run -d --name nginx-proxy --restart=always -p 443:443 -v /root/certs:/etc/nginx/certs -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆