docker启动nginx代理不上_Docker nginx 反向代理设置
文章目錄
[隱藏]
緣起
方案一
方案二
延伸
緣起
最近在公司搭建了一個基于 Gogs 的代碼管理系統,以及基于 Kanboard 的任務管理系統等幾個內部系統。由于部署在同一臺機器上,基于不同的端口區分不同的服務。比如:
Git 服務 http://10.10.1.110:10080
任務管理系統http://10.10.1.110:8888
其他
為了更好的使用,通過內部域名區分,比如 :
Git 服務 http://gogs.vking.io
任務管理系統 http://task.vking.io
其他
注:vking.io 是內部域名,可通過 dnsmasq (http://www.thekelleys.org.uk/dnsmasq/doc.html) 配置。
方案一
現有服務都是通過 Docker 部署,nginx 同樣通過 Docker 部署,使用官方提供的鏡像即可。
新建 nginx 配置文件, nginx.conf,存放路徑為 /srv/docker/nginx/nginx.conf
# user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } # daemon off;
新建反向代理設置文件 reverse-proxy.conf,存放路徑為 /srv/docker/nginx/conf.d/reverse-proxy.conf
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the # scheme used to connect to this server map $http_x_forwarded_proto $proxy_x_forwarded_proto { default $http_x_forwarded_proto; '' $scheme; } # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the # server port the client connected to map $http_x_forwarded_port $proxy_x_forwarded_port { default $http_x_forwarded_port; '' $server_port; } # If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any # Connection header that may have been passed to this server map $http_upgrade $proxy_connection { default upgrade; '' close; } # Apply fix for very long server names server_names_hash_bucket_size 128; # Default dhparam ssl_dhparam /etc/nginx/dhparam/dhparam.pem; # Set appropriate X-Forwarded-Ssl header map $scheme $proxy_x_forwarded_ssl { default off; https on; } gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; log_format vhost '$host $remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; access_log off; # HTTP 1.1 support proxy_http_version 1.1; proxy_buffering off; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $proxy_connection; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl; proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port; # Mitigate httpoxy attack (see README for details) proxy_set_header Proxy ""; server { server_name _; # This is just an invalid value which will never trigger on a real hostname. listen 80; access_log /var/log/nginx/access.log vhost; return 503; } # gogs.vking.io upstream gogs.vking.io { # gogs server gogs:3000; } server { server_name gogs.vking.io; listen 80; location / { proxy_pass http://gogs.vking.io; } access_log /var/log/nginx/access.log vhost; } # task.vking.io upstream task.vking.io { # kanboard server kanboard:80; } server { server_name task.vking.io; listen 80; location / { proxy_pass http://task.vking.io; } access_log /var/log/nginx/access.log vhost; }
gogs 啟動命令
docker container run -d --name gogs --restart always -p 10022:22 -p 10080:3000 --network gogs-net -v /srv/docker/gogs:/data gogs/gogs:latest
注: upstream gogs.vking.io 中的 server 中的 gogs:3000 分別指容器名稱和原始expose 的端口。
啟動容器
docker container run -d --name nginx --restart always -p 80:80 --network gogs-net -v /srv/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /srv/docker/nginx/conf.d:/etc/nginx/conf.d nginx:alpines
注:–network gogs-net 指定三個容器在同一網絡,如果用默認的 bridge的話,不需要設置
方案二
因為這些服務都是部署在一臺機器上的,可以通過 Docker 的自動服務發現部署,原理見此。
gogs
docker container run -d --name gogs --restart always -p 10022:22 -p 10080:3000 --network gogs-net -e VIRTUAL_HOST=gogs.vking.io -e VIRTUAL_PORT=3000 -v /opt/docker/gogs:/data gogs/gogs:latest
Kanboard
docker container run -d --name kanboard --restart always -p 8888:80 --network gogs-net -e VIRTUAL_HOST=task.vking.io -e VIRTUAL_PORT=80 -v /srv/docker/kanboard/data:/var/www/app/data -v /srv/docker/kanboard/plugins:/var/www/app/plugins kanboard/kanboard:latest
nginx
docker container run -d --name nginx --restart always -p 80:80 --network gogs-net -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy:alpine
注:關鍵是容器通過 -e VIRTUAL_HOST 指定 url,通過 -e VIRTUAL_PORT=80 指定端口,同樣端口也必須是原始鏡像 expose 的端口。
延伸
目前服務都是通過 shell 啟動,可改成通過 Docker Compose 統一編排任務,把 dnsmasq+nginx+gogs+… 等統一管理。如果是部署在公網上的話,還可以把 SSL 證書到期自動刷新等一起編排。
—EOF—
原文出處:github -> https://gythialy.github.io/Docker-nginx-reverse-proxy/
總結
以上是生活随笔為你收集整理的docker启动nginx代理不上_Docker nginx 反向代理设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: a股历史30年的大盘价_[最新]回顾A股
- 下一篇: mysql 压力测试知乎_MySQL 对