简体   繁体   English

反向代理NGINX前端以重定向Dockerized Apache

[英]Reverse proxy NGINX front-end to redirect Dockerized Apache

I am coming from this . 我来自这个 I am new in web-servers. 我是网络服务器的新手。 I am setting up a server (example.de) serving several dockerized applications. 我正在设置一台服务器(example.de),为多个dockerized应用程序服务。 One of them (app3) is running on behind an Apache server (port 80 inside the container mapped to 8484). 其中一个(app3)在Apache服务器后面运行(容器内的端口80映射到8484)。 I want to use a NGINX docker container to redirect the url example.de/app3 to example.de:8484. 我想使用NGINX docker容器将URL example.de/app3重定向到example.de:8484。 Other two applications will be also redirected to example.de/app?. 其他两个应用程序也将重定向到example.de/app?。 When I request the url example.de:8484 it works fine. 当我请求网址example.de:8484时,它可以正常工作。 However, when I use example.de/app3 only the "It works" webpage appears. 但是,当我使用example.de/app3时,只会显示“有效”网页。

The docker-compose file is something similar to: docker-compose文件类似于:

version: '3'
services:
    app3:
        build:
            context: app3
        container_name: app3

    app3_apache:
        build:
            context: app3/apache
        container_name: app3_apache
        ports:
            - "8484:80"
        volumes:
            - vol_app3:/vol/app3
        depends_on:
            - app3
        links:
            - app3

    nginx-proxy:
      container_name: nginx
      build:
        context: nginx/
      ports:
        - "80:80"
      depends_on:
        - app3_apache
      links:
        - app3_apache
      volumes:
        - vol_nginx:/etc/nginx/conf.d

volumes:
    vol_app3:
        driver_opts:
            type: none
            device: /vol/app3
            o: bind
    vol_nginx:
        driver_opts:
            type: none
            device: /vol/nginx
            o: bind

NGINX default.conf file looks like: NGINX default.conf文件如下所示:

server {
    listen 80;
    listen [::]:80;
    server_name example.de;

    location /app3 {
      proxy_pass http://app3_apache:80;
    }
}

Additional information: It creates and run the containers. 附加信息:它创建并运行容器。 The webpage in the APACHE container is served in the NGINX container using the xxx.xxx.xxx.xxx:80 but not app3_apache:80 <h1>Bad Request (400)</h1> . APACHE容器中的网页是使用xxx.xxx.xxx.xxx:80而不是app3_apache:80 <h1>Bad Request (400)</h1>在NGINX容器中提供的。

I can reach the app3_apache container from the NGINX container: 我可以从NGINX容器访问app3_apache容器:

traceroute app3_apache
traceroute to app3_apache (xxx.xxx.xxx.xxx), 30 hops max, 60 byte packets
 1  app3_apache.server_default (xxx.xxx.xxx.xxx)  0.351 ms  0.054 ms  0.035 ms

You should create a docker network to link the container. 您应该创建一个泊坞窗网络来链接容器。 You can check docs here: https://docs.docker.com/network/bridge/ 您可以在此处检查文档: https : //docs.docker.com/network/bridge/

After you create the network, you will have to change configuration for nginx proxy_pass to point out the internal IP of the web server application. 创建网络后,您将必须更改nginx proxy_pass的配置以指出Web服务器应用程序的内部IP。

As for example I setup this network configuration to connect 3 containers with fixed IP addresses. 例如,我设置此网络配置以连接3个具有固定IP地址的容器。

version: '2'
services:
    zabbix-server4:
    container_name: zabbix-server4
    image: zabbix/zabbix-server-mysql:alpine-4.0.5
    networks:
        zbx_net:
        aliases:
            - zabbix-server4
        ipv4_address: 172.16.238.5

    zabbix-web4:
    container_name: zabbix-web4
    image: zabbix/zabbix-web-nginx-mysql:alpine-4.0.5
    ports: 
        - 127.0.0.1:11011:80
    links:
        - zabbix-server4
    environment:
        - ZBX_SERVER_HOST=172.16.238.5
    networks:
        zbx_net:
        aliases:
            - zabbix-web4
        ipv4_address: 172.16.238.10

    zabbix-agent4:
    container_name: zabbix-agent4
    image: zabbix/zabbix-agent:alpine-4.0.5
    links:
        - zabbix-server4
    environment:
        - ZBX_SERVER_HOST=172.16.238.5
    networks:
        zbx_net:
        aliases:
            - zabbix-agent4
        ipv4_address: 172.16.238.15


networks:
zbx_net:
    driver: bridge
    driver_opts:
    com.docker.network.enable_ipv6: "false"
    ipam:
    driver: default
    config:
    - subnet: 172.16.238.0/24
        gateway: 172.16.238.1

In your case, you could set a new network and assign new network's IPs to both the nginx and web container, then in nginx proxy_pass configuration you should put web server's IP. 在您的情况下,您可以设置一个新网络并将新网络的IP分配给nginx和Web容器,然后在nginx proxy_pass配置中,应放置Web服务器的IP。 Eg: 例如:

version: '3'
services:
    app3:
        build:
            context: app3
        container_name: app3

    app3_apache:
        build:
            context: app3/apache
        container_name: app3_apache
        ports:
            - "8484:80"
        volumes:
            - vol_app3:/vol/app3
        depends_on:
            - app3
        links:
            - app3
    networks:
        my_net:
        aliases:
            - zabbix-agent4
        ipv4_address: 172.16.11.10

    nginx-proxy:
    container_name: nginx
    build:
        context: nginx/
    ports:
        - "80:80"
    depends_on:
        - app3_apache
    links:
        - app3_apache
    volumes:
        - vol_nginx:/etc/nginx/conf.d
    networks:
        my_net:
        aliases:
            - zabbix-agent4
        ipv4_address: 172.16.11.20

volumes:
    vol_app3:
        driver_opts:
            type: none
            device: /vol/app3
            o: bind
    vol_nginx:
        driver_opts:
            type: none
            device: /vol/nginx
            o: bind

networks:
my_net:
    driver: bridge
    driver_opts:
    com.docker.network.enable_ipv6: "false"
    ipam:
    driver: default
    config:
    - subnet: 172.16.11.0/24
        gateway: 172.16.11.1

And proxy pass should be sety to 并且代理通行证应该设置为

location /app3 {
  proxy_pass http://172.16.11.10:80;
}

To prevent having this trouble, I run nginx on host, not in a container, then I split traffic in containers using same approach. 为了避免出现此问题,我在主机上而不是在容器中运行nginx,然后使用相同的方法在容器中拆分流量。 Having nginx installed in the host machine allow me to avoid this kind of configuration overhead. 在主机中安装nginx可以避免这种配置开销。

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

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