【问题标题】:Docker - Running nginx as a proxy for RedmineDocker - 运行 nginx 作为 Redmine 的代理
【发布时间】:2016-11-16 03:51:54
【问题描述】:

我想对 nginx 的设置进行 Dockerize 以 redmine(和其他服务)反向代理,但是在将请求从 nginx 转发到 redmine 时,我在浏览器上看到 501 bad gateway request 和 "failed (111 Connection refused)" on nginx 输出。

nginx 配置与常规 nginx 服务器(非 dockerized)一起使用,将请求转发到 dockerized redmine 服务器,这让我相信我在网络上做了一些棘手的事情。我也可以直接通过3000端口访问redmine服务器。

这是我的设置的 sn-p:

nginx.conf

http {
    server {
        listen 80;

        # Seeing a 501 Bad Gateway on the browser
        location /redmine {
            proxy_pass http://127.0.0.1:3000;
        }
    }
}

docker-compose.yaml

version: '2'
services:
    nginx:
        image: nginx
        ports:
            - "80:80"
        volumes:
            - ./nginx/nginx.conf:/etc/nginx/nginx.conf
        networks:
            - shared_internal
    redmine:
        image: redmine
        ports:
            - "3000:3000"
        networks:
            - shared_internal
networks:
    shared_internal:

【问题讨论】:

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


    【解决方案1】:

    注意:我还没有看过Bukharov Sergey's answer,所以这可能更优雅。


    这是我想出的。我发现了 2 种实现网络共享功能的方法和 1 种针对使用上述方法时出现的 Redmine 问题的破解方法。

    方法 1(首选是因为它更短,但可能不是因为它已弃用?):Container Linking

    docker-compose.yaml

    version: '2'
    services:
        nginx:
            image: nginx
            ports:
                - "80:80"
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            # Method 1: Linking
            links:
                - redmine
        redmine:
            image: redmine
            # Method 1: Exposing port to linked containers
            expose:
                - "3000"
    

    nginx.conf

    http {
        server {
            listen 80;
    
    # Method 1: Access via alias from link
            location /redmine/ {
                proxy_pass http://redmine:3000/;
            }
    }
    

    方法二:Defining a Network

    docker-compose.yaml

    version: '2'
    services:
        nginx:
            image: nginx
            ports:
                - "80:80"
            volumes:
                - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            # Method 2
            networks:
                shared_net:
                    ipv4_address: 172.22.0.4
        redmine:
            image: redmine
            # Method 2
            networks:
                shared_net:
                    ipv4_address: 172.22.0.5
    # Method number 2: Via networks
    networks:
        shared_net:
            driver: bridge
            ipam:
                config:
                    - subnet: 172.22.0.0/16
                      gateway: 172.22.0.1
    

    nginx.conf

    http {
        server {
            listen 80;
    
    # Method 2: Access via ip address in shared network
            location /redmine_networked/ {
                proxy_pass http://172.22.0.5:3000/;
            }
        }
    }
    

    Redmine 黑客:Accessing redmine via a suburl

    上述解决方案允许访问 Redmine 的主页。但是,所有 Redmine URL 都将指向根目录(例如,“/”代表主页,而不是“/redmine”或“/redmine_networked”)。所以这些链接都不起作用。如果 nginx 设置为将所有 '/' url 重定向到 Redmine,这将不是问题。以下 hack 假设情况并非如此。

    要让 Redmine 指向配置的 URL,需要编辑 config/environment.rb 文件。

    这是破解:

     > docker exec -it <redmine_container> bash
     redmine> cd config
    # We're going to install vim (this is a hack)
     redmine> apt-get update 
     redmine> apt-get install vim
     redmine> vim environment.rb
    

    更改 config/environment.rb 底部的以下行

    Initialize the Rails application Rails.application.initialize!
    

    RedmineApp::Application.routes.default_scope = "/redmine" 
    Initialize the Rails application Rails.application.initialize!
    
    redmine> exit
    > docker restart <redmine> (or just kill other docker process and run docker up again)
    

    【讨论】:

      【解决方案2】:

      您可以使用nginx proxy container nginx-proxy 设置一个运行 nginx 和 docker-gen 的容器。 docker-gen 为 nginx 生成反向代理配置,并在容器启动和停止时重新加载 nginx。

      比手动配置nginx更方便

      【讨论】:

        猜你喜欢
        • 2017-07-21
        • 2020-08-11
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多