Nginx 简介
Nginx (“engine x”) 是一个开源的,支持高性能、高并发的 Web 服务和代理服务软件。它是由俄罗斯人 Igor Sysoev 开发的,最初被应用在俄罗斯的大型网站 www.rambler.ru 上。后来作者将源代码以类 BSD 许可的形式开源出来供全球使用。
Nginx 的官方介绍见 http://nginx.org/en/
Nginx 中文介绍见 http://tengine.taobao.org/nginx_docs/cn/
Nginx 的重要特性
Nginx 基本特性
- 可针对静态资源高速高并发访问及缓存。
- 可使用反向代理加速,并且可进行数据缓存。
- 具有简单负载均衡、节点健康检查和容错功能。
- 支持远程 FastCGI 服务的缓存加速。
- 支持 FastCGI、Uwsgi、SCGI、Memcached Servers 的加速和缓存。
- 支持SSL、TLS、SNI。
- 具有模块化的架构:过滤器包括 gzip 压缩、ranges 支持、chunked 响应、XSLT、SSI 及图像缩放等功能。在SSI 过滤中,一个包含多个 SSI 的页面,如果经由 FastCGI 或反向代理,可被并行处理。
Nginx Web 服务特性
- 支持基于名字、端口及IP的多虚拟主机站点。
- 支持 Keep-alive 和 pipelined 连接。
- 可进行简单、方便、灵活的配置和管理。
- 支持修改 Nginx 配置,并且在代码上线时,可平滑重启,不中断业务访问。
- 可自定义访问日志格式,临时缓冲写日志操作,快速日志轮询及通过 rsyslog 处理日志。
- 可利用信号控制 Nginx 进程。
- 支持 3xx-5xx HTTP状态码重定向。
- 支持 rewrite 模块,支持 URI 重写及正则表达式匹配。
- 支持基于客户端 IP 地址和 HTTP 基本认证的访问控制。
- 支持 PUT、DELETE、MKCOL、COPY 及 MOVE 等特殊的 HTTP 请求方法。
- 支持 FLV 流和 MP4 流技术产品应用。
- 支持 HTTP 响应速率限制。
- 支持同一 IP 地址的并发连接或请求数限制。
- 支持邮件服务代理。
Nginx 工作原理
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
Nginx默认采用多进程工作方式,Nginx启动后,会运行一个master进程和多个worker进程。其中master充当整个进程组与用户的交互接口,同时对进程进行监护,管理worker进程来实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。worker用来处理基本的网络事件,worker之间是平等的,他们共同竞争来处理来自客户端的请求。
nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
web sever:web服务器的集群
application server : 应用服务器的集群
mencached: 缓存服务器的集群
backend: 后端
advaced I/O: 同步
sendfile,AIO 异步
工作方式
在工作方式上,Nginx分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。
Nginx在启动后,会有一个master进程和多个worker进程。
下图展示了nginx模块一次常规的HTTP请求和响应的过程
WEB服务请求步骤
客户端发送一个请求到Web服务器,请求首先是到网卡
网卡将请求交由内核空间的内核处理,其实就是拆包了,发现请求的是80端口
内核便将请求发给了在用户空间的Web服务器,Web服务器解包发现客户端请求的index.html页面
Web服务器便进行系统调用将请求发给内核
内核发现在请求的是一页面,便调用磁盘的驱动程序,连接磁盘
内核通过驱动调用磁盘取得的页面文件
内核将取得的页面文件保存在自己的缓存区域中便通知Web进程或线程来取相应的页面文件
Web服务器通过系统调用将内核缓存中的页面文件复制到进程缓存区域中
Web服务器取得页面文件来响应用户,再次通过系统调用将页面文件发给内核
内核进程页面文件的封装并通过网卡发送出去
当报文到达网卡时通过网络响应给客户端
1.建立连接
建立连接:接收或拒绝连接请求:三次握手的过程
提高http连接性能:
并行链接:通过多条tcp连接发起并发的http请求
持久连接:keepalive长连接,重用TCP连接,以消除连接和关闭的时延,以事务个数和时间来决定是否关闭连接
管道化连接:通过共享TCP连接和发起并发的http请求
串行连接
并行连接
持久连接
管道化连接
Web访问响应模型(Web I/O)
单进程I/O模型:启动一个进程处理用户请求,而且一次只处理一个,多个请求被串行响应,太古老了
多进程I/O模型:并行启动多个进程,每个进程响应一个连接请求
复用I/O结构:启动一个进程,同时响应N个连接请求,连接池
实现方法:多线程模型和事件驱动
多线程模型:一个进程生成N个线程,每线程响应一个连接请求
事件驱动:一个进程处理N个请求
进程:比如复制的工作,项目小组,耗资源
线程:比如人,轻量级
一个进程必有一个线程,一个进程可以有多个线程
复用的多进程I/O模型:启动M个进程,每个进程响应N个连接请求,同时接收M*N个请求
处理请求
服务器对请求报文进行解析,并获取请求的资源及请求方法等相关信息,根据方法,资源,首部和可选的主题部分对请求进行处理
元数据:请求报文首部
HEADERS 格式 name:value
示例:
Host: www.along.com 请求的主机名称
Server: Apache/2.4.7
HTTP 常用请求方式,Method:
GET 、POST 、HEAD 、PUT 、DELETE 、TRACE 、OPTIONS
访问资源
服务器获取请求报文中请求的资源web服务器,即存放了web资源的服务器,负责向请求者提供对方请求的静态资源,或动态运行后生成的资源
资源放置于本地文件系统特定的路径:DocRoot服务的根
DocRoot ——> /var/www/html
例:/var/www/html/test/1.jpg
构建响应报文
一旦web服务器识别出了资源,就执行请求方法中描述的动作,并返回响应报文。响应报文中,包含有响应状态码、响应首部,如果生成了响应主体的话,还包括响应主题。
响应实体:如果事务处理产生了响应主体,就将内容放在响应报文中回送过去。响应报文中通常包括:
描述响应主体MIME类型 的Content-Type首部
描述了响应主体长度大小的Content-Length
实际报文的主体内容
URL重定向:web服务构建的响应并非客户端请求的资源,而是资源另外一个访问路径
MIME类型:多媒体的邮件扩展
web服务器要负责确定响应主体的MIME类型。有很多配置服务器的方法可以将MIME类型与资源管理起来
魔法分类(扫描首部信息):apache web服务器可以扫描每个资源的内容,并将其与一个已知模式表,首部(被称为魔法文件)进行匹配,以决定每个文件的MIME类型。这样做可能比较慢,但很方便,尤其是文件没有标准扩展名的时候
显式分类:可以对web服务器进行配置,使其不考虑文件的扩展名或内容,强制特定文件或目录内容拥有某个MIME类型,例如:php,apache不识别,强制识别
类型协商:有些web服务器经过配置,可以以多种文档格式来存储资源。在这种情况下,可以配置web服务器,使其可以通过与用户的协商来决定使用哪种格式(及最相关的MIME类型)最好
## 发送响应报文
web服务器通过连接发送数据时也会面临与接收数据一样的问题。服务器可能有很多条到各个客户端的连接,有些是空闲的,有些在向服务器发送数据,还有一些在向客户端回送响应数据。服务器要记录连接的状态,还要特别注意对持久连接的处理。对非持久连接而言,服务器应该在发送了整条报文之后,关闭自己这一端的连接。对持久连接来说,连接可能仍保持打开状态,在这种情况下,服务器要正确地计算Content-Length首部,不然客户端就无法知道响应什么时候结束了
记录日志
最后,当事务结束时,web服务器会在日志文件中添加一个条目,来描述已执行的事务
日志类型:
- 访问日志:现在愈发重要,大数据时代
- Length首部,不然客户端就无法知道响应什么时候结束了
nginx的安装与配置
nginx及apache网站存放的位置:
- 源码安装的nginx,网站存放在安装目录上的HTML下面
- yum安装的nginx,网站存放在/usr/share/nginx/html下面
- 源码安装的apache,网站存放在安装目录的htdocs下面
- yum安装的apache,网站放在/var/www/html下面
// 关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service
[root@localhost ~]# setenforce 0
// 安装epel源、vim、wget
[root@nginx ~]# yum -y install epel-release vim wget
//创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
--2021-10-26 8:34:46-- http://nginx.org/download/nginx-1.12.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:980831 (958K) [application/octet-stream]
正在保存至: “nginx-1.12.0.tar.gz”
100%[=========================>] 980,831 498KB/s 用时 1.9s
2021-10-25 18:34:48 (498 KB/s) - 已保存 “nginx-1.12.0.tar.gz” [980831/980831])
//编译安装
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make && make install
//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
//启动nginx
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
nginx的配置文件详解
主配置文件:/usr/local/nginx/conf/nginx.conf
- 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
- 可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | MIME类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf配置详解
nginx.conf的内容分为以下几段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定义event模型工作特性
- http {}:定义http协议相关的配置
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 …];
支持使用变量:
- 内置变量:模块会提供内建变量定义
- 自定义变量:set var_name value
**
用于调试、定位问题的配置参数
**
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process{on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
位置一般用的是file 文件
级别一般默认的是error
正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组 pid
/path/to/pid_file; //指定nginx守护进程的pid文件 worker_rlimit_nofile
number; //设置所有worker进程最大可以打开的文件数,默认为1024 worker_rlimit_core size;
//指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s keepalive_requests
number; //在一个长连接上所能够允许请求的最大资源数 keepalive_disable
[msie6|safari|none]; //为指定类型的UserAgent禁用长连接 tcp_nodelay on|off;
//是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on client_header_timeout
number; //读取http请求报文首部的超时时长 client_body_timeout number;
//读取http请求报文body部分的超时时长 send_timeout number; //发送响应报文的超时时长
fastcgi的相关配置参数
LNMP:php要启用fpm模型
配置示例如下:
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_params; }
常需要进行调整的参数
- worker_processes
- worker_connections
- worker_cpu_affinity
- worker_priority
nginx作为web服务器时使用的配置:http{}段的配置参数
http{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:
http {//协议级别 include mime.types; default_type
application/octet-stream; keepalive_timeout 65; gzip on;
upstream {//负载均衡配置
… } server {//服务器级别,每个server类似于httpd中的一个
listen 80;
server_name localhost;
location / {//请求级别,类似于httpd中的,用于定义URL与本地文件系统的映射关系
root html;
index index.html index.htm;
} } }
http{}段配置指令:
server {}:定义一个虚拟主机,示例如下:
server { listen 80; server_name www.idfsoft.com; root
“/vhosts/web”; }
listen:指定监听的地址和端口
listen address[:port]; listen port;
server_name NAME […]; 后面可跟多个主机,名称可使用正则表达式或通配符
当有多个server时,匹配顺序如下:
1.先做精确匹配检查
2.左侧通配符匹配检查,如*.idfsoft.com
3.右侧通配符匹配检查,如mail.*
4.正则表达式匹配检查,如~ ^.*.idfsoft.com$
5.default_server
root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; 用于location配置段,定义路径别名
index file; 默认主页面
平滑升级
//获取之前的编译参数
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
//下载新模块echo-nginx-module并解压
[root@localhost nginx-1.20.1]# unzip echo-nginx-module-master.zip
//编译安装
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master
[root@localhost nginx-1.20.1]# make
//备份原程序、将原程序替换并启动新程序
[root@localhost nginx-1.20.1]# cp /usr/local/nginx/sbin/nginx /opt/
[root@localhost nginx-1.20.1]# ls /opt/
mime.types nginx nginx.conf
[root@localhost nginx-1.20.1]# nginx -s stop;objs/nginx -c /usr/local/nginx/conf/nginx.conf
//测试
[root@localhost local]# vim nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location /test {
echo "zrz";
}
[root@localhost local]# ./nginx-1.20.1/objs/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@localhost local]# ./nginx-1.20.1/objs/nginx -s reload
[root@localhost ~]# curl http://192.168.126.156/test
mkf
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
48 error_page 404 =200 /404.html;
[root@localhost ~]# nginx -s reload
location配置
location区段,通过指定模式来与客户端请求的URI相匹配
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
如果没有匹配到相应的内容则用默认的主页响应http://xxx.com/
没有修饰符表示必须以指定模式开始,如:
[root@nginx conf]# vim nginx.conf … server { server_name
www.idfsoft.com; location /abc {
… } }
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22 //“?” 向服务器提交数据 账户密码这类的验证
- http://www.idfsoft.com/abc/
=:表示必须与指定的模式精确匹配,如;
server { server_name www.idfsoft.com; location = /abc {
… } }
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abc/abcde
~:表示指定的正则表达式要区分大小写,如:
server { server_name www.idfsoft.com; location ~ ^/abc$ { …
} }
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/ABC
- http://www.idfsoft.com/abcde
~*:表示指定的正则表达式不区分大小写,如:
server { server_name www.idfsoft.com; location ~* ^/abc$ {
… } }
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
- http://www.idfsoft.com/ABC
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abcde
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
官方文档实例:
[root@nginx conf]# vim nginx.conf
#access_log logs/host.access.log main;
location = / {
echo "[ configuration A ]";
}
location / {
echo "[ configuration B ]";
}
location /documents/ {
echo "[ configuration C ]";
}
location ^~ /images/ {
echo "[ configuration D ]";
}
location ~* \.(gif|jpg|jpeg)$ {
echo "[ configuration E ]";
}
error_page 404 =200 /404.html;
访问控制
用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
示例:
[root@localhost conf]# htpasswd -c -m .pass admin // admin是虚拟账户可随意指定,不能用来登录系统,但是可以访问nginx
New password:
Re-type new password:
Adding password for user admin
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
location /test {
allow 192.168.126.1; //添加允许的访问IP
allow 192.168.127.1; //添加允许的网段
deny all; //禁止所有人访问,除了上面两个IP
root html;
index index.html;
}
这样就只有126.1和127.1地址的主机访问,其他所有主机都不行
用户认证
语法、使用位置
Syntax: auth_basic string | off; 语法 Default: auth_basic off;
//默认关闭 Context: http, server, location, limit_except //使用位置
user_auth_file内容格式为:
username:password
这里的密码为加密后的密码串,建议用htpasswd来创建此文件:
htpasswd -c -m /path/to/.user_auth_file USERNAME
https配置
[root@localhost conf]# mkdir ssl
[root@localhost conf]# cd ssl
[root@localhost conf]# mkdir -p /etc/pki/CA
[root@localhost ssl]# cd /etc/pki/CA
//生成密钥
[root@localhost CA]# mkdir private && (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
................................+++++
.................................................................................................+++++
e is 65537 (0x010001)
//生成自签证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:rumtime
Organizational Unit Name (eg, section) []:rumtime
Common Name (eg, your name or your server's hostname) []:test.runtime.com
Email Address []:1@2.com
[root@localhost CA]# mkdir certs newcerts crl
[root@localhost CA]# touch index.txt && echo 01 > serial
//生成密钥
[root@localhost ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
....................................................+++++
e is 65537 (0x010001)
[root@localhost ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:rumtime
Organizational Unit Name (eg, section) []:rumtime
Common Name (eg, your name or your server's hostname) []:test.runtime.com
Email Address []:1@2.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 27 14:26:54 2021 GMT
Not After : Oct 27 14:26:54 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = HB
organizationName = rumtime
organizationalUnitName = rumtime
commonName = test.runtime.com
emailAddress = 1450146910@qq.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
E4:31:1D:62:46:1B:33:0E:49:27:D8:2E:AC:93:7D:CF:4E:1C:53:D8
X509v3 Authority Key Identifier:
keyid:3A:BE:A2:4E:93:D1:EA:11:7C:4B:F6:68:03:F8:67:33:D4:71:BB:44
Certificate is to be certified until Oct 27 14:26:54 2022 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]# rm -rf nginx.csr
[root@localhost conf]# vim nginx.conf
server { #这些都取消注释
listen 443 ssl;
server_name test.runtime.com;
ssl_certificate ssl/nginx.crt; #修改证书的位置
ssl_certificate_key ssl/nginx.key; #修改
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
[root@localhost conf]# nginx -s reload
开启状态界面
开启status:
location /status {
stub_status {on | off};
allow 192.168.126.156;
deny all;
}
访问状态页面的方式:http://server_ip/status
状态页面信息详解:
状态码 | 表示的意义 |
---|---|
Active connections 2 | 当前所有处于打开状态的连接数 |
accepts | 总共处理了多少个连接 |
handled | 成功创建多少握手 |
requests | 总共处理了多少个请求 |
Reading | nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数 |
Writing | nginx返回给客户端的Header信息数,表示请求已经接收完成,且正处于处理请求或发送响应的过程中的连接数 |
Waiting | 开启keep-alive的情况下,这个值等于active - (reading + writing),意思就是Nginx已处理完正在等候下一次请求指令的驻留连接 |
示例:
location /test {
stub_status;
allow 192.168.126.156;
deny all;
}
查看
[root@nginx ~]# curl http://192.168.126.156/test
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
zabbix_agent配置不与描述,详情可查阅zabbix监控配置流程
[root@nginx ~]# mkdir /scripts
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# ls
[root@nginx scripts]# touch check_status.sh
[root@nginx scripts]# vim check_status.sh
[root@nginx scripts]# cat check_status.sh
#!/bin/bash
status=$(curl -s 192.168.126.156/status |awk 'NR==4'|awk -F: {'print $4'})
if [ $status -ge 1 ];then
echo "1"
else
echo "0"
fi
[root@nginx scripts]# chmod +x check_status.sh
rewaite
语法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*.jpg)$ /imgs/$1 break;
此处的$1用于引用(.*.jpg)匹配到的内容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
标识符 | 意义 |
---|---|
^ | 必须以^后的实体开头 |
$ | 必须以$前的实体结尾 |
. | 匹配任意字符 |
[] | 匹配指定字符集内的任意字符 |
[^] | 匹配任何不包括在指定字符集内的任意字符串 |
() | 分组,组成一组用于匹配的实体,通常会有 |
还有一个:| : 匹配 | 之前或之后的实体
捕获子表达式,可以捕获放在()之间的任何文本,比如:
^(hello|sir)$ //字符串为“hi sir”捕获的结果:$1=hi$2=sir
//这些被捕获的数据,在后面就可以当变量一样使用了
示例:
[root@nginx html]# mkdir images
[root@nginx html]# cd images/
[root@nginx images]# ls
1.jpg
修改images文件夹名称
[root@nginx html]# mv images/ imgs/
[root@nginx html]# ls
404.html 50x.html 中国公益网_files imgs index.html test
弹出错误信息
修改nginx.conf文件
//添加rewrite用法
[root@nginx nginx]# vim conf/nginx.conf
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; //添加break是让他在执行完这一条后直接输出
}
此时我们访问images文件夹依旧能够访问到
实例2:
将输出页面转载到其他网站
//配置nginx.conf
[root@nginx nginx]# vim conf/nginx.conf
location /images {
rewrite ^/images/(.*)$ http://images.baidu.com break; //替换成百度图片界面
}
last向后匹配
//配置
location /images {
rewrite ^/images/(.*i\.jpg)$ /imgs/$1 last; //已经匹配到时,因为有last,所以还会向后在匹配一次
}
location /imgs {
rewrite ^/images/(.*)$ http://images.baidu.com; //匹配到此处
}
访问:
输入192.168.126.156/images/1.jpg
跳转到images.baidu.com
break则与之相反,匹配到了则会停止匹配直接输出
if
语法:if (condition) {…}
应用场景:
- server段
- location段
常见的condition
- 变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
- 以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
- 正则表达式的模式匹配操作
~:区分大小写的模式匹配检查
~:不区分大小写的模式匹配检查
!和!:对上面两种测试取反 - 测试指定路径为文件的可能性(-f,!-f)
- 测试指定路径为文件的可能性(-f,!-f)
- 测试文件的存在性(-e,!-e)
- 检查文件是否有执行权限(-x,!-x)
基于浏览器实现分离案例
if ($http_user_agent ~ Firefox) { //火狐浏览器
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) { //IE浏览器
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) { //谷歌浏览器
rewrite ^(.*)$ /chrome/$1 break;
}
防盗链案例
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.dzcia.top.com;
if ($invalid_referer) {
rewrite ^/ http://www.dzcia.top/403.html;
}
}