nginx容器之间代理
反向代理端
[root@server01 nginx]# ll 总用量 4 -rw-r--r-- 1 root root 1383 1月 19 13:23 default.conf [root@server01 nginx]# cat default.conf upstream node1 { server 192.168.0.221:82; server 192.168.0.221; } server { listen 80; server_name 192.168.0.220; access_log /var/log/nginx/forward.access.log main; error_log /var/log/nginx/forward.error.log warn; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 location / { proxy_pass http://node1; } }
启动容器
docker run -it --name nginx -p 80:80 -v /nginx/default.conf:/etc/nginx/conf.d/default.conf -d nginx:latest
后端服务器
启动两个nginx容器 docker run -it --name nginx1 -p 82:80 -d nginx:latest docker run -it --name nginx -p 80:80 -d nginx1:latest
网页内容
[root@server02 nginx]# docker exec -it nginx /bin/bash root@bb26273d260b:/# cat /usr/share/nginx/html/index.html port test 192.168.0.221 root@bb26273d260b:/# exit [root@server02 nginx]# docker exec -it nginx1 /bin/bash root@f0d8409e7e14:/# cat /usr/share/nginx/html/index.html 123456 root@f0d8409e7e14:/# exit exit
[root@server02 nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0d8409e7e14 nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 27 minutes 0.0.0.0:82->80/tcp nginx1 bb26273d260b nginx:latest "/docker-entrypoint.…" 2 hours ago Up About an hour 0.0.0.0:80->80/tcp nginx
测试
[root@server01 nginx]# curl localhost 123456 [root@server01 nginx]# curl localhost port test 192.168.0.221 [root@server01 nginx]# curl localhost 123456 [root@server01 nginx]# curl localhost port test 192.168.0.221 [root@server01 nginx]# curl localhost 123456 [root@server01 nginx]# curl localhost port test 192.168.0.221
单机上两个nginx容器代理
先启动两个nginx容器
docker run -it --name nginx2 -p 82:80 -d nginx1:latest
docker run -it --name nginx2 -p 83:80 -d nginx2:latest
[root@server01 nginx]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9885bdc24a54 nginx:latest "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:83->80/tcp nginx2 e02eb72e071a nginx:latest "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:82->80/tcp nginx1
docker run -it --name nginx -p 80:80 --link nginx1 --link nginx2 -v /nginx/default.conf:/etc/nginx/conf.d/default.conf -d nginx:latest
[root@server01 nginx]# cat default.conf upstream node1 { server nginx1:80; server nginx2:80; } server { listen 80; server_name 192.168.0.220; access_log /var/log/nginx/forward.access.log main; error_log /var/log/nginx/forward.error.log warn; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://node1; } } [root@server01 nginx]#
不同主机间nginx容器代理
192.168.0.221启动三个nginx容器
docker run -it --name nginx5 -p 85:80 -d nginx:latest docker run -it --name nginx1 -p 82:80 -d nginx:latest docker run -it --name nginx -p 80:80 -d nginx:latest
写好每个nginx容器的index.html,内容不要相同。
192.168.0.220启动一个容器
[root@server01 nginx]# cat default.conf upstream node1 { server 192.168.0.221:85; server 192.168.0.221:80; server 192.168.0.221:82; } server { listen 80; server_name 192.168.0.220; access_log /var/log/nginx/forward.access.log main; error_log /var/log/nginx/forward.error.log warn; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://node1; } }
启动容器
docker run -it --name nginx -p 80:80 -v /nginx/default.conf:/etc/nginx/conf.d/default.conf -d nginx:latest
测试:
[root@server01 nginx]# curl localhost
123456
[root@server01 nginx]# curl localhost
jdfhskdjfh
[root@server01 nginx]# curl localhost
port test
192.168.0.221
[root@server01 nginx]# curl localhost
123456
[root@server01 nginx]# curl localhost
jdfhskdjfh
[root@server01 nginx]# curl localhost
port test
192.168.0.221