nginx部署基于http负载均衡器
标签:
nginx跨多个应用程序实例的负载平衡是一种用于优化资源利用率,最大化吞吐量,,减少延迟和确保容错配置的常用技术。 环境介绍配置nginx负载均衡器因会用到多台服务器来进行,所以下面我会用到docker,具体docker的使用请移步docker实战
系统环境: [email protected]:~# lsb_release -a #查看系统版本 No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 19.10 Release: 19.10 Codename: eoan [email protected]:~# uname -a #查看系统是多少位 Linux ubuntu 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
docker版本和相关操作系统版本 docker版本: [email protected]:~# docker --version #查看docker版本 Docker version 19.03.3, build a872fc2f86 操作系统版本: [[email protected] /]# cat /etc/redhat-release #查看系统版本 CentOS Linux release 8.0.1905 (Core) [[email protected] /]# uname -a Linux 57b669db1de1 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
软件版本: nginx版本: [email protected]:~# nginx -v 查看nginx版本 nginx version: nginx/1.16.1 (Ubuntu) docker中nginx版本: [[email protected] /]# nginx -v #查看nginx版本 nginx version: nginx/1.14.1
安装nginx具体nginx的安装请参考nginx安装部署
安装依赖软件 更新软件包: [email protected]:~# apt-get update 安装依赖软件: [email protected]:~# apt -y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc
编译安装nginx 下载nginx: [email protected]:~# wget http:#nginx.org/download/nginx-1.17.6.tar.gz 解压: [email protected]:/opt# tar zxf nginx-1.17.6.tar.gz [email protected]:/opt# cd nginx-1.17.6/ [email protected]:/opt/nginx-1.17.6# ls #列出目录 auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [email protected]:/opt/nginx-1.17.6# 配置: [email protected]:/opt/nginx-1.17.6# ./configure --prefix=http://www.mamicode.com/usr/local/nginx 编译以及部署: [email protected]:/opt/nginx-1.17.6# make && make install [email protected]:/opt/nginx-1.17.6# cd /usr/local/nginx/ [email protected]:/usr/local/nginx# ls conf html logs sbin 启动: [email protected]:/usr/local/nginx# ln sbin/nginx /usr/local/sbin/ #创建nginx软连接,设置为命令 [email protected]:/usr/local/nginx# nginx #启动nginx [email protected]:/usr/local/nginx# netstat -anpl | grep nginx #查看nginx端口是否开启 [email protected]:/usr/local/nginx# lsof -i:80 #查看80端口是否开启 docker安装nginx
创建容器 下载镜像: [email protected]:~# docker pull registry.cn-beijing.aliyuncs.com/blxt/centos 创建容器并启动: [email protected]:~# [email protected]:~# docker run -itd --name nginx1 --privileged registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init [email protected]:~# docker ps -a #查看容器是否创建并启动 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME 6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 5 minutes ago Up 5 minutes nginx1 相同方法创建第二个容器: [email protected]:~# docker run -itd --name nginx2 --privileged registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init [email protected]:~# docker ps -a [email protected]:~# [email protected]:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d31d3fc0ec1 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 4 minutes ago Up 4 minutes nginx2 6801d55682a3 registry.cn-beijing.aliyuncs.com/blxt/centos "/sbin/init" 5 minutes ago Up 5 minutes nginx1 连接到容器: [email protected]:~# docker exec -it nginx1 /bin/bash [email protected]:~# docker exec -it nginx2 /bin/bash [[email protected] /]# hostname nginx1 #更改主机名称 [[email protected] /]# bash #使更改的名称生效 [[email protected] /]# hostname nginx2 [[email protected] /]# bash
安装nginx 更新软件包: [[email protected] /]# yum clean all #清空缓存 [[email protected] /]# yum makecache #更新软件包 安装nginx: [[email protected] /]# yum -y install nginx [[email protected] /]# rpm -qa | grep nginx #查看是否已经安装 启动nginx: [[email protected] /]# systemctl start nginx [[email protected] /]# netstat -anpl | grep nginx #查看nginxnn端口是否开启 [[email protected] /]# lsof -i:80 #查看80端口是否开启 安装第二个nginx使用相同方法即可!此处省略!!! 访问nginx: [[email protected] /]# curl 127.0.0.1
修改网页内容 查看网页存在目录: [[email protected] nginx]# more nginx.conf #查看配置文件 在配置文件中找到下面root行,后面目录即网页文件存放目录 root /usr/share/nginx/html; [[email protected] html]# cd /usr/share/nginx/html/ [[email protected] html]# ls #列出目录内容 404.html 50x.html index.html nginx-logo.png poweredby.png [[email protected] html]# echo nginx1 > index.html #清空nginx主页文件内容,并重新写入内容为nginx1 nginx2如同,注意不要设置一样的主页内容 访问nginx: [[email protected] html]# curl 127.0.0.1 nginx1 [[email protected] ~]# curl 127.0.0.1 nginx2
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/40145.html