简体   繁体   English

Nginx Angular Docker

[英]Nginx Angular Docker

I run docker-compose with one frontend and two backend servers. 我使用一个前端服务器和两个后端服务器运行docker-compose。 The requests to the backends work like a charm, I can navigate in my app by using angulars router. 对后端的请求就像一个护身符一样工作,我可以使用angulars路由器在我的应用中导航。 The urls are changed in my browser but I assume this is done somehow internally by angular. 网址在我的浏览器中已更改,但我认为这是通过角度在内部完成的。 My baseref is / I also tried it with ./ . 我的baseref是/我也用./尝试过。

Problem: As soon as I reload a page (or enter an uri manually) which is not index nginx tries to forward this page to @node where it is not found. 问题:我重新加载不是索引的页面(或手动输入uri)后,nginx尝试将该页面转发至未找到的@node。 I could then redirect with express to index but this does not sound like a ligid solution to me. 然后,我可以将express重定向到index,但这对我来说似乎不是一个可靠的解决方案。

My nginx config: 我的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";
     }
  }

}

Neither 都没有

try_files $uri $uri/ @node;

nor (from nginx-angular2-angular-routes ) 也(从nginx-angular2-angular-routes

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

work. 工作。

In this solution seems to be to froward to index but I would like to keep my backend routes unprefixed. 在此解决方案中似乎无法索引,但我想使后端路由不加前缀。

I think route order matters in the nginx config. 我认为Nginx配置中的路由顺序很重要。 I recommend putting "location /" route as the last route. 我建议将“位置/”路线作为最后一条路线。 Otherwise it might overwrite routes that follow, could be happening with your /socket.io route? 否则,它可能会覆盖随后的路由,可能与您的/socket.io路由一起发生?

Try changing your try_files line under location / to: 尝试将位置/下的try_files行更改为:

try_files $uri /index.html

I'm using it that way in my setup and it works. 我在我的设置中以这种方式使用它,并且可以正常工作。 If you need the config under @node for your root location you can copy it to that location. 如果您需要在@node下的配置作为您的根位置,则可以将其复制到该位置。

As suggested, the location order is important. 如建议的那样,位置顺序很重要。 try something like this (in the following i use a prefix for all API) 尝试这样的事情(在下文中,我为所有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;
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM