nginx7层负载均衡相关部署

一、nginx简单介绍

1)什么是nginx?

Nginx是一个 轻量级/高性能的反向代理Web服务器,他实现非常高效的反向代理、负载均衡,可以处理2-3万并发连接数,官方监测能支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、 腾讯等。

2)为什么要用Nginx?

1.跨平台、配置简单、反向代理、高并发连接:处理2-3万并发连接数,官方监测能支持5万并发,内存消耗小:开启10个nginx才占150M内存 ,nginx处理静态文件好,耗费内存少

2.而且Nginx内置的健康检测功能:如果有一个服务器宕机,会做一个健康检查,再发送的请求就不会发送到宕机的服务器了。重新将请求提交到其他的节点上。

3.使用Nginx的话还能:

1、节省宽带:支持GZIP压缩,可以添加浏览器本地缓存

2、稳定性高:宕机的概率非常小

3、接收用户请求是异步的

3)Nginx应用场景?

1.http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

2.反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。

3.虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。

4.nginx 中也可以配置安全管理、比如可以使用Nginx搭建API接口网关,对每个接口服务进行拦截。

二、nginx部署

基本环境:centos7.6

三部曲configure–>makefile–>make install

nginx网上寻找该开源软件挺多

安装nginx所需相关依赖,不然后期会提醒缺少依赖

[root@server1 ~]# yum install -y gcc pcre-devel openssl-devel

并解压nginx,进入目录,在配置文件里面把debug模式禁掉,这样快一些并且内存占用小。

[root@server1 ~]# tar zxf nginx-1.22.0.tar.gz
[root@server1 ~]# cd nginx-1.22.0/
[root@server1 nginx-1.22.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #禁用debug

编译安装

[root@server1 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[root@server1 nginx-1.22.0]# make
[root@server1 nginx-1.22.0]# make install
[root@server1 nginx-1.22.0]# cd /usr/local/nginx
[root@server1 nginx]# ls
conf html logs sbin

./nginx开启服务。netstat -antlp可查看端口,80端口。

每次开启或者关闭服务都需要进入指定目录,不方便,关闭服务,建立软链接以开启全局nginx。

制作软连接

[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server1 ~]# nginx -t #检测语法
[root@server1 ~]# nginx #启动服务
[root@server1 ~]# nginx -s reload #重载服务
[root@server1 ~]# nginx -s stop #停止服务

nginx开机自启

确认关闭nginx服务,编辑nginx服务文件,然后刷新服务列表,开启服务。

[root@server1 ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl enable --now nginx

使用curl命令进行测试,能够访问通畅

三、nginx升级回滚

实验:nginx-1.22.0到nginx-1.22.1并回退。

Nginx如何做到热部署(平滑升级回滚)?

修改配置文件nginx.conf后,重新生成新的worker进程,当然会以新的配置进行处理请求,而且新的请求必须都交给新的worker进程,至于老的worker进程,等把那些以前的请求处理完毕后,kill掉即可。

基本步骤:

1 编译安装新旧版本

2 启动旧版本nginx

3 版本升级其实就是针对二进制文件的升级

解压tar包,进入目录,禁用debug,编译,make之后不要make install

[root@server1 ~]# tar zxf nginx-1.22.1.tar.gz
[root@server1 ~]# cd nginx-1.22.1/
[root@server1 nginx-1.22.1]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
[root@server1 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[root@server1 nginx-1.22.1]# make

在升级之前备份原程序

[root@server1 nginx-1.22.1]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@server1 nginx-1.22.1]# \cp -f objs/nginx /usr/local/nginx/sbin/
[root@server1 nginx-1.22.1]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ll
total 1896
-rwxr-xr-x 1 root root 967560 Dec 17 09:11 nginx
-rwxr-xr-x 1 root root 967560 Dec 17 09:10 nginx.bak

查看当前nginx pid

[root@server1 sbin]# ps ax |grep nginx
7767 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
7768 ? S 0:00 nginx: worker process

升级新程序

[root@server1 sbin]# kill -USR2 7767
[root@server1 sbin]# ps ax|grep nginx
7767 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
7768 ? S 0:00 nginx: worker process
10572 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10573 ? S 0:00 nginx: worker process

此时访问还是老版本

关闭原worker进程,保留主进程

[root@server1 sbin]# kill -WINCH 7767
[root@server1 sbin]# ps ax|grep nginx
7767 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10572 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10573 ? S 0:00 nginx: worker process

升级成功

如果新版本并没有旧版本用着顺手,那么关闭worker进程但保留主进程就是为了回退,即就是关闭工作端worker,保留master,回退的过程是相反的,先还原nginx程序,唤醒原进程,回收新版本,并且关闭。

版本回退:

[root@server1 sbin]# \cp -f nginx.bak nginx
[root@server1 sbin]# kill -HUP 7767
[root@server1 sbin]# ps ax|grep nginx
7767 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10572 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10573 ? S 0:00 nginx: worker process
10618 ? S 0:00 nginx: worker process

老版本进程已经拉起来,但访问还是新版本

关闭进程,成功回退。

[root@server1 sbin]# kill -WINCH 10572
[root@server1 sbin]# ps ax|grep nginx
7767 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10572 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
10618 ? S 0:00 nginx: worker process

四、nginx常规配置

修改nginx运行用户

[root@server1 conf]# useradd -M -d /usr/local/nginx -s /sbin/nologin nginx
[root@server1 conf]# vim nginx.conf
user nginx;
[root@server1 conf]# systemctl reload nginx

修改nginx工作进程数

[root@server1 conf]# vim nginx.conf
worker_processes 2; #推荐和本机的cpu核数保持一致
worker_cpu_affinity 01 10; #nginx进程与cpu核绑定
[root@server1 conf]# systemctl reload nginx

修改nginx并发连接数

[root@server1 conf]# vim nginx.conf
events {
worker_connections 65535;
}

修改系统限制

[root@server1 conf]# vim /etc/security/limits.conf
ginx - nofile 65535
[root@server1 conf]# sysctl fs.file-max #内核限制一般不需要调整,和本机内存大小有关
fs.file-max = 197384

五、nginx反向代理负载均衡

反向代理:指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,简单来说就是真实的服务器不能直接被外部网络访问,想要访问必须通过代理。

作用

1、防止主服务器被恶意攻击

2、为负载均衡和动静分离提供实现支持

负载均衡:使用反向代理同时代理多个相同内容的应用服务器(比如tomcat),将客户端请求分发到各个应用服务器上并接收响应返回给客户端

负载均衡的作用:当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃。为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力。我们可以建立很多很多服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器,在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此一来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。

nginx负载均衡,是通过增加服务器的数量,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们所说的负载均衡.

默认轮询

编辑nginx配置文件 upstream轮询,定义两个server端,在http{}语句块中添加 upstream{}

[root@server1 conf]# vim nginx.conf
http {
upstream backend { #添加后端server列表
server server2 weight=2; #默认使用Round Robin调度算法,可以设置权重
server server3;
server 127.0.0.1 backup; #backup当所有server全部故障后启用
}
...
server { #定义虚拟主机
listen 80;
server_name myapp.westos.org;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://backend; #所有请求转发到backend,实现负载均衡
}
}
}
[root@server1 conf]# systemctl reload nginx

在测试机上,添加域名解析并访问10次

[root@test ~]# for i in {1..10}; do curl myapp.westos.org;done
server2
server3
server2
server2
server3
server2
server2
server3
server2
server2

ip_hash算法

请求发送到的服务器由客户端 IP 地址确定。在这种情况下,要么使用 IPv4 地址的前三个八位字节,要么使用整个 IPv6 地址来计算哈希值。该方法保证来自同一地址的请求到达同一服务器,除非它不可用。 应用场景:保持session 会话一至性。源地址不变,则后端调度不变,同一个ip过来的请求会发往同一个后端,不会改变。

ip_hash是基于round robin的,判断后端是否可用的方法是一样的。

ip_hash对后端做健康检测,如果server3出问题,则调度server2。如果后端全挂,则http报错502

[root@server1 conf]# vim nginx.conf
upstream backend {
ip_hash;
server server2;
server server3;
#server 127.0.0.1 backup;
}
[root@server1 conf]# systemctl reload nginx
[root@harbor ~]# for i in {1..10}; do curl myapp.westos.org;done
server3
server3
server3
server3
server3
server3
server3
server3
server3
server3
同一个客户端ip的请求会转发到同一个后端服务器处理

sticky算法-nginx扩展模块

sticky算法:是nginx的一个模块,它是基于cookie(状态信息)的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上 写法:直接在upstream字段内第一行写入 sticky; weight 权重参数:加权轮巡,权重值越高就越优先访问 写法:在server ip 后面加上weight=1; 1代表权重值为1

默认nginx plus支持该算法,开源版本需要自行编译

[root@server1 ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
[root@server1 ~]# cd nginx-1.22.1/
[root@server1 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
[root@server1 nginx-1.22.1]# make
不要执行make install
[root@server1 conf]# systemctl stop nginx
[root@server1 nginx-1.22.1]# \cp -f objs/nginx /usr/local/nginx/sbin/
[root@server1 nginx-1.22.1]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
upstream backend {
#ip_hash;
sticky;
server server2;
server server3;
#server 127.0.0.1 backup;
}
[root@server1 conf]# systemctl start nginx

使用浏览器测试

六、安全控制

限制并发连接数

[root@server1 conf]# vim nginx.conf
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
...
server {
...
location / {
root html;
index index.html index.htm;
#proxy_pass http://backend;
}
location /download/ {
limit_conn addr 1; #限制并发数
}
}
[root@server1 conf]# systemctl reload nginx

超出的并发请求会失败

同理,限制每秒请求数,排队,超过指定数量则排队访问,无延迟相关设置如下。

[root@server1 conf]# vim nginx.conf
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #限制请求速率,每秒1次
...
server {
location / {
limit_req zone=one; #客户端超出限制的请求都会失败,日志中会显示503
# limit_req zone=one burst=5 #过多的请求将会排队
# limit_req zone=one burst=5 nodelay; #突发流量不需要延迟
}
}

限制带宽

现实生活中,我们下载东西时,会被限制网速,其实就是限制带宽。

在配置文件中设定带宽50k,重启服务

location /download/ {
limit_conn addr 1;
limit_rate 50k;
}

自动索引

自动索引到相关目录,方便下载相关文件

location /download/ {
autoindex on;
}

七、web server

日志截断

数据量激增下,每天产生大量日志,占用服务器内存,可定时清理缓解内存消耗。

[root@server1 conf]# vim /opt/nginxlog.sh
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
[root@server1 conf]# chmod +x /opt/nginxlog.sh
使用crontab调用脚本
[root@server1 conf]# crontab -e
00 00 * * * /opt/nginxlog.sh

日志可视化

采用goaccess 日志可视化:一款可视化web日志监控工具,够为需要动态可视服务器报告的系统管理员提供快速且有价值的 HTTP 统计信息,主要优点:快速、实时、具有美观的页面,几乎支持所有的web日志格式

安装软件依赖性

yum install -y GeoIP-devel-1.5.0-13.el7.x86_64.rpm
yum install ncurses-devel

编译安装

tar zxf goaccess-1.4.tar.gz
cd goaccess-1.4/
./configure --enable-utf8 --enable-geoip=legacy
make
make install

在后台运行goaccess

goaccess /usr/local/nginx/logs/access.log -o /usr/local/nginx/html/report.html --log-format=COMBINED --real-time-html &

对nginx压测,日志会动态更新

ab -n 100 -c 10 http://192.168.56.170/index.html

nginx常规设置

charset utf-8; #默认字符集定义,解决中文乱码
location ~ .*\.(gif|jpg|png)$ {
access_log off; #禁用日志记录
root html;
allow 192.168.56.0/24; #运行访问网段
deny all; #禁止所有访问
}

重定向

使用 Nginx 的重定向功能时,除了可以重定向到新域名,还可以将请求重定向到特定的协议上。

拒绝访问,报错500

1.切入配置目录,编辑配置文件,设定在访问本机时,返回500,重启服务。此时使用curl命令访问本机,会显示http报错500

server {
listen 80;
server_name localhost;
return 500; #通过ip访问的全部返回500错误码

2.编辑配置文件,设定将所有访问请求重定向至指定域名,重启服务。此时使用curl命令访问本机,会显示访问地址为

rewrite ^(.*) http://myapp.westos.org permanent;
  1. 端口重定向,将80定向到443

HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

https配置

server {
listen 443 ssl;
server_name myapp.westos.org;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /myapp;
index index.html index.htm;
}
}
server {
listen 80;
server_name myapp.westos.org;
rewrite ^/(.*)$ https://myapp.westos.org/$1 permanent;
..}

创建证书

重启服务并查看端口

[root@server1 conf]# systemctl reload nginx.service
[root@server1 ~]# netstat -antlp|grep :443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 8757/nginx: master

看到https重定向成功。

防盗链

server {
listen 80;
server_name myapp.westos.org;
#rewrite ^/(.*)$ https://myapp.westos.org/$1 permanent;
#rewrite ^/bbs$ http://bbs.westos.org permanent;
#rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;
location / {
root html;
index index.html index.htm;
#proxy_pass http://backend;
}
location ~ \.(jpg|png)$ {
#access_log off;
valid_referers none blocked myapp.westos.org; #myapp.westos.org正常访问
if ($invalid_referer) {
return 403; #非myapp.westos.org调用返回403错误码
#rewrite ^/ http://bbs.westos.org/daolian.jpg; #也可以重定向到指定链接
}
}
}

盗链网站设置,放张图

[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html
<html>
<body>
<img src="http://myapp.westos.org/download/vim.jpg">
</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值