本文描述如何在一台机器上搭建nginx负载均衡,我将会启动3个nginx的docker,分别是1台前置nginx负责分发,后面2台负责处理请求。

首先我切换到/usr/local/docker/文件夹下,这个文件夹是专门用来做docker映射文件夹用的,docker里的重要的文件夹会映射到这里,在这里执行

mkdir nginx

mkdir nginx01

mkdir nginx02

nginx下存储的是前置nginx的文件

nginx01和nginx02负责存储后边两台nginx服务器的文件

《一、前置nginx配置以及启动》

切换到/usr/local/docker/nginx下

然后执行

mkdir -p conf  html  logs

切换到conf

vim nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
} http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server {
listen 80 default_server;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; location / {
proxy_pass http://pic;
} error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
} upstream pic{
server 172.26.200.89:8088 weight=5;
server 172.26.200.89:8089 weight=5;
} }

配置好以后,保存conf文件

然后启动这个前置nginx

docker run --name mynginx -d -p 82:80  -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /usr/local/docker/nginx/logs:/var/log/nginx -d nginx

《二、nginx01配置以及启动》

切换到/usr/local/docker/nginx01/文件夹

执行

mkdir -p conf  html  logs

然后切换到conf文件夹

vim nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
} http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server { listen 8088;
root /usr/share/nginx/html;
index index.html index.htm;
} }

然后保存配置文件,退出

切换到/usr/local/docker/nginx01/html/文件夹下

vim index.html,简单写点东西,如下

8088 port nginx

然后启动docker

docker run --name mynginx01 -d -p 8088:8088 -v /usr/local/docker/nginx01/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx01/logs:/var/log/nginx -v /usr/local/docker/nginx01/html:/usr/share/nginx/html -d nginx

《三、nginx02配置以及启动》

切换到/usr/local/docker/nginx02/文件夹

执行

mkdir -p conf  html  logs

然后切换到conf文件夹

vim nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; events {
worker_connections 1024;
} http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048; include /etc/nginx/mime.types;
default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf; server { listen 8089;
root /usr/share/nginx/html;
index index.html index.htm;
} }

然后保存配置文件,退出

切换到/usr/local/docker/nginx02/html/文件夹下

vim index.html,简单写点东西,如下

8089 port nginx

然后启动docker

docker run --name mynginx02 -d -p 8089:8089  -v /usr/local/docker/nginx02/conf/nginx.conf:/etc/nginx/nginx.conf  -v /usr/local/docker/nginx02/logs:/var/log/nginx -v /usr/local/docker/nginx02/html:/usr/share/nginx/html -d nginx

-------------------------------分割线-----------------------

至此,前置nginx和两台后置nginx都启动了,我们测试一下

在命令行执行

curl 127.0.0.1:82

如下图:

可以看到,由于我们前置nginx上的负载策略是各百分之50,所以出现了第一次请求发送到8088的nginx,第二次请求发送到8089的nginx,第三次又到8088,第四次8089。。。。。。

大家仅供参考,谢谢阅读全文。

爱词霸

 
 
划词翻译
自动发声
自动添加生词本

docker 搭建 nginx负载均衡的更多相关文章

  1. Net分布式系统之二:CentOS系统搭建Nginx负载均衡

    一.关于CentOS系统介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat ...

  2. Nginx系列一:正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡

    转自https://www.cnblogs.com/leeSmall/p/9351343.html 仅供个人学习 一.什么是正向代理.什么是反向代理 1. 正向代理,意思是一个位于客户端和原始服务器( ...

  3. Docker 安装 Nginx 负载均衡配置

    Docker 安装 # 1)安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 2)添加Docker软件包源(否则d ...

  4. Nginx系列篇二:linux搭建Nginx负载均衡

    建议先搭建好Nginx环境 可阅读--->Linux中搭建Nginx 1.准备好三台服务器[标配] 一.nginx负载均衡服务器:192.168.102.110,配置好Nginx 二.tomca ...

  5. Linux学习10-CentOS搭建nginx负载均衡环境

    前言 当自己的web网站访问的人越来越多,一台服务器无法满足现有的业务时,此时会想到多加几台服务器来实现负载均衡. 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,怎样将同一个域名的访问分散 ...

  6. Tomcat WEB搭建+Nginx负载均衡动静分离+DNS解析的实验

    实验拓扑图: 实验环境: 在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境. 实验过程中需要安装的工具包包括:vim unzip lrzsz ls ...

  7. Docker容器Nginx负载均衡配置、check及stub模块安装

    Nginx是一款高性能的HTTP和反向代理.负载均衡web服务器.本次在Docker容器中部署三个tomcat,Nginx代理三个tomcat服务(以下称节点)来模拟实现负载均衡效果,配置check模 ...

  8. Net分布式系统之二:CentOS系统搭建Nginx负载均衡(下)

    上一篇文章介绍了VMWare12虚拟机.Linux(CentOS7)系统安装.部署Nginx1.6.3代理服务做负载均衡.接下来介绍通过Nginx将请求分发到各web应用处理服务. 一.Web应用开发 ...

  9. Linux搭建nginx负载均衡(两台服务器之间)

    负载均衡种类 第一种:通过硬件负载解决,常见的有NetScaler.F5.Radware和Array等商用的负载均衡器,价格比较昂贵 第二种:通过软件负载解决,常见的软件有LVS.Nginx.apac ...

随机推荐

  1. Harbor镜像删除回收?只看这篇

    最近,公司的技术平台,运维的破事儿颇多.Jira无法访问,ES堆内存不足,Jenkins频繁不工作..等等等,让我这个刚入门的小兵抓心脑肝,夜不能寐,关键时刻方恨经验薄弱呀!!一波未平,一波又起,这不 ...

  2. show slave status常用参数备忘

    mysql> show slave status\G*************************** 1. row *************************** Slave_IO ...

  3. 利用Python-docx 读写 Word 文档中的正文、表格、段落、字体等

    前言: 前两篇博客介绍了 Python 的 docx 模块对 Word 文档的写操作,这篇博客将介绍如何用 docx 模块读取已有 Word 文档中的信息. 本篇博客主要内容有: 1.获取文档的章节信 ...

  4. 【Linux】centos7中 root家目录中perl5文件夹无法删除问题解决

    由于新项目上线,安装了一些perl的一些包 但是发现,在/root下有一个perl5/的文件夹,删除后,重新登录又会出现,很是烦人,而且他还没有内容,就是一个空文件 那么着手搞掉他 环境:centos ...

  5. 【Linux】Linux系统dev/目录下的tty

    终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备.tty是Teletype的缩写.Teletype是最早出现的一种终端设备,很象电传打字机(或者说就是),是由Teletyp ...

  6. 在Firefox上使用Chrome的crx扩展程序

    假如你喜欢使用Firefox火狐浏览器,可是发现有个很喜欢很想用的扩展只发布了支持Chrome的crx格式--Firefox从57版以后使用了WebExtension API作为新附加组件的开发标准, ...

  7. .NET 项目中的单元测试

    .NET 项目中的单元测试 Intro "不会写单元测试的程序员不是合格的程序员,不写单元测试的程序员不是优秀的工程师." -- 一只想要成为一个优秀程序员的渣逼程序猿. 那么问题 ...

  8. 7行代码解决P1441砝码称重(附优化过程)

    先贴上最终代码感受一下: #include <bits/stdc++.h> using namespace std; int i, N, M, wi[21], res = 0; int m ...

  9. Java中的深浅拷贝问题,你清楚吗?

    一.前言 拷贝这个词想必大家都很熟悉,在工作中经常需要拷贝一份文件作为副本.拷贝的好处也很明显,相较于新建来说,可以节省很大的工作量.在Java中,同样存在拷贝这个概念,拷贝的意义也是可以节省创建对象 ...

  10. 【图像处理】RGB Bayer Color分析

    Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示,由一半的G,1/4的R,1/4的B组 ...