本文描述如何在一台机器上搭建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. Spark推荐系统实践

    推荐系统是根据用户的行为.兴趣等特征,将用户感兴趣的信息.产品等推荐给用户的系统,它的出现主要是为了解决信息过载和用户无明确需求的问题,根据划分标准的不同,又分很多种类别: 根据目标用户的不同,可划分 ...

  2. 【C++】《Effective C++》第三章

    第三章 资源管理 条款13:以对象管理资源 当申请一块动态内存时,可能会发生内存泄漏. class Investment {}; void f() { Investment* pInv = creat ...

  3. 一道有趣的golang排错题

    很久没写博客了,不得不说go语言爱好者周刊是个宝贝,本来想随便看看打发时间的,没想到一下子给了我久违的灵感. go语言爱好者周刊78期出了一道非常有意思的题目. 我们来看看题目.先给出如下的代码: p ...

  4. docker 报错: Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

    最近在 Windows 子系统 WSL 上面安装了一个 ubuntu18.04, 安装完docker 跑 hello-world 的时候报错了 docker: Cannot connect to th ...

  5. 【VNC】vnc远程连接的时候无法显示图像已解决

    介绍一个 VNC连接工具:iis7服务器管理工具 IIs7服务器管理工具可以批量连接并管理VNC服务器 作为服务器集成管理器,它最优秀的功能就是批量管理windows与linux系统服务器.vps.能 ...

  6. iptables自动屏蔽访问网站最频繁的IP

    iptables自动屏蔽访问网站频繁的IP 屏蔽每分钟访问超过200的IP 方法1:根据访问日志(Nginx为例 #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) AB ...

  7. postgresql中权限介绍

    postgresql权限分为实例的权限,数据库的权限,模式的权限,对象的权限,表空间的权限 实例的权限:由pg_hba.conf文件控制,控制那些用户那些IP以哪种方式连接数据库 数据库的权限:是否允 ...

  8. 通过logmnr找到被修改前的存储过程

    1.找到存储过程被修改时的归档日志 SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME BETWEEN TO_DATE('20191118080000', ...

  9. TCP服务器程序

    Linux下编写TCP服务器调用的函数顺序为:socket -> bind -> listen -> accept -> recv/send socket 参见:http:// ...

  10. 转 15 jmeter分布式性能测试

    15 jmeter分布式性能测试   背景由于jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起Java内存溢出的错误.要解决这个问题, ...