docker搭建nginx實現(xiàn)負載均衡的示例代碼
安裝nginx
查詢安裝
[root@localhost ~]# docker search nginx [root@localhost ~]# docker pull nginx
準備
創(chuàng)建一個什么都不配置的nginx拿到一個nginx.conf文件和conf.d文件夾
創(chuàng)建文件、文件夾(只需創(chuàng)建logs文件夾即可,log文件是運行后自動掛載的)
[root@hao /usr/local/software/nginx]# tree
.
├── conf
│ ├── conf.d
│ │ └── default.conf
│ └── nginx.conf
├── html
│ ├── 50x.html
│ └── index.html
└── logs
├── access.log
└── error.log
4 directories, 6 files
運行(什么都不進行配置)
使用端口:8075映射80
docker run -it \ --name nginx \ -p 8075:80 \ -p 8080:8080 \ --privileged \ --network wn_docker_net \ --ip 172.18.12.90 \ -v /etc/localtime:/etc/localtime \ -v /usr/local/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /usr/local/software/nginx/html:/usr/share/nginx/html \ -v /usr/local/software/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /usr/local/software/nginx/logs:/var/log/nginx \ -d nginx
配置實現(xiàn)負載均衡
打開nginx.conf

完整的文件內(nèi)容:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
#myCode:
#配置上有服務(wù)器,形成負載
upstream activityBalance{
server 192.168.133.100:15348 weight=1;
server 192.168.200.113:15348 weight=1;
}
server{
keepalive_requests 120; #單連接請求上限次數(shù)
listen 8080; #監(jiān)聽端口號
location /api/{
proxy_pass http://activityBalance/api/; #反向代理服務(wù)器的訪問地址
proxy_set_header Host $host; #主機ip
proxy_set_header X-real-ip $remote_addr; #客戶端訪問的真實ip
proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for; #代理轉(zhuǎn)發(fā)歷史
proxy_redirect off;
}
}
include /etc/nginx/conf.d/*.conf;
}
到此這篇關(guān)于docker搭建nginx實現(xiàn)負載均衡的示例代碼的文章就介紹到這了,更多相關(guān)docker nginx負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker安裝nginx實現(xiàn)對springboot項目的負載均衡的操作方法
- docker swam集群如何實現(xiàn)負載均衡
- Docker安裝Nacos容器并根據(jù)Nginx實現(xiàn)負載均衡
- 基于Docker部署Tomcat集群、 Nginx負載均衡的問題小結(jié)
- docker swarm外部驗證負載均衡時不生效的解決方案
- Docker Nginx容器和Tomcat容器實現(xiàn)負載均衡與動靜分離操作
- 使用Docker Compose 實現(xiàn)nginx負載均衡的方法步驟
- 詳解Docker Swarm服務(wù)發(fā)現(xiàn)和負載均衡原理
- 詳解利用nginx和docker實現(xiàn)一個簡易的負載均衡
- Docker部署tenine實現(xiàn)后端應(yīng)用的高可用與負載均衡(推薦)
相關(guān)文章
使用Docker啟動mysql成功后,使用docker ps查不到的問題及解決
這篇文章主要介紹了使用Docker啟動mysql成功后,使用docker ps查不到的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
docker mysql修改root賬號密碼并賦予權(quán)限
本文主要介紹了docker mysql修改root賬號密碼并賦予權(quán)限,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
docker-compose網(wǎng)絡(luò)設(shè)置之networks的使用
本文詳細解釋了在使用 Docker Compose時如何配置網(wǎng)絡(luò),包括創(chuàng)建、使用和問題解決等方面,介紹了如何通過docker-compose.yml文件快速編排和部署應(yīng)用服務(wù),同時解決網(wǎng)絡(luò)隔離問題,感興趣的可以了解一下2024-10-10
docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
這篇文章主要介紹了docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
SQL?Server?簡介與?Docker?Compose?部署SQL?Server?容器
SQL?Server?是一個功能強大的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),適用于各種規(guī)模的應(yīng)用程序和數(shù)據(jù)存儲需求,在本文中,我將簡要介紹?SQL?Server?的基本概念,并詳細闡述如何使用?Docker?Compose?部署?SQL?Server?容器,感興趣的朋友跟隨小編一起看看吧2023-10-10

