docker - 如何使用NGINX将请求转发到Docker Microservice

标签 docker nginx nginx-location nginx-reverse-proxy

我想将NGINX用作反向代理,以将请求转发到微服务。
NGINX和微服务都托管在docker容器上。

下面是我的nginx.conf文件

    worker_processes 1;

    events { worker_connections 1024; }

    #test

    http {

        sendfile on;

        # upstream docker-nginx {
        #     server nginx:80;
        # }

        upstream admin-portal {
            # server admin-portal:9006;
            server xx.xx.xx.xx:9006;
            # server localhost:9006;

        }

server {
            listen 8080;

            location / {
                proxy_pass         http://admin-portal;
                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;
            }


        }


    }

Docker文件
FROM nginx
RUN apt-get update && apt-get install -y \
curl 
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080

docker-compose.yml
version: '3'
services:
  nginx:
    restart: always
    build: ../../conf/
    volumes:
    - ./mysite.template:/etc/nginx/conf.d/mysite.template
    ports:
    - "8080:8080"
    networks:
      - cloud


networks:
  cloud:
   driver: bridge
  • 我可以通过 localhost:8080访问nginx主页
  • admin-portal是在9006端口
  • 上运行的微服务

    但是,如果我执行 localhost:8080 / admin-portal / ,则会收到以下错误消息
    nginx_1 | 2018/07/04 07:08:17 [error] 7#7: *1 "/usr/share/nginx/html/admin-portal/index.html" is not found (2: No such file or directory), client: xx.xx.xx.xx, server: your-domain.com, request: "GET /admin-portal/ HTTP/1.1", host: "xx.xx.xx.xx:8080" nginx_1 | 2018/07/04 07:08:17 [error] 7#7: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: xx.xx.xx.xx, server: your-domain.com, request: "GET /admin-portal/ HTTP/1.1", host: "xx.xx.xx.xx:8080" nginx_1 | xx.xx.xx.xx - - [04/Jul/2018:07:08:17 +0000] "GET /admin-portal/ HTTP/1.1" 404 170 "-" "curl/7.47.0" admin-portal/
    请提出我需要做些什么更改才能使用nginx将请求转发到admin-portal

    最佳答案

    upstream admin-portal {
        server 127.0.0.1:9006;
    }
    

    它应该是 :
    upstream admin-portal {
        server 172.17.0.1:9006;
    }
    

    172.17.0.1是容器的IP地址网关。

    docker inspect containermicroservice_id然后获取该容器的IP地址。
     upstream admin-portal {
        server ipaddressofmicroservicecontainer:9006;
    }
    

    将服务器的IP地址放入
            server_name    localhost ipaddressofserver www.example.com;
    

    然后访问http://ipaddressofserver:8080/admin-portal
    注释掉这部分:
    #server {
    #            listen       8080;
    #            server_name     your-domain.com www.your-domain.com;
    #            root   /usr/share/nginx/html;
    #            index  index.html index.htm;
    
    #            error_page  404              /404.html;
    #            error_page   500 502 503 504  /50x.html;
    #            location = /50x.html {
    #                root   html;
    #            }
            }
    

    关于docker - 如何使用NGINX将请求转发到Docker Microservice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167990/

    相关文章:

    php - 多个远程上传服务器到服务器

    docker - 在 CI/CD 中如何管理前端和后端之间的依赖关系?

    logging - 通过配置日志驱动程序将 Docker 容器日志发送到 ELK Stack - 简单方法

    docker - 恢复到 neo4j docker 容器

    redirect - NGINX - 返回 301 与重写

    nginx - 我可以在 Nginx 中创建一个 "private"位置吗?

    docker stack deploy 不会删除当前 yaml compose 文件中未声明的服务

    nginx - 如何在 Nginx 中重定向单个 URL?

    javascript - 生产规模太大

    php - Nginx 子目录根目录与 PHP