本文描述如何在一台机器上搭建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. C#扫盲篇(一):反射机制--情真意切的说

    在一线编码已有多年,积累了不少非常实用的技能,最近的更新会逐步的分享出来,希望能帮助到还有一丢丢喜欢.Net的朋友,当然这些都比较适合入门选手,虽然自己已是个精通抄代码的老猿,但技术造诣仍是渣渣. 犹 ...

  2. ASP.NET Core - JWT认证实现

    一.JWT结构 JWT介绍就太多了,这里主要关注下Jwt的结构. Jwt中包含三个部分:Header(头部).Payload(负载).Signature(签名) Header:描述 JWT 的元数据的 ...

  3. GMT UTC CST ISO 夏令时 时间戳,都是些什么鬼?

    目录 ✍前言 本文提纲 版本约定 ✍正文 GMT:格林威治时间 凭什么格林威治作为标准时间? 地球自转 中国有哪几个时区? 美国有哪几个时区? GMT和Http协议的渊源 UTC:世界标准时间 UTC ...

  4. kubernets之控制器之间的协作以及网络

    一  创建一个deployment的时候整个kubernets集群的资源和事件的调用链 1.1  创建一个deployment的资源,在提交的时候,集群中的调度器,控制器以及node节点上kubele ...

  5. 萌新入门之python基础语法

    首先我们先了解一些python最最基础的入门 1.标识符 定义:我们写代码的时候自己取得名字比如项目名,包名,模块名这些: 规范:1.数字/字母/下划线组成,不能以数字开头 2.起名字要见名知意 3. ...

  6. [java]文件上传下载删除与图片预览

    图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...

  7. Swagger-UI展示接口

    简单介绍API的管理工具Swagger的UI模块. 简介:swagger ui就是一个能整合到项目中让api的注释能够生成到一个网页上.能简单测试和给前端看. 第一步:添加引用 打开NuGet程序包管 ...

  8. MySQL调优用户监控之show processlist

    简介 show processlist显示这台MySQL正在连接的用户: mysql> show processlist; +----+------+-----------+-------+-- ...

  9. 如何让淘宝不卡顿? 读写比例 动态扩容 分布式化路线 mysql 优化

    作为数据库核心成员,如何让淘宝不卡顿? https://mp.weixin.qq.com/s/l-qXV8NI6ywnUvp3S6an3g

  10. python_3 装饰器参数之谜

    装饰器参数之谜 之前已经初步了解过装饰器了,知道了装饰器可以"偷梁换柱",在不改变函数的调用方式和函数内容的时候,而把函数的功能偷偷地修改. 那么问题来了,如果被修改的函数中有参数 ...