docker搭建nginx實(shí)現(xiàn)負(fù)載均衡的示例代碼
安裝nginx
查詢安裝
[root@localhost ~]# docker search nginx [root@localhost ~]# docker pull nginx
準(zhǔn)備
創(chuàng)建一個(gè)什么都不配置的nginx拿到一個(gè)nginx.conf文件和conf.d文件夾
創(chuàng)建文件、文件夾(只需創(chuàng)建logs文件夾即可,log文件是運(yùn)行后自動(dòng)掛載的)
[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
運(yùn)行(什么都不進(jìn)行配置)
使用端口: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
配置實(shí)現(xiàn)負(fù)載均衡
打開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ù)器,形成負(fù)載 upstream activityBalance{ server 192.168.133.100:15348 weight=1; server 192.168.200.113:15348 weight=1; } server{ keepalive_requests 120; #單連接請(qǐng)求上限次數(shù) listen 8080; #監(jiān)聽端口號(hào) location /api/{ proxy_pass http://activityBalance/api/; #反向代理服務(wù)器的訪問地址 proxy_set_header Host $host; #主機(jī)ip proxy_set_header X-real-ip $remote_addr; #客戶端訪問的真實(shí)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實(shí)現(xiàn)負(fù)載均衡的示例代碼的文章就介紹到這了,更多相關(guān)docker nginx負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker安裝nginx實(shí)現(xiàn)對(duì)springboot項(xiàng)目的負(fù)載均衡的操作方法
- docker swam集群如何實(shí)現(xiàn)負(fù)載均衡
- Docker安裝Nacos容器并根據(jù)Nginx實(shí)現(xiàn)負(fù)載均衡
- 基于Docker部署Tomcat集群、 Nginx負(fù)載均衡的問題小結(jié)
- docker swarm外部驗(yàn)證負(fù)載均衡時(shí)不生效的解決方案
- Docker Nginx容器和Tomcat容器實(shí)現(xiàn)負(fù)載均衡與動(dòng)靜分離操作
- 使用Docker Compose 實(shí)現(xiàn)nginx負(fù)載均衡的方法步驟
- 詳解Docker Swarm服務(wù)發(fā)現(xiàn)和負(fù)載均衡原理
- 詳解利用nginx和docker實(shí)現(xiàn)一個(gè)簡(jiǎn)易的負(fù)載均衡
- Docker部署tenine實(shí)現(xiàn)后端應(yīng)用的高可用與負(fù)載均衡(推薦)
相關(guān)文章
dockerfile的使用及使用dockerfile部署springboot項(xiàng)目的過程
Dockerfile是用來構(gòu)建Docker鏡像的文本文件,是由一條條構(gòu)建鏡像所需的指令和參數(shù)構(gòu)成的腳本,這篇文章主要介紹了dockerfile的使用,使用dockerfile部署springboot項(xiàng)目,需要的朋友可以參考下2023-04-04Portainer部署和添加其他docker服務(wù)器的方法
文章介紹了如何部署Portainer并添加其他Docker服務(wù)器,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-03-03docker安裝、調(diào)試qsign簽名服務(wù)器的解決方案
這篇文章主要介紹了docker安裝、調(diào)試qsign簽名服務(wù)器的解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-04-04docker-compose如何定義一個(gè)橋接網(wǎng)絡(luò),并為該網(wǎng)絡(luò)配置一個(gè)IP地址池
在DockerCompose中定義橋接網(wǎng)絡(luò)并配置IP地址池,可以實(shí)現(xiàn)服務(wù)的自動(dòng)IP地址分配,通過定義網(wǎng)絡(luò)、指定子網(wǎng)范圍、設(shè)置網(wǎng)關(guān)和啟用自動(dòng)分配功能,可以輕松管理服務(wù)的網(wǎng)絡(luò)配置,確保IP地址在子網(wǎng)范圍內(nèi)且不與其他網(wǎng)絡(luò)沖突,以避免網(wǎng)絡(luò)沖突2025-01-01