Nginx一个具有高性能的HTTP和反向代理的WEB服务器,同时也是一个POP3/SMTP/IMAP代理服务器。
1.架构/集群中的定位角度(各司其职)
2.部署方式:YUM,rpm,手工编译安装
3.切入点:模块功能
静态处理(模块功能)
跳转后端(模块功能)
为了更好的实现以上功能,会不会有其它的一些功能来参与围绕着更好的静态处理或者(延申)能够更好的应对并发问题
一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30000~50000个并发请求
NG并发连接能力受以下二个因素的影响:
1.CPU个数
2.本地物理服务器系统的最大文件打开数
静态服务器(图片,视频服务)
动态服务
反向代理,负载均衡
缓存服务
nginx的http七层代理,其实它工作在OSI七层模型的应用层。由于其可以解析http协议,我们可以根据URI进行请求的分发,具有很大的灵活性,但是协议的解析存在性能的消耗。为了能获取更高的代理性能,nginx(nginx-1.18和nginx-1.21版本)支持了四层代理,即传输层,就是我们常说的TCP/UDP层,没有协议解析,就是简单的TCP/UDP转发,代理性能突飞猛进,该功能依赖于ngx_http_upstream_module和ngx_stream_upstream_module,互联网公司将其作为入口代理来工作。
反向代理是指用户不需要知道目标服务器的地址,也不需要在用户端做任何设定,可以直接通过访问反向代理服务器来获得目标服务器的资源。反向代理提高了内部服务器的安全,加快了对内部服务器的访问速度。
反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。
同时,用户不需要知道目标服务器的地址,也无须在用户端做任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。
提高了内部服务器的安全
加快了对内部服务器的访问速度
节约了有限的IP资源
[root@yzq opt]# systemctl stop firewalld
[root@yzq opt]# systemctl disable firewalld
[root@yzq opt]# setenforce 0
[root@yzq opt]# ls
nginx-1.15.9 nginx-1.15.9.tar.gz rh yum.sh yum.sh~
nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
[root@yzq opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@yzq opt]# yum -y install gcc gcc-c++ pcre-devel zlib-devel make
新增vts模块
[root@yzq opt]# cd nginx-1.15.9/
[root@yzq nginx-1.15.9]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --add-module=/usr/local/nginx-module-vts-master/
[root@yzq nginx-1.15.9]# make && make install
优化路径使Nginx服务器的运行更方便(创建软连接)
[root@yzq nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
创建Nginx用户
[root@yzq nginx-1.15.9]# useradd -M -s /sbin/nologin nginx
[root@yzq nginx-1.15.9]# nginx -t ##检查配置文件是否配置正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yzq nginx-1.15.9]# vim /usr/lib/systemd/system/nginx.service
[root@yzq nginx-1.15.9]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@yzq nginx-1.15.9]# chmod 754 /usr/lib/systemd/system/nginx.service
[root@yzq nginx-1.15.9]# systemctl start nginx.service
[root@yzq nginx-1.15.9]# systemctl enable nginx.service
每2秒查看nginx状态
[root@yzq nginx-1.15.9]# watch -n 2 systemctl status nginx
[root@yzq conf]# cp nginx.conf nginx.conf.bak
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_processes 1; #工作进程数量,一般为1,压力大时可配置成服务器内核数 * 2
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID 文件的位置
events { #event事件
use epoll; #使用 epoll 模型;epoll是一种抗高并发的参数之一(2.6及以上版本的系统内核,建议使用epoll模型以提高性能)
worker_connections 1024; #每个进程最多处理的连接数量(socket) ,上限为65535(内核中的优化):1024 个连接数
}
PS:两种修改方式,以下为临时
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.
ulimit -a 查看每个进程可处理文件数
友好的把可用选项显示在后面
http { #http协议的配置
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型
#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 logs/access.log main; #访问日志位置
sendfile on; ##支持文件发送(下载)默认开启
#tcp_nopush on; #此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#keepalive_timeout 0; ##连接保持超时时间,单位:秒
keepalive_timeout 65;
#gzip on; #压缩模块 on 表示开启(压缩发送,加快时间,节省资源)
server { #web 服务相关的一些配置
listen 80; #默认监听端口(不能和其它服务端口冲突)
server_name localhost; #站点域名
#charset koi8-r; #字符集支持(修改为中文)UTF-8
#access_log logs/host.access.log main; #此web服务(匹配此server)的主访问日志
location / { #匹配URL(其中路径) “/”根目录配置 (www.baidu.com/)
root html; #网站根目录的位置/usr/local/nginx/html(相对路径)
index index.html index.htm; #支持的首页文件格式
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #当发生错误的时候能够显示一个预定义的错误页面
location = /50x.html { #错误页面配置
root html;
}
流量监控
监控Nginx主要用到以下三个模块:
1、nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。
2、nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。
3、Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。
[root@yzq opt]# unzip nginx-module-vts-master.zip
[root@yzq opt]# mv nginx-module-vts-master /usr/local/
[root@yzq conf]# nginx -V #显示服务版本以及开启的模块
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/usr/local/nginx-module-vts-master/
显示已安装vst监控模块
编译安装nginx,优化管理已做过,下面直接修改配置文件
主要是修改默认日志文件格式,添加压缩配置,添加监控配置,具体可根据自己的需求修改
[root@yzq conf]# vim nginx.conf
在http中添加
在server中添加
查看配置是否成功,并重启nginx
[root@yzq conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yzq conf]# systemctl restart nginx.service
输入网址192.168.28.100/status监控
监控内容
Nginx支持的虚拟主机
基于域名的虚拟主机
基于IP的虚拟主机
基于端口的虚拟主机
通过 “server {}” 配置段实现
每个server类似于一个小型服务器
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则是从上往下执行,如匹配则停止,不会继续往下匹配
[root@yzq conf]# vim nginx.conf
[root@yzq conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
修改如下,使用nginx -t查看,修改没有问题
[root@yzq conf]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.28.100 www.ziqi.com www.yzq.com ##新添加域名
创建网页根目录与主页
[root@yzq conf]# mkdir -p /var/www/html/ziqi
[root@yzq conf]# mkdir -p /var/www/html/yzq
[root@yzq conf]# cd /var/www/html/
[root@yzq html]# ls
yzq ziqi
[root@yzq html]# echo "ziqi
" > ziqi/index.html
[root@yzq html]# echo "yzq
" > yzq/index.html
[root@yzq html]# ls *
yzq:
index.html
ziqi:
index.html
虚拟主机以server为单位,所以我们修改server中的配置文件
[root@yzq html]# vim /usr/local/nginx/conf/nginx.conf
修改后(两个修改一样,只是讲yzq换为ziqi)
修改成功,重启nginx
[root@yzq html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yzq html]# systemctl restart nginx.service
进入虚拟机查看,两个网址都能成功访问
修改一下文件内容加上端口号
[root@yzq html]# mv yzq/ yzq80
[root@yzq html]# mv ziqi/ ziqi8080
[root@yzq html]# ls
yzq80 ziqi8080
[root@yzq yzq80]# vim index.html
[root@yzq yzq80]# cat index.html
<h1>yzq80</h1>
[root@yzq ziqi8080]# vim index.html
[root@yzq ziqi8080]# cat index.html
<h1>ziqi8080</h1>
[root@yzq conf]# vim /usr/local/nginx/conf/nginx.conf
修改如下
修改无错误,重启nginx
[root@yzq conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@yzq conf]# systemctl restart nginx.service
访问网页(nginx默认80端口所以80端口不用加上端口号也能访问,但是8080不可以,需要加上端口号,如下)
创建yyy目录
[root@yzq conf]# ls /var/www/html/
yyy yzq80 ziqi8080
创建内容
[root@yzq conf]# cat /var/www/html/yyy/index.html
<h1>this is yyy</h1>
修改并保存配置文件,重启nginx
[root@yzq conf]# vim nginx.conf
[root@yzq conf]# systemctl restart nginx.service
同一端口,两个不同IP均可使用