【问题标题】:Nginx Angular DockerNginx 角码头工人
【发布时间】:2019-07-22 09:30:39
【问题描述】:

我使用一个前端和两个后端服务器运行 docker-compose。对后端的请求就像一个魅力,我可以使用 angulars 路由器在我的应用程序中导航。 url 在我的浏览器中发生了更改,但我认为这是通过 angular 以某种方式在内部完成的。我的 baseref 是 / 我也用 ./ 尝试过。

问题: 一旦我重新加载不是索引的页面(或手动输入 uri),nginx 就会尝试将此页面转发到未找到它的@node。然后我可以用 express 重定向到索引,但这对我来说听起来不是一个可靠的解决方案。

我的 nginx 配置:

#This is a minimalist nginx configuration file that might be an appropriate starting point for dev work
#This file was not developed with the intent of being used on a production environment.
#user nobody nogroup;

worker_processes 1;

pid        /var/log/nginx/nginx.pid;
error_log  /var/log/nginx/error.log error;

events {
    worker_connections 512;
}

http {
  include   /etc/nginx/mime.types;

  #send all requests which are not served by nginx to nodejs core
  upstream docker-node {
      server myapp_core:3000;
  }
  upstream docker-intergram {
      server myapp_intergram:5000;
  }

  #chatbot
  server {
    listen 8443 default_server ssl;
    ssl_certificate /etc/ssl/cert_chain.crt;
    ssl_certificate_key /etc/ssl/my-app.key;
    server_name my-app.com;

    location / {
        proxy_pass         http://docker-intergram;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
  }

  #redirect to ssl
  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
  }

  # nginx server instance
  server {
      access_log /var/log/nginx/access.log;
      listen 443 default_server ssl;
      ssl_certificate /etc/ssl/cert_chain.crt;
      ssl_certificate_key /etc/ssl/my-app.key;
      server_name my-app.com;

      client_header_buffer_size 256k;
      large_client_header_buffers 8 1024k;
      client_max_body_size 5M;

      location /assets {
          alias /usr/src/app/assets/;
      }

      location /node_modules {
          alias /usr/src/app/node_modules;
      }

      location / {
          index index.html index.htm;
          root /usr/src/app;
          # try_files $uri $uri/ @node;
          try_files $uri$args $uri$args/ $uri/ @node;
      }

      location @node {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_buffering on;

          proxy_pass http://docker-node;
          proxy_redirect off;

          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
      }

      location /socket.io/ {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;
          proxy_buffering off;

          proxy_pass http://docker-node;
          proxy_redirect off;

          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
     }
  }

}

没有

try_files $uri $uri/ @node;

也不(来自nginx-angular2-angular-routes

try_files $uri$args $uri$args/ $uri/ @node; # solution from other stackoverflow question does not work

工作。

在这个solution 中似乎是为了索引,但我想保持我的后端路由不加前缀。

【问题讨论】:

  • 刷新时,您是否在浏览器上收到 Get 错误,说明路由不存在?
  • 一个 GET 错误,所以我认为它是由 @node 提供的。

标签: angular docker nginx


【解决方案1】:

正如建议的那样,位置顺序很重要。 尝试这样的事情(在下面我为所有 API 使用前缀)

upstream node_server {
  server localhost:3000;
}

server {
    listen 80;

     root /home/ec2-user/Elaisian/dist/;
     include /etc/nginx/default.d/*.conf;

    if ($http_x_forwarded_proto = 'http'){
       return 301 https://$host$request_uri;
    }

location /api/ {
   proxy_pass http://node_server/api/;
   proxy_http_version 1.1;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
 }

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

【讨论】:

  • 我想避免使用前缀,因为那时我需要在很多外部应用程序中更改它。我更多的是寻找在 404.S.th 之后再次重定向的重定向。就像try_files $uri $uri/ @node /index.html; 一样,但不幸的是,这不起作用,所以我不得不寻找两个位置的解决方案。
  • 你应该从节点考虑serving static filesapp.use(express.static(path.join(__dirname, '../dist')));
【解决方案2】:

我认为路由顺序在 nginx 配置中很重要。我建议将“位置/”路线作为最后一条路线。否则它可能会覆盖后面的路由,可能会发生在您的 /socket.io 路由中?

尝试将 location / 下的 try_files 行更改为:

try_files $uri /index.html

我在我的设置中以这种方式使用它并且它有效。 如果您需要 @node 下的配置作为根位置,您可以将其复制到该位置。

【讨论】:

  • 顺序很重要,但如果位置返回 404,它将尝试后续位置。所以 socket.io 仍然会被服务。我会把它放在前面,但这并不能解决问题,因为它是路由而不是 websockets 没有被服务。无论如何谢谢这个建议。我不能这样做try_files $uri /index.html,因为它需要为 be 路线提供不同的位置。
  • 没有看到您想要不带前缀的后端路由。 Angular 提供哈希路由,无需任何 nginx 更改即可解决您的问题。 angular.io/guide/…
猜你喜欢
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2017-02-01
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
相关资源
最近更新 更多