这是本节的多页打印视图。 .
Nginx / Tengine
- 1: Nginx 常用命令
- 2: Nginx 知识点
- 3: tengine 安装笔记
- 4: location 和 rewrite 笔记
- 5: ngx_http_proxy_module 模块学习笔记
- 6: ngx_http_rewrite_module 模块学习笔记
共 6 篇文档。
1 - Nginx 常用命令
启动命令
- linux 启动方式
- window 启动方式
其他常用命令
- -?,-h : 查看帮助信息
- -v : 显示版本信息
- -V : 显示版本信息和编译配置参数
- -t : 测试配置文件是否正确
- -T : 测试配置文件是否正确,同时输出所有有效配置内容
- -q : 在配置测试期间禁止显示非错误消息
- -s signal : 向主进程发送信号,常用的信号有 stop 快速关闭, quit 正常关闭, reopen 重新打开日志文件, reload 重新加载配置文件,启动一个加载新配置文件的 Worker Process, 正常关闭一个加载旧配置文件的 Worker Process
- -e filename : 指定 error 文件路径
- -c filename : 指定配置文件路径
- -g directives : 指定全局配置指令, 如:nginx -g “daemon off;”
2 - Nginx 知识点
root 和 alias 的区别
Nginx 中 root 是路径拼接,alias 是路径替换,二者决定请求 URI 如何映射到磁盘文件路径。
| 维度 | root | alias |
|---|---|---|
| 路径逻辑 | 根路径 + 完整请求 URI(追加) | 别名路径 + 去掉 location 匹配前缀后的剩余 URI(替换) |
| 适用上下文 | http、server、location | 仅 location |
| 末尾斜杠 | 可有可无(不影响拼接) | 建议以 / 结尾(否则可能被当作文件导致错误) |
| 正则匹配 | 无需捕获组 | 必须包含捕获组且 alias 引用(官方要求) |
| 典型场景 | URI 与目录结构一致 | 需将 URI 映射到不同目录 |
简单示例
- root 示例
请求 /img/top.gif → 映射到 /data/dist/img/top.gif。
- alias 示例
请求 /img/top.gif → 映射到 /data/dist/images/top.gif。
- location 正则匹配时,alias 必须引用正则捕获组,root 无此限制
请求 /img/top.gif → 映射到 /data/dist/images/top.gif。
3 - tengine 安装笔记
编译安装 nginx 淘宝定制的
- 官网下载 tengine
- 解压文件
tar zxvf tengine-2.3.3.tar.gz cd tengine-2.3.3./configuremakesudo make install默认会将 tengine 安装到 /usr/local/nginx 下- 编译提示
error: the HTTP rewrite module requires the PCRE library安装yum -y install pcre-devel - 编译提示
error: SSL modules require the OpenSSL library安装yum -y install openssl openssl-devel
注意使用 ./configure 生成 makefile 时,可以使用 ./configure --help 查看其有哪些参数可设置,其中部分参数例子,使用如下:
将 nginx 注册为系统服务在 /lib/systemd/system/ 目录创建 nginx.service 文件,如下:
nginx 服务启动的相关命令:
nginx1.18 以后动态添加模块
运行命令 nginx -V 查看 nginx 已经安装了哪些模块,运行命令后显示示例结果如下:
重新使用 ./configure 生成新的 makefile 时,需要把旧 nginx 程序中的 configure arguments中的参数也要带上,不然已安装或者禁止的模块会丢弃。
./configure 之后,然后运行 make 编译生成新的 nginx 程序,注意make 之后,不要make install因为 make install 是覆盖安装。
make 完之后新的 nginx 程序会生成在跟 configure 同级的 objs 目录中。运行 ./objs/nginx -V 如果看到编译参数已经变成新编辑的参数,说明编译成功。
将新编译生成的 nginx 覆盖原来的 nginx 即可,注意备份原来的 nginx
4 - location 和 rewrite 笔记
location 指令
location 指令由 ngx_http_core_module 模块提供,官方文档说明
The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.
匹配是针对标准化的 URI 执行的,故在执行前,会将 URI 中以 %XX 形式编码的文本先进行解码、及解析对相对路径中 . 和 .. 组件的引用,和尽可能的去将路径中 // 压缩替换成 /。
A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “*” modifier (for case-insensitive matching), or the “” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
location 的 URI 匹配规则可以使用前缀字符串定义,也可以由正则表达式定义。使用正则表达式,前缀指定 ~* 修饰符(用于不区分大小写的匹配)或 ~ 修饰符(适用于区分大小写匹配)。查找 location 匹配请求时,nginx 会优先检查匹配使用前缀字符串定义的 location,如果存在多个匹配的前缀字符串 location,则会取优先选择最长前缀字符串的 location 记录起来,然后按照正则表达式在配置文件中的出现顺序检查匹配正则表达式 location,搜索匹配到第一个正则表达式 location 时,就会终止查找匹配,并使用该正则表达作为最终的匹配结果。如果找不到匹配的正则表达式 location,则使用前面记录的的前缀字符串 location。
location blocks can be nested, with some exceptions mentioned below.
location 语句块是可以嵌套的,除了下面提到的一些情况是例外的。
For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.
对于 macOS 和 Cygwin 等不区分大小写的操作系统,匹配前缀字符串 location 会忽略大小写( 0.7.7 版本)。然而,比较仅限于一个字节的区域设置。
Regular expressions can contain captures (0.7.40) that can later be used in other directives.
正则表达式可以包含捕获(0.7.40),稍后可以在其他指令中使用。
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
如果匹配最长的前缀字符串 location 前缀使用 ^~ 修饰符,则不会在继续检查匹配正则表达式 location。
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.
当然,使用 = 修饰符定义的前缀字符串 location,它会精准匹配 URI。如果精准匹配 location 匹配上了,就会终止后续的匹配。例如,如果一个 / 频繁的请求,使用 = 符号,定义一个 location = / 精准匹配,将加快对这些请求的处理,由于匹配上第一精准的 location 之后就在终止继续查找匹配。像这样的 location 显然是不能嵌套另一个 location 的。
In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.
在 0.7.1 到 0.8.41 版本中,如果请求与前缀字符串的 location 匹配,但没有 = 和 ^~ 修饰符,也会终止检查匹配,并且不检查正则表达式。
Let’s illustrate the above by an example:
让我们通过一个例子来说明以上内容:
The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
访问 / 请求会匹配 configuration A,访问 /index.html 请求会匹配 configuration B,访问 /documents/document.htm 会匹配 configuration C,访问 /images/1.gif 请求会匹配 configuration D,和访问 /documents/1.jpg 请求会匹配 configuration E。
The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.
@ 符号前缀定义命名 location,这样定义的 location 不能用于常规请求处理,但是可以嵌套在其他 location 中用于请求重定向和转发请求,它们自身是不能嵌套其他 locations 的。
If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:
如果一个 location 的匹配规则使用前缀字符定义,并且结尾是 / 字符,并且请求由 proxy_pass、fastcgi_pass、uwsgi_pass、scgi_pass、memcached_pass 或 grpc_pass 其中的一个处理,则会执行响应特殊处理。
一个请求 URI 与响应(转发)的 URI 前缀路径相同,但是响应(转发)的 URI,不是以 / 结尾的,将会返回代码为 301 的永久重定向到 以 ‘/’ 结尾的请求 URI。如果不希望这样做,可以定精确匹配 location 来匹配 URI,如下面的例子:
参考链接
5 - ngx_http_proxy_module 模块学习笔记
介绍
The ngx_http_proxy_module module allows passing requests to another server.
ngx_http_proxy_module 模块允许转发请求到其他的服务器,即我们平时所说的反向代理。
proxy_pass 指令
官网语法说明
该指令用于设置请求匹配 location 后,转发的代理服务器的 protocol 和 address 以及可选 URI。protocol 可以指定使用 http 或者 https,adress 可以指定为域名或者 ip 地址和可选的端口号(没有端口则默认端口 80),使用例子如下:
请求的转发规则如下:
如果 proxy_pass 指令带有 URI,那么当请求需要转发到服务器时,请求 URI 与 location 匹配的前缀部分字符串,将会被替换成 proxy_pass 指令中指定的 URI。 故请求的最终转发地址等于:
proxy_pass-host + request-uri.replace前缀(location-prefix-uri, proxy_pass-uri)如果 proxy_pass 指令没有带有 URI,那么当请求转发到服务器时,转发后的 URI,就是请求匹配 location 的 URI。
访问 http://127.0.0.1/some/path 则转发后的地址为 http://127.0.0.1:8080/some/path
访问 http://127.0.0.1/some/path/test 则转发后的地址为 http://127.0.0.1:8080/some/path/test
访问 http://127.0.0.1/some/path?param=xxx 则转发后的地址为 http://127.0.0.1:8080/some/path?param=xxx
注意以下几种情况是 proxy_pass 指令不能携带 URI 的
- 如果 location 的匹配规则使用的是正则表达,proxy_pass 不能携带 URI
- 当在 location 中使用 rewrite 指令时,proxy_pass 指令不能携带 URI
- 在具名的 location 中 proxy_pass 指令不能携带 URI
- 在 if 条件中 proxy_pass 指令不能携带 URI
参考链接
6 - ngx_http_rewrite_module 模块学习笔记
介绍
ngx_http_rewrite_module 主要用于改变请求的 URI,通过 PCRE 正则表达式,return 指令,以及有选择的配置条件去改变。
模块中的 break、if、return、rewrite 和 set 指令会按以下顺序执行处理:
- 在同一级别指令语句块中按配置文件中出现的位置,按顺序执行;
- 如果对一个请求 URI 使用 rewrite 重定向,限制最多只能重定向 10 次。
一个 URL 请求到达 nginx 时,nginx 先会顺序执行 server 指令语句块中的 ngx_http_rewrite_module 指令集合,如果执行到 break 指令会直接返回 404,执行到 return 指令也会直接返回,执行到 rewrite 如果匹配 rewrite 表达式,将会重写请求的 URI 然后使用重写后的 URI 再去匹配 location。如果 server 指令语句块中没有 ngx_http_rewrite_module 指令集合或者有匹配不到的情况下,请求的 URL 则会去匹配 location。
ngx_http_rewrite_module 模块指令日志记录,记录在 error_log 文件中的,且日志的级别需要改成 notice 级别。rewrite 指令的日志还需要开启 rewrite_log。
break 指令
break 指令的官网语法如下:
break 用于终止 ngx_http_rewrite_module 指令集合,即同一个指令块作用域内的指令集合。使用例子说明如下:
if 指令
if 指令的官方语法如下:
if 指令支持的判断条件如下:
- 空字符串和 0 默认 false,在 1.0.1 版本之前,任何以 0 开头的字符串也被认为是 false
- 变量和字符串比较只支持 = 和 != 符号
- 支持变量和正则表达式匹配,
~符号表示区分大小写匹配,~*表示不区分大小写匹配,正则匹配支持非运算符!~和!~*,同时支持捕获组,捕获组可通过 $1..$9 变量获取值。 如果正则表示式包含}和;符号,那么正则表达式必须使用引号包起来。
- 支持使用
-f和!-f符号检查文件是否存在
- 支持使用
-d和!-d符号检查目录是否存在
- 支持使用
-e和!-e运算符检查文件、目录或符号链接是否存在
- 支持使用
-x和-x运算符检查可执行文件
- 不支持与或逻辑符号判断,但是可以通过变量拼接字符串的形式实现
rewrite 指令
rewrite 指令的官方语法如下:
If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.
如果指定的 regex 正则表达式匹配请求的 URI, URI 将会被替换成指定 replacement 字符串。重写指令按照它们在配置文件中出现的顺序依次执行。可以使用标志符来终止对指令的进一步处理。如果 repalcement 字符串以 “http://"、“https://” 或 “$scheme” 开头,则停止处理并将重定向返回给客户。
falg 可选值说明:
- last:表示 regex 正则表达式匹配到 URI 后立即停止处理当前 ngx_http_rewrite_module 指令集合,并用 replactment 后的 URI 匹配查找 location
- break:表示 regex 正则表达式匹配到 URI 停止处理当前 ngx_http_rewrite_module 指令集合,如同处理 break 指令一样
- redirect:返回一个状态是 302 的临时重定向
- permanent:返回一个状态是 301 的永久重定向
永久重定向和临时重定向的区别是:浏览器会缓存永久重定向会记录,比如浏览器第一次访问 A 链接,如果返回 301 永久重定向到 B 链接,浏览器就会缓存 A 链接到 B 链接重定向记录,当浏览器再访问 A 链接时,如果缓存中有重定向记录则会直接重定向到 B 链接。如果第一次访问返回的 302 临时重定到 B 链接,浏览器每次都会重新 A 链接由 nginx 判断是不是重定向。即永久重定向会读缓存减轻服务器压力,临时重定向不会缓存,因为是临时的,临时说明随时会改变的所以不能缓存起来。
rewrite 指令在 server 指令语句块的例子说明:
在 location 指令中的 rewrite 指令例子:
获取正则表达的值:
return 指令
return 的官方语法如下:
常用例子如下:
set 指令
set 指令用于定义变量,其官方语法如下:
常用例子如下: