問(wèn)題背景
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),廣昌網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:廣昌等地區(qū)。廣昌做網(wǎng)站價(jià)格咨詢:028-86922220
在docker里用同一份鏡像創(chuàng)建4個(gè)容器,網(wǎng)絡(luò)選擇bridge模式,A服務(wù)在四個(gè)容器中都使用了同一個(gè)端口號(hào)(6000);為了減少對(duì)外暴露的端口數(shù),另外使用了nginx對(duì)這4個(gè)服務(wù)實(shí)例做代理,4個(gè)服務(wù)實(shí)例分屬4個(gè)upstream,使用了類似/service1、/service2的路徑來(lái)訪問(wèn)4個(gè)實(shí)例。
此時(shí)從本地訪問(wèn)任一服務(wù),則會(huì)報(bào)502錯(cuò)誤,百思不得其解。
connect() failed (111: Connection refused) while connecting to upstream
compose文件version: '2'networks: nn: driver: bridgeservices: service-1: container_name: service-1 image: foo networks: - nn volumes: - ./logs/1:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/1.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-2: container_name: service-2 image: foo networks: - nn volumes: - ./logs/2:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/2.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-3: container_name: service-3 image: foo networks: - nn volumes: - ./logs/3:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/3.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 service-4: container_name: service-4 image: foo networks: - nn volumes: - ./logs/4:/apps/aaa/bbb-logs - ./common:/apps/aaa/bbb - ./xxx/4.xml:/ccc/targets.xml entrypoint: foo.sh command: start app=foo port=6000 nginx: container_name: nginx image: nginx:1.15-alpine ports: - 6001:6001 networks: - nn volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./logs/nginx:/var/log/nginx
nginx.confworker_processes 8;worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type aplication/octet-stream; sendfile on; log_format main '[$time_local]$remote_addr-$upstream_addr "$request" $status $body_bytes_sent'; upstream service1.local { server service-1:6000; } upstream service2.local { server service-2:6000; } upstream service3.local { server service-3:6000; } upstream service4.local { server service-4:6000; } server { listen 6001; client_max_body_size 100M; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location /service1/ { proxy_pass http://service1.local/; } location /service2/ { proxy_pass http://service2.local/; } location /service3/ { proxy_pass http://service3.local/; } location /service4/ { proxy_pass http://service4.local/; } location /nginx_status { stub_status on; access_log off; } }}
此時(shí)curl localhost:6001/service1/api/v1/....就會(huì)報(bào)上面的502錯(cuò)誤,按理說(shuō)每一個(gè)容器都是有各自的網(wǎng)卡,不同容器的端口號(hào)應(yīng)該不沖突才對(duì)。解決方案
暫時(shí)沒(méi)有較好的方案,只能對(duì)4個(gè)服務(wù)使用不同的端口號(hào),nginx也相應(yīng)地修改。
補(bǔ)充:同臺(tái)服務(wù)器部署多套docker容器,端口重定向問(wèn)題
在生成環(huán)境,部署多個(gè)容器,訪問(wèn)多個(gè)端口;
例如:-p 80:80 -p 81:81
81地址退出的時(shí)候,直接訪問(wèn)到80端口的地址。
誤區(qū):開始以為是cookie的問(wèn)題,因?yàn)樗⑿铝薱ookie(cookie是不區(qū)分端口號(hào))
最終找到了原因:redirect問(wèn)題,因?yàn)橥顺鲋囟ㄏ虻降卿涰?yè)面解決方案:配置nginx參數(shù)
proxy_set_header HOST $host; 改成 proxy_set_header HOST $host:81;
因?yàn)椴还茉鯓?,?qǐng)求參數(shù)投都帶上了端口號(hào)。
網(wǎng)上還有一種方法:修改proxy_redirect 參數(shù)(不過(guò)試過(guò)沒(méi)有生效)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。