Docker撰写主机网络访问和反向代理

时间:2019-11-01 02:51:19

标签: docker nginx networking docker-compose reverse-proxy

在某种情况下,我创建了一个微服务,该微服务可以更改正在运行的服务器上的cat5以太网端口的网络设置。为此,需要将其设置为network_mode: host模式。微服务在nginx反向代理后面公开了一个我希望拥有的HTTP rest api,但是由于它使用了桥接网络,因此我似乎无法访问network_utilities(请参见下面的docker-compose文件)服务。有关如何进行这项工作的任何建议?

这是我压缩的docker-compose文件:

version: '3.3'
services:
  nginx:
    image: nginx:stable
    container_name: production_nginx
    restart: unless-stopped
    ports:
      - 80:80
    depends_on:
      - smart-mobile-server
      - network_utilities
    volumes:
      - ./config/front-end-gui:/var/www/html
      - ./config/nginx:/etc/nginx
    networks:
      - smart-mobile-loopback
  smart-mobile-server:
    container_name: smart-mobile-rest-api
    image: smartmobile_server:latest
    build:
      context: .
      dockerfile: main.Dockerfile
    environment:
      NODE_ENV: production
    command: sh -c "pm2 start --env production && pm2 logs all"
    depends_on:
      - 'postgres'
    restart: unless-stopped
    networks:
      - smart-mobile-loopback
    volumes:
      - ~/server:/usr/app/dist/express-server/uploads
      - ~/server/logs:/usr/app/logs
  network_utilities:
    image: smartgear/network-utilities-service:latest
    network_mode: host
    environment:
      NODE_ENV: production
      REST_API_PORT: '64000'
    privileged: true

networks:
  smart-mobile-loopback:
    driver: bridge

nginx.conf

worker_processes  2;

events {
    # Connections per worker process
    worker_connections  1024;

    # Turning epolling on is a handy tuning mechanism to use more efficient connection handling models.
    use epoll;

    # We turn off accept_mutex for speed, because we don’t mind the wasted resources at low connection request counts.
    accept_mutex off;
}

http {

    upstream main_server {
        # least_conn Specifies that a group should use a load balancing method where a request is 
        #   passed to the server with the least number of active connections, 
        #   taking into account weights of servers. If there are several such 
        #   servers, they are tried in turn using a weighted round-robin balancing method.
        ip_hash;
        # These are references to our backend containers, facilitated by
        # Compose, as defined in docker-compose.yml   
        server smart-mobile-server:10010;
    }

    upstream network_utilities {
        least_conn;
        server 127.0.0.1:64000;
    }

    server {
        # GZIP SETTINGS FOR LARGE FILES
        gzip  on;
        gzip_http_version 1.0;
        gzip_comp_level 6;
        gzip_min_length 0;
        gzip_buffers 16 8k;
        gzip_proxied any;
        gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
        gzip_disable "MSIE [1-6]\.";
        gzip_vary on;

        include /etc/nginx/mime.types;

        ## SECURITY SETTINGS
        # don't send the nginx version number in error pages and Server header
        server_tokens off;
        # when serving user-supplied content, include a X-Content-Type-Options: nosniff header along with the Content-Type: header,
        # to disable content-type sniffing on some browsers.
        add_header X-Content-Type-Options nosniff;


        listen 80;

        location / {
             # This would be the directory where your React app's static files are stored at
            root /var/www/html/;
            index index.html;

            try_files $uri /index.html;
        }

        location /api/documentation/network-utilities {
            proxy_pass http://network_utilities/api/documentation/network-utilities;
            proxy_set_header Host $host;
        }

        location /api/v1/network-utilities/ {
            proxy_pass http://network_utilities/;
            proxy_set_header Host $host;
        }

        location /api/ {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://main_server/api/;
        }
    }

}


0 个答案:

没有答案