Docker - 运行 nginx 作为 Redmine 的代理
Docker - Running nginx as a proxy for Redmine
我想将 nginx 的设置 Dockerize 到 redmine(和其他服务)反向代理,但是当将请求从 nginx 转发到 redmine 时,我在浏览器上看到 501 错误的网关请求和 "failed (111 Connection refused)"
在 nginx 输出上。
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:
您可以使用 nginx proxy container
nginx-proxy 设置了一个容器 运行 nginx 和 docker-gen。 docker-gen 为 nginx 生成反向代理配置,并在容器启动和停止时重新加载 nginx。
比手动配置nginx更方便
注意:我还没有看过,所以这样可能更优雅。
这是我的想法。我找到了 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/;
}
}
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”)。因此,这些链接中的 none 将起作用。如果 nginx 被设置为将所有 '/' url 重定向到 Redmine,这将不是问题。以下技巧假设情况并非如此。
要让 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
Change the following lines at the bottom of your config/environment.rb
Initialize the Rails application Rails.application.initialize!
to
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)
我想将 nginx 的设置 Dockerize 到 redmine(和其他服务)反向代理,但是当将请求从 nginx 转发到 redmine 时,我在浏览器上看到 501 错误的网关请求和 "failed (111 Connection refused)"
在 nginx 输出上。
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:
您可以使用 nginx proxy container nginx-proxy 设置了一个容器 运行 nginx 和 docker-gen。 docker-gen 为 nginx 生成反向代理配置,并在容器启动和停止时重新加载 nginx。
比手动配置nginx更方便
注意:我还没有看过
这是我的想法。我找到了 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/;
}
}
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”)。因此,这些链接中的 none 将起作用。如果 nginx 被设置为将所有 '/' url 重定向到 Redmine,这将不是问题。以下技巧假设情况并非如此。
要让 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
Change the following lines at the bottom of your config/environment.rb
Initialize the Rails application Rails.application.initialize!
to
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)