我想对Nginx的设置进行Dockerize来重新定义(和其他服务)反向代理,但是当将请求从Nginx转发到Redmine时,我在浏览器上看到501错误的网关请求,而在Nginx输出上看到了"failed (111 Connection refused)"

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

这是我的设置的摘要:

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:

最佳答案

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

这是我想出的。我发现有两种实现网络共享功能的方法,还有一种针对使用上述方法时出现的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/;
        }
}

方法2: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 Hack:Accessing redmine via a suburl

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

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

这是hack:
 > 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


redmine> exit
> docker restart <redmine> (or just kill other docker process and run docker up again)

关于nginx - Docker-运行Nginx作为Redmine的代理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38353707/

10-15 12:38