nginx

实验环境

(1)四台主机

  • 调度机 ----> rhel7.6
  • 访问机 ----> server1 server2
  • 测试机 ----> checkhost

(2)关闭火墙服务
(3)四台主机之间可以相互通信


1.nginx

Nginx同Apache一样都是一种WEB服务器。基于REST架构风格,以统一资源描述符(UniformResources ldentifier)URl或者统一资源定位符(Uniform Resources Locator)URL作为沟通依据,通过HTTP协议提供各种网络服务。

Apache的发展时期很长,而且是毫无争议的世界第一大服务器。它有着很多优点:稳定、开源、跨平台等等。它出现的时间太长了,它兴起的年代,互联网产业远远比不上现在。所以它被设计为一个重量级的。它不支持高并发的服务器。在Apache上运行数以万计的并发访问,会导致服务器消耗大量内存sz操作系统对其进行进程或线程间的切换也消耗了大量的CPU资源,导致HTTP请求的平均响应速度降低。
这些都决定了Apache不可能成为高性能WEB服务器,轻量级高并发服务器Nginx就应运而生了。

特点:

  • 是一个高性能的HTTP和反向代理web服务器,轻量级提供了IMAP/POP3/SMTP服务
  • 发布于2004年10月4日(第一个公开版本0.1.0 )Nginx的1.4.0稳定版已经于2013年4月24日发布C语言编写
  • Nginx是一个跨平台服务器
  • Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。
    优势:
  • 占有内存少(在3W并发连接中,开启的10个nginx进程消耗内存大约150M)
  • 高并发能力强(官方测试能够支撑5W并发连接,在实际生产环境中能到2-3W并发连接数)简单(配置文件通俗易懂)
  • 价格(免费、开源)
  • 支持Rewriter重写(能够根据域名、URL的不同,将HTTP请:求分到不同的后端服务器群组)内置健康检查(如果nginx后端有几个服务宕机了,不会影响前端访问,能自动检测服务状态)节省带宽(支持GZIP压缩,可以添加浏览器本地缓存的Header头)
    稳定性高,反向代理,很少宕机
  • 中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
    功能:
    web服务器、轻量级;负载、均衡;缓存;高并发
    应用场景:
    代理服务器;IP负载、静态负载;动静分离;限流、健康监控

2.nginx的编译及优化

curl -I www.service.com

在这里插入图片描述


资源下载:
nginx-1.21.6.tar
:https://nginx.org/en/download.html


(1) nginx的环境部署及编译

tar axf nginx-1.21.6.tar.gz
cd nginx-1.21.6/
yum install gcc -y
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

在这里插入图片描述
在这里插入图片描述

yum install pcre-devel -y
yum install openssl-devel -y
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

cd
cd /usr/local/nginx/
du -sh

在这里插入图片描述

cd sbin/
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
nginx
netstat -ant
curl localhost

在这里插入图片描述

(2) nginx环境部署二次优化

nginx -s stop
cd
cd /usr/local/nginx/
cd ..
rm -rf nginx/

在这里插入图片描述

cd
cd nginx-1.21.6/
vim auto/cc/gcc
更改:

        #CFLAGS="$CFLAGS -g"

在这里插入图片描述

make clean
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
cd /usr/local/nginx/
du -sh

在这里插入图片描述

cd sbin/
nginx
curl localhost
nginx -s reload

在这里插入图片描述

(3)nginx的启动与停止

nginx                   启动
nginx -s stop           停止
nginx -s reload         重启
nginx -t                检测语法错误

(4) nginx的负载均衡

cd nginx-1.21.6
cd conf/
vim nginx.conf
更改:
http {
       upstream westos
         server 192.168.0.65:80;
         server 192.168.0.75:80;

location / {
       #    root   html;
       #    index  index.html index.htm;
           proxy_pass http://westos;
}

nginx -s reload

在这里插入图片描述
在这里插入图片描述


在访问机上:

yum install httpd -y
systemctl disable --now firewalld
systemctl start httpd
在server1主机上:
echo server1 > /var/www/html/index.html
在server2主机上:
echo server2 > /var/www/html/index.htmml

测试:

curl 192.168.0.55

在这里插入图片描述


2.nginx并发优化

• Linux下高并发socket最大连接数所受的各种限制
• 修改用户进程可打开文件数限制

 /etc/security/limits.conf
 nginx - nofile 65535

• Linux系统级的最大打开文件数限制

fs.file-max = 188414 //和内存容量相关

• 修改网络内核对TCP连接的有关限制

 net.ipv4.ip_local_port_range = 1024 65535

• 限制接收新 TCP 连接侦听队列的大小

net.core.somaxconn = 2048

• 启用tcp连接timewait快速回收和重用

net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
worker_processes //工作进程数
 worker_connections //单个工作进程并发连接数

• nginx作为http服务器时:

 max_clients = worker_processes * worker_connections

• nginx作为反向代理服务器时:

max_clients = worker_processes * worker_connections / 2

• worker_processes //将其设置为可用的CPU内核数将是一个好的开始
• worker_rlimit_nofile //worker进程的最大打开文件数限制。如果没设置的
话,这个值为操作系统的限制。
• worker_processes 2; //最多开启8个worker_cpu_affinity
01 10; //cpu有多少个核,就有几位数,1代表内核开启,0代表内核关闭
• multi_accept on; //告诉nginx收到一个新连接通知后接受尽可能多的连接
• use epoll; //使用epoll模型
• 开启文件高效传输模式,同时设置tcp_nopush 和tcp_nodelay 为on,可以防止网路和磁盘IO阻塞。

• sendfile on;
• tcp_nopush on; 
• tcp_nodelay on;

cd /usr/local/nginx/conf/
useradd -M -d /usr/local/nginx/ -s /sbin/nologin nginx
vim nginx.conf
更改:
  2 user  nginx nginx;
  3 worker_processes  4;
  4 worker_cpu_affinity 0001 0010 0100 1000;
  12 events {
 13     worker_connections  65535;
 14 }
 nginx -s reload

在这里插入图片描述
在这里插入图片描述

vim /etc/security/limits.conf
写入:
nginx - nofile 65535

在这里插入图片描述


3.nginx的平滑升级

nginx-1.21.6 ----> nginx-1.22.0
下载 nginx-1.22.0.tar.gz:
https://nginx.org/en/download.html
• 下载nginx新版本软件,正常执行./configure 和make 但不要执行make install

tar zxf nginx-1.22.0.tar.gz
cd nginx-1.22.0/
vim auto/cc/gcc
更改:
172 #CFLAGS="$CFLAGS -g"        关闭dubug
nginx -s reload
 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make             
升级编译过程中不能进行"make install"
cd objs/
./nginx -v               查看当前路径nginx版本
  • 备份原程序:
cd /usr/local/nginx/sbin/
cp nginx nginx.bak
  • 拷贝新程序:
cd nginx-1.22.0/
cd objs/
\cp -f nginx /usr/local/nginx/sbin/nginx

• 获取当前nginx主进程pid:

ps ax|grep nginx
  • 升级新程序:
kill -USR2 52367
ps ax|grep nginx

• 关闭原worker进程但保留主进程:为了回退

kill -WINCH 52367
ps ax|grep nginx
  • 查看当前nginx版本,如为nginx-1.22.0版本,则升级成功
curl localhost -I

• 版本回退:

cp -f nginx.old nginx         还原nginx程序
kill -HUP 52367               唤醒原进程
kill -WINCH 29761             回收新版本的worker进程
kill -QUIT 29761              关闭新版本主进程

4.nginx 限流

(1)增加权重

增加权重的服务器,调度器主机会将客户端访问定向次数增加相对应的权重次数

cd /usr/local/nginx/conf/
vim nginx.conf
更改:
 17 http {
 18       upstream westos {
 19         server 192.168.0.65:80;  weight=2
 20         server 192.168.0.75:80;
 nginx -s reload

在这里插入图片描述

测试:

for i in {1..10}; do curl 192.168.0.55;done     测试访问主机的次数

在这里插入图片描述


(2) backup

如果另外两台服务器未能连接时,调度器主机会把访问定向到本机backup

yum install httpd -y    安装httpd服务
vim /etc/httpd/conf/httpd.conf
更改:
listen 8080                  修改httpd服务监听端口为8080
systemctl staret httpd      开启httpd服务
cd /usr/local/nginx/conf/
vim nginx.conf:
 17 http {
 18       upstream westos {
 19         server 192.168.0.65:80  weight=2;
 20         server 192.168.0.75:80;
 21         server 127.0.0.1:8080 backup;
          }                      添加本机"backup "服务端口为8080端口
nginx -s reload

在访问机上:

systemctl stop httpd         在server1和server2主机上关闭httpd服务        

在调度机(192.168.0.55)主机上:

echo rhel7.6 back up > /var/www/html/index.html
测试:
curl 192.168.0.55

在这里插入图片描述


(3) ip_hash

如果客户端ip地址不变,将会把所有的访问定向到一个服务器端

cd /usr/local/nginx/conf/
vim nginx.conf
写入:

在这里插入图片描述

nginx -s reload

测试:

curl 192.168.0.55

在这里插入图片描述


(4) sticky

基于浏览器的cookie进行调度方式;
适用于cdn反向代理;

  • 重新编译含有strick源码的nginx
nginx -s stop
cd nginx-1.21.6/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
make
cd objs/
make clean
\cp -f nginx /usr/local/nginx/sbin/    将编译完成的nginxf复制到/usr/local/nginx/sbin/ 下
nginx
cd /usr/local/nginx/conf
vim nginx.conf
写入:

在这里插入图片描述

nginx -s reload

测试:
在浏览器上:

http://192.168.0.55

在这里插入图片描述


(5) nginx限流

nginx并发限制

cd /usr/local/nginx/conf
vim nginx.conf
写入:

在这里插入图片描述
在这里插入图片描述

nginx -s reload
  • 在nginxhtml目录中创建download目录
cd html/
mkdir download

在download目录中上传一张图片;

在远程主机(checkhost):

ab -n 10 -c 10 http://192.168.0.55/download/vim.jpg

在这里插入图片描述


nginx限制请求数量

cd /usr/local/nginx/
vim nginx.conf

写入:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
limit_req zone=one;

单线程且限制请求数为一次
在这里插入图片描述
在这里插入图片描述

nginx -s reoad

测试:

ab -n 10 -c 10 http://192.168.0.55/download/vim.jpg

在这里插入图片描述


nginx队列排序

cd /usr/local/nginx/
vim nginx.conf

写入:

limit_rep zone=one burst=5; nodelay;

通过队列排序的方法,只完成允许五次请求
在后面加 nodelay; 指请求不延迟
在这里插入图片描述

nginx -s reload
cat ../logs/access.log    可通过查看日志排查错误

nginx限制带宽

cd /usr/local/nginx/
vim nginx.conf

写入:

limit_rate 50k;

在这里插入图片描述

nginx -s reload

测试:

ab -n 10 -c 10 http://192.168.0.55/download/vim.jpg

在这里插入图片描述


(7)nginx配置管理

• 自动索引

下载方便

cd /usr/local/nginx/
vim nginx.conf

写入:

location / {
 autoindex on;
}

在这里插入图片描述


• Nginx expire缓存配置

:缓存可以降低网站带宽,加速用户访问

location ~ .*\.(gif|jpg|png)$ {
 expires 365d;
 root /www;

在这里插入图片描述

nginx -s reload

测试:

curl -I 192.168.0.55/download/vim.jpg

在这里插入图片描述


•日志轮询

禁用不必要的日志记录,以节省磁盘IO的消耗
vim nginx.conf
写入:

location ~ .*\.(js|jpg|jpeg|png|css|bmp|gif)$ {
access_log off;
}

•站点目录和文件的限制

location ~ ^/images/.*\.(sh|php)$ {
 deny all;
 }

•中文乱码

charset utf-8;

• 限制IP

location / {
deny 172.25.0.10;
allow 172.25.0.0/24;
deny all;
}
if ($remote_addr = 172.25.0.254) {
return 403;
}

(8) nginx重定向

•防止域名恶意解析到服务器IP:

server { 
listen 80; 
server_name _; 
return 500; 
} 

• 也可以重定向:

server { 
listen 80; 
server_name _; 
rewrite ^(.*) http://www.westos.org permanent; 
}

• 80重定向443:

server {
listen 80;
server_name www.westos.org;
rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
}

• www.westos.org/bbs 重定向bbs.westos.org:

rewrite ^/bbs$ http://bbs.westos.org permanent;
rewrite ^/bbs/(.*)$ http://bbs.westos.org/$1 permanent;

• bbs.westos.org 重定向www.westos.org/bbs:

if ($host = "bbs.westos.org"){
rewrite ^/(.*)$ http://www.westos.org/bbs/$1 permanent;
}

(9) nginx防盗链

location ~ \.(jpg|png)$ {
 valid_referers none blocked www.westos.org;
 if ($invalid_referer) {
 return 403;
 #rewrite ^/ http://www2.westos.org/daolian.jpg;
 }
 }
 

(10)PHP源码编译

• 软件下载:https://www.php.net/
• 软件编译:
• # tar jxf php-7.4.6.tar.bz2
• # cd php-7.4.6
• # ./configure --prefix=/usr/local/lnmp/php --with-config-filepath=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpmgroup=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl –
enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enablesockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp –
enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
• # make
• # make install
PHP源码编译
• 拷贝php-fpm配置文件:
• cp php.ini-production /usr/local/lnmp/php/etc/php.ini
• cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
• cp sapi/fpm/php-fpm.conf /usr/local/lnmp/php/etc/
• cp sapi/fpm/www.conf /usr/local/lnmp/php/etc/php-fpm.d/
• 修改php-fpm启动文件:
• # vim /usr/lib/systemd/system/php-fpm.service
ProtectSystem=false //默认php-fpm启动时只读挂载/usr目录
• # systemctl daemon-reload
• # systemctl start php-fpm
nginx结合php-fpm
• 修改nginx配置文件:
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}


(11)php添加memcache功能模块

• 软件下载:http://pecl.php.net/package/memcache
• 软件安装:
• # tar zxf memcache-4.0.5.2.tgz
• # cd memcache-4.0.5.2
• # phpize
• # ./configure
• # make && make install • 配置php加载模块:
• # vim php.ini
extension=memcache.so
• # systemctl reload php-fpm
• # php -m |grep memcache
memcache


(12)构建nginx高速缓存

• 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
传统缓存策略
高效缓存策略
• 软件下载: https://openresty.org/cn/ • 软件安装:
• # wget https://openresty.org/package/rhel/openresty.repo
• # mv openresty.repo /etc/yum.repos.d/
• # yum install -y openresty
• 软件配置:
• # cd /usr/local/openresty/nginx/conf
• # vim nginx.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512; //保持512个不立即关闭的连接用于提升性能
}
location /memc {
internal; //表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key q u e r y s t r i n g ; / / 使用内置的 query_string; //使用内置的 querystring;//使用内置的query_string来作为key
set KaTeX parse error: Expected 'EOF', got '}' at position 52: …ass memcache; }̲ location ~ \.p… {
set $key u r i uri uriargs;
srcache_fetch GET /memc $key;
srcache_store PUT /memc KaTeX parse error: Expected 'EOF', got '#' at position 75: …ex index.php; #̲fastcgi_param S…fastcgi_script_name;
include fastcgi.conf;
}
当所请求的uri以“.php”结尾时,首先到memcache中查询有没有以 u r i uri uriargs为key的数据,如
果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以
u r i uri uriargs为key,将输入结果存入memcache。
nginx结合lua
location @client {
proxy_pass http://172.25.0.3;
}
location ~ .php$ {
default_type text/html;
content_by_lua ‘ngx.say(“this is westos.org”)’;
access_by_lua ’
if ngx.var.remote_addr == “172.25.0.250” then
ngx.exec(“@client”)
end
';
}


(13)tomcat结合memcache

• 软件下载:https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/ • 拷贝jar包到/usr/local/tomcat/lib目录:
• 配置tomcat:
• # vim /usr/local/tomcat/conf/context.xml


(14)goaccess 日志可视化

• 软件安装:
• # wget https://tar.goaccess.io/goaccess-1.4.tar.gz
• # tar -xzvf goaccess-1.4.tar.gz
• # cd goaccess-1.4/
• # ./configure --enable-utf8 --enable-geoip=legacy
• # make
• # make install
• 软件使用:
• # goaccess access.log -o /usr/local/nginx/html/report.html --logformat=COMBINED --real-time-html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值