gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:29:40 24 4
gpt4 key购买 nike

我想将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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com