Nginx

1、Nginx 进程结构

web 请求处理机制
多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端,直
到用户关闭连接,这样的优势是处理速度快,子进程之间相互独立,但是如果访问过大会导致服务
器资源耗尽而无法提供请求
多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程和此客
户端进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程度减轻了 web 服务器
对系统资源的要求,但是多线程也有自己的缺点,即当多个线程位于同一个进程内工作的时候,可
以相互访问同样的内存地址空间,所以他们相互影响,一旦主进程挂掉则所有子线程都不能工作
了, IIS 服务器使用了多线程的方式,需要间隔一段时间就重启一次才能稳定。
Nginx 是多进程组织模型,而且是一个由 Master 主进程和 Worker 工作进程组成。

(1)master 进程

当 Nginx 在启动后,会有一个 master 进程和多个worker 进程。master 进程主要用来管理 worker 进程,master 要做的就是:接收来自外界的信号,向各 worker 进程发送信号,监控 worker 进程的运行状态,当 worker 进程退出后(异常情况下),会自动重新启动新的 worker 进程。

master 进程主要完成如下工作:

读取并验证配置信息;
创建、绑定及关闭套接字;
启动、终止 worker 进程及维护 worker 进程的个数;
无须中止服务而重新配置工作;
控制非中断式程序升级,启用新的二进制程序并在需要时回滚至老版本;
重新打开日志文件;
编译嵌入式 Perl 脚本。

(2) worker 进程

对于基本的网络事件,Nginx 则是放在 worker 进程中来处理。多个 worker 进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。(会发生惊群<内核小于2.6.18,Nginx中处理epoll时惊群问题的思路很简单,多个子进程有一个锁,谁拿到锁,谁才将accept的fd加入到epoll队列中,其他的子进程拿不到锁,也就不会将fd加入到epoll中,连接到来也就不会导致所有子进程的epoll被唤醒返回)。一个请求,只可能在一个 worker 进程中处理,一个 worker 进程,不可能处理其它进程的请求(一对一)。然而 Nginx 没有专门地仲裁或连接分布的 worker,这项工作是由操作系统内核机制完成的。在启动时,创建一组初始的监听套接字,HTTP 请求和响应之时,worker 连续接收、读取和写入套接字。

worker 进程主要完成如下工作:

接收、传入并处理来自客户端的连接
提供反向代理及过滤功能
nginx 任何能完成的其它任务

2、Nginx 核心配置详解

(1)、Nginx的配置文件的组成部分:

主配置文件: nginx.conf
子配置文件 : include conf.d/*.conf
fastcgi uwsgi scgi 等协议相关的配置文件
mime.types :支持的 mime 类型, MIME(Multipurpose Internet Mail Extensions) 多用途互联网邮
件扩展类型, MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某
种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动
使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
nginx 配置文件格式说明

(2)、主配置文件结构:四部分

main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
...
}
#http/https 协议相关配置段
http {
...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}
#stream 服务器相关配置段
stream {
...
}

(3)、root alias

root :指定 web 的家目录,在定义 location 的时候,文件的绝对路径等于 root+location
server {
listen 80;
server_name lee.lle.org;
location / {
root /webdata/nginx/ll.org/lee/html;
}
location /dirtest { #必须建立/mnt/dirtest才能访问
root /mnt;
}
}

alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于 location上下文,此指令使用较少

server {
listen 80;
server_name lee.ll.org;
location / {
root /webdata/nginx/ll.org/lee/html;
}
location /dirtest {
root /mnt;
}
location /alias { #注意about后不要加/
#使用alias的时候uri后面如果加了斜杠,则下面的路径配置必须加斜杠,否则403
alias /mnt/dirtest; #当访问alias的时候,会显示alias定义的/mnt/dirtest里面的内容
}
}

3、Nginx 高级配置

(1)、Nginx 状态页

location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数
#包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active –(reading+writing)

(2)、Nginx 压缩功能

#启用或禁用gzip压缩,默认关闭
gzip on | off;
#压缩比由低到高从1到9,默认为1,值越高压缩后文件越小,但是消耗cpu比较高。基本设定未4或者5
gzip_comp_level 4;
#禁用IE6 gzip功能,早期的IE6之前的版本不支持压缩
gzip_disable "MSIE [1-6]\.";
#gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
#启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_http_version 1.0 | 1.1;
#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
gzip_types mime-type ...;
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;

4、内置变量

$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP
$args;
#变量中存放了URL中的所有参数
#例如:https://search.jd.com/Search?keyword=手机&enc=utf-8
#返回结果为: keyword=手机&enc=utf-8
$is_args
#如果有参数为? 否则为空
$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/webdata/nginx/timinglee.org/lee。
$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令
#比如:http://lee.timinglee.org/var?\id=11111会被定义为/var
#返回结果为:/var
$host;
#存放了请求的host名称
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
$remote_user;
#已经经过Auth Basic Module验证的用户名
$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称
$request_method;
#请求资源的方式,GET/PUT/DELETE等
$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,
#如:webdata/nginx/timinglee.org/lee/var/index.html
$request_uri;
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,
#例如:/main/index.do?id=20190221&partner=search
$scheme;
#请求的协议,例如:http,https,ftp等
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr;
#保存了服务器的IP地址
$server_name;
#虚拟主机的主机名
$server_port;
#虚拟主机的端口号
$http_user_agent;
#客户端浏览器的详细信息
$http_cookie;
#客户端的所有cookie信息
$cookie_<name>
#name为任意请求报文首部字部cookie的key名
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线

5、Rewrite

(1)if 指令

if (条件匹配) {
action
}

= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)

(2)set指令

指定 key 并给其定义一个变量,变量可以调用 Nginx 内置变量赋值给 key,另外set 定义格式为 set $key value value 可以是 text, variables 和两者的组合。

(3)break指令

用于中断当前相同作用域 (location) 中的其他 Nginx 配置 与该指令处于同一作用域的Nginx 配置中,位于它前面的配置生效 位于后面的 ngx_http_rewrite_module 模块中指令就不再执行
Nginx 服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server 块和 locationif 块中使用

(4)return 指令

return 用于完成对请求的处理,并直接向客户端返回响应状态码,比如 : 可以指定重定向 URL( 对于特殊重 定向状态码,301/302 ) 或者是指定提示文本内容 ( 对于特殊状态码 403/500 ) ,处于此指令后的所有配置都将不被执行,return 可以在 server if location 块进行配置
语法格式
return code; # 返回给客户端指定的 HTTP 状态码
return code [text]; # 返回给客户端的状态码及响应报文的实体内容
# 可以调用变量 , 其中 text 如果有空格 , 需要用单或双引号
return code URL; # 返回给客户端的 URL 地址

(5)rewrite 指令

rewrite 将用户请求的 URI 基于 regex 所描述的模式进行检查,匹配到时将其替换为表达式指定的新的 URI
正则表达式格式
. # 匹配除换行符以外的任意字符
\w # 匹配字母或数字或下划线或汉字
\s # 匹配任意的空白符
\d # 匹配数字
\b # 匹配单词的开始或结束
^ # 匹配字付串的开始
$ # 匹配字符串的结束
* # 匹配重复零次或更多次
+ # 匹配重复一次或更多次
? # 匹配重复零次或一次
(n) # 匹配重复 n
{n,} # 匹配重复 n 次或更多次
{n,m} # 匹配重复 n m
*? # 匹配重复任意次,但尽可能少重复
+? # 匹配重复 1 次或更多次,但尽可能少重复
?? # 匹配重复 0 次或 1 次,但尽可能少重复
{n,m}? # 匹配重复 n m 次,但尽可能少重复
{n,}? # 匹配重复 n 次以上,但尽可能少重复
\W # 匹配任意不是字母,数字,下划线,汉字的字符
\S # 匹配任意不是空白符的字符
\D # 匹配任意非数字的字符
\B # 匹配不是单词开头或结束的位置
[^x] # 匹配除了 x 以外的任意字符
[^lee] # 匹配除了 magedu 这几个字母以外的任意字符

(6)nginx防盗链

none # 请求报文首部没有 referer 首部,
# 比如用户直接在浏览器输入域名访问 web 网站,就没有 referer 信息。
blocked # 请求报文有 referer 首部,但无有效值,比如为空。
server_names #referer 首部中包含本主机名及即 nginx 监听的 server_name
arbitrary_string # 自定义指定字符串,但可使用 * 作通配符。
regular expression # 被指定的正则表达式模式匹配到的字符串 , 要使用 ~ 开头

6、反向代理功能

反向代理: reverse proxy ,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
ngx_http_proxy_module: # 将客户端的请求以 http 协议转发至指定服务器进行处理
ngx_http_upstream_module # 用于定义为 proxy_pass,fastcgi_pass,uwsgi_pass
# 等指令引用的后端服务器分组
ngx_stream_proxy_module: # 将客户端的请求以 tcp 协议转发至指定服务器处理
ngx_http_fastcgi_module: # 将客户端对 php 的请求以 fastcgi 协议转发至指定服务器助理
ngx_http_uwsgi_module: # 将客户端对 Python 的请求以 uwsgi 协议转发至指定服务器处理
实现 Nginx 四层负载均衡
Nginx 1.9.0 版本开始支持 tcp 模式的负载均衡,在 1.9.13 版本开始支持 udp 协议的负载, udp 主要用于 DNS的域名解析,其配置方式和指令和 http 代理类似,其基于 ngx_stream_proxy_module 模块实现 tcp 负载,另外基于模块ngx_stream_upstream_module 实现后端服务器分组转发、权重分配、状态监测、 调度算法等高级功能。
如果编译安装 , 需要指定 --with-stream 选项才能支持 ngx_stream_proxy_module 模块

(1)反向代理配置参数

proxy_pass; #用来设置将客户端请求转发给的后端服务器的主机
#可以是主机名(将转发至后端服务做为主机头首部)、IP地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
url后面
#此行为类似于root
#proxy_pass指定的uri不带斜线将访问的/web
#等于访问后端服务器
uri内容
#此行为类似于alias
#proxy_pass指定的uri带斜线
#等于访问后端服务器的
#http://172.25.254.40:8080/index.html
#内容返回给客户端
proxy_pass_header field; #透传
#默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数
#如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端
#field 首部字段大小不敏感
proxy_pass_request_body on | off;
#是否向后端服务器发送HTTP实体部分,可以设置在http,server或location块,默认即为开启
proxy_pass_request_headers on | off;
#是否将客户端的请求头部转发给后端服务器,可以设置在http,server或location块,默认即为开启
proxy_set_header;#可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部
proxy_connect_timeout time;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒
用法如下:proxy_connect_timeout 6s;
#60s为自定义nginx与后端服务器建立连接的超时时间,超时会返回客户端504响应码
proxy_read_timeout time;
#配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_timeout time;
#配置nginx项后端服务器或服务器组发起write请求后,等待的超时 时间,默认60s
proxy_http_version 1.0;
#用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
proxy_ignore_client_abort off;
#当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器、
会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求
并立即记录499日志,默认为off。

(2)缓存功能

proxy_cache proxycache;
proxy_cache_key $request_uri; # 对指定的数据进行 MD5 的运算做为缓存的 key
proxy_cache_valid 200 302 301 10m; # 指定的状态码返回的数据缓存多长时间
proxy_cache_valid any 1m; # 除指定的状态码返回的数据以外的缓存多长时间 , 必须设置 ,
否则不会缓存
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |
http_502 | http_503 | http_504 | http_403 | http_404 | off ; # 默认是 off
# 在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
# 示例
proxy_cache_use_stale error http_502 http_503;
proxy_cache_methods GET | HEAD | POST ...;
# 对哪些客户端请求方法对应的响应进行缓存, GET HEAD 方法总是被缓存

7、实现 FastCGI

什么是PHP-FPM?

PHP-FPM(FastCGI Process Manager
FastCGI 进程管理器 ) 是一个实现了 Fastcgi 的程序,并且提供进程管理的功能。
进程包括 master 进程和 worker 进程。 master 进程只有一个,负责监听端口,接受来自 web server
的请求 worker进程一般会有多个,每个进程中会嵌入一个 PHP 解析器,进行 PHP 代码的处理。

(1)php源码配置

# 解压源码并安装
[root@Nginx ~]# ./configure \
--prefix=/usr/local/php \ # 安装路径
--enable-fpm \ # cgi 方式启动程序
--with-fpm-user=nginx \ # 指定运行用户身份
--with-fpm-group=nginx \
--with-curl \ # 打开 curl 浏览器支持
--with-iconv \ # 启用 iconv 函数,转换字符编码
--with-mhash \ #mhash 加密方式扩展库
--with-zlib \ # 支持 zlib 库,用于压缩 http 压缩传输
--with-openssl \ # 支持 ssl 加密
--enable-mysqlnd \ #mysql 数据库
--with-mysqli \ --with-pdo-mysql \
--disable-debug \ # 关闭 debug 功能
--enable-sockets \ # 支持套接字访问
--enable-soap \ # 支持 soap 扩展协议
--enable-xml \ # 支持 xml
--enable-ftp \ # 支持 ftp
--enable-gd \ # 支持 gd
--enable-exif \ # 支持图片元数据
--enable-mbstring \ # 支持多字节字符串
--enable-bcmath \ # 打开图片大小调整 , 用到 zabbix 监控的时候用到了这个模块
--with-fpm-systemd # 支持 systemctl 管理 cgi

8、实验

A、nginx平滑升级和回滚

[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip

关闭debug功能

[root@nginx nginx-1.24.0]# vim auto/cc/gcc
[root@nginx nginx-1.24.0]# make && make install
[root@nginx nginx-1.24.0]# vim ~/.bash_profile
把nginx软件的命令执行路径添加到环境变量中

[root@nginx nginx-1.24.0]# source ~/.bash_profile

平滑升级回收旧版本

版本回滚旧的激活,新的需要回收

B、nginx命令参数

----------------------------参数
[root@Nginx ~]# nginx -v
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示版本和编译参数
-t : test configuration and exit #测试配置文件是否异
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号,reload信号 会生成新的worker,但master不会重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路径
-g directives : set global directives out of configuration file #设置全局指令,注意和配置文件不要同时配置,否则冲突

C、nginx启动文件编写

D、nginx全局参数优化

E、location用法

在一个serverlocation配置段可存在多个,用于实现从uri到文件系统的路径映射; ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配, 而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri ,uri是用户请求的字符串,即域名后面的web文件路径 ,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理 此请求。

#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
#对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号

F、nginx的用户认证

[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd ll

G、自定义错误页面

H、自定义日志


查看生成日志[root@nginx ~]# cat /var/log/ll.org/access.log
[root@nginx ~]# cat /var/log/ll.org/error.log

I、nginx文件检测

J、nginx长链接控制

测试
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# telnet www.ll.org 80

K、nginx下载服务器

autoindex on; #自动索引功能
autoindex_exact_size on; #计算文件确切大小(单位bytes),此为默认值,off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间
limit_rate 1024k; #限速,默认不限速

测速

L、nginx的压缩功能

[root@nginx ~]# echo hello grtout > /data/web/html/small.html
[root@nginx ~]# cat /usr/local/nginx/logs/error.log > /data/web/html/small.html
[root@nginx ~]# cat /usr/local/nginx/logs/error.log > /data/web/html/big.html

M、nginx的内建变量

N、nginx的网页重写功能

[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl var.ll.org/test2/index.html
[root@nginx conf.d]# mkdir -p /data/web/html/test2/
[root@nginx conf.d]# echo test2 > /data/web/html/test2/index.html
[root@nginx conf.d]# curl var.ll.org/test2/index.html
显示结果为test2

set指令

[root@client20 ~]# curl ll.ll.org/test3
ll

break指令

[root@client20 ~]# curl ll.ll.org/break #当未添加break时
ll
80
[root@client20 ~]# curl ll.ll.org/break #添加break后
lee

return指令

[root@client20~]# curl lee.ll.org/return
/web/data/nginx/ll.org/ll/return is exist
[root@client20 ~]# curl ll.ll.org/return1
/web/data/nginx/ll.org/ll/return1 is not exist
. #匹配除换行符以外的任意字符
\w #匹配字母或数字或下划线或汉字
\s #匹配任意的空白符
\d #匹配数字
\b #匹配单词的开始或结束
^ #匹配字付串的开始
$ #匹配字符串的结束
* #匹配重复零次或更多次
+ #匹配重复一次或更多次
? #匹配重复零次或一次
(n) #匹配重复n次
{n,} #匹配重复n次或更多次
{n,m} #匹配重复n到m次
*? #匹配重复任意次,但尽可能少重复
+? #匹配重复1次或更多次,但尽可能少重复
?? #匹配重复0次或1次,但尽可能少重复
{n,m}? #匹配重复n到m次,但尽可能少重复
{n,}? #匹配重复n次以上,但尽可能少重复
\W #匹配任意不是字母,数字,下划线,汉字的字符
\S #匹配任意不是空白符的字符
\D #匹配任意非数字的字符
\B #匹配不是单词开头或结束的位置
[^x] #匹配除了x以外的任意字符
[^lee] #匹配除了magedu 这几个字母以外的任意字符
flag 说明
redirect;
#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端
#由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent;
#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端
#由客户端重新发起请求,状态码:301
break;
#重写完成后,停止对当前URL在当前location中后续的其它重写操作
#而后直接跳转至重写规则配置块之后的其它配置,结束循环,建议在location中使用
#适用于一个URL一次重写
last;
#重写完成后,停止对当前URI在当前location中后续的其它重写操作,
#而后对新的URL启动新一轮重写检查,不建议在location中使用
#适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户

break 与 last

O、nginx的网页重写实战案例

[root@nginx ~]# cd /usr/local/nginx
[root@nginx nginx]# mkdir certs
[root@nginx nginx]# cd certs/

[root@nginx ~]# cd /usr/local/nginx/certs
[root@nginx certs]# ls
ll.org.crt  ll.org.key
[root@nginx certs]# cd ..
[root@nginx nginx]# ls
certs             conf    fastcgi_temp  logs        sbin       uwsgi_temp
client_body_temp  conf.d  html          proxy_temp  scgi_temp
[root@nginx nginx]# cd conf.d
[root@nginx conf.d]# 

判断文件是否存在

访问的是www.ll.org/a/b 无 所以定向到index.html

P、防盗链

Q、nginx的反向代理

[root@client10 ~]# echo 172.25.254.10 > /var/www/html/index.html
[root@client20 ~]# echo 172.25.254.20 > /var/www/html/index.html

[root@client20 ~]# vim /etc/httpd/conf/httpd.conf

[root@client20 ~]# systemctl restart httpd

[root@client20 ~]# mkdir -p /var/www/html/static
[root@client20 ~]# echo static 172.25.254.20 > /var/www/html/static/index.html

[root@client10 ~]# yum install php -y
[root@client10 ~]# systemctl restart httpd
[root@client10 ~]# vim /var/www/html/index.php
[root@client10 ~]# cat /var/www/html/index.php
<?php
  phpinfo();
?>

Q、反向代理的缓存功能

[root@client10 ~]# ab -n1000 -c100 http://www.ll.org/static/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf

[root@client10 ~]# ab -n1000 -c100 http://www.ll.org/static/index.html

R、反向代理负载均衡

uri

[root@client10 ~]# mkdir -p /var/www/html/static
[root@client10 ~]# echo 172.25.254.10 static > /var/www/html/static/index.html

cookie

四层负载

[root@client10 ~]# dnf install bind -y
[root@client20 ~]# dnf install bind -y
[root@client20 ~]# vim /etc/named.conf

[root@client10 ~]# vim /etc/named.rfc1912.zones

[root@client10 ~]# cd /var/named/
[root@client10 named]# cp named.localhost ll.org.zone -p
[root@client10 named]# vim ll.org.zone
[root@client10 named]# systemctl start named

[root@client10 named]# dig www.ll.org @172.25.254.10

[root@client10 named]# dnf install mariadb-server -y
[root@client20 named]# dnf install mariadb-server -y
[root@nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf

[root@nginx tcpconf.d]# dnf install mariadb -y
[root@nginx tcpconf.d]# mysql -u ll -p -h 172.25.254.100
[root@nginx tcpconf.d]# mysql -u ll -p -h 172.25.254.100
+-------------+
| @@server_id |
+-------------+
| 20 |
+-------------+

S、php源码编译

[root@nginx ~]# cd nginx-1.26.1/
[root@nginx nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33 --user=nginx --group=nginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-pcre

[root@nginx nginx-1.26.1]# make && make install

[root@nginx php-8.3.9]# dnf install systemd-devel -y

[root@nginx php-8.3.9]# yum install -y bzip2  libxml2-devel sqlite-devellibpng-devel libcurl-devel oniguruma-devel
[root@nginx php-8.3.9]# make && make install


 

T、PHP配置优化

[root@nginx ~]# cd /usr/local/php/
[root@nginx php]# ls
bin  etc  include  lib  php  sbin  var
[root@nginx php]# cd
[root@nginx ~]# cd /usr/local/php/etc/
[root@nginx etc]# ls
php-fpm.conf.default  php-fpm.d
[root@nginx etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@nginx etc]# vim php-fpm.conf

[root@nginx etc]# cd php-fpm.d/
[root@nginx php-fpm.d]# ls
www.conf.default
[root@nginx php-fpm.d]# cp www.conf.default www.conf -p
[root@nginx php-fpm.d]# ls
www.conf  www.conf.default
[root@nginx php-fpm.d]# vim www.conf

[root@nginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@nginx php-8.3.9]# cd 
[root@nginx ~]# cd /usr/local/php/etc
[root@nginx etc]# vim php.ini

U、nginx和php的整合

[root@nginx ~]# cd /usr/local/php/
[root@nginx php]# ls
bin  etc  include  lib  php  sbin  var
[root@nginx php]# cd bin/
[root@nginx bin]# ls
phar  phar.phar  php  php-cgi  php-config  phpdbg  phpize
[root@nginx bin]# ped
bash: ped: command not found...
[root@nginx bin]# pwd
/usr/local/php/bin
[root@nginx bin]# vim ~/.bash_profile
[root@nginx bin]# source ~/.bash_profile

[root@nginx nginx]# mkdir conf1.d
[root@nginx nginx]# vim conf/nginx.conf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值