GVKun编程网logo

nginx 配置 location 总结及 rewrite 规则写法(nginx配置location总结及rewrite规则写法)

24

此处将为大家介绍关于nginx配置location总结及rewrite规则写法的详细内容,并且为您解答有关nginx配置location总结及rewrite规则写法的相关问题,此外,我们还将为您介绍关

此处将为大家介绍关于nginx 配置 location 总结及 rewrite 规则写法的详细内容,并且为您解答有关nginx配置location总结及rewrite规则写法的相关问题,此外,我们还将为您介绍关于001-nginx基础配置-location、Rewrite、return 直接返回码、全局变量、Linux Nginx——地址重写Rewrite、Rewrite 指令、last,break详解、Nginx https 、Apache https、location详解、nginx https配置以及 api接口版本号rewrite apache rewrite nginx rewrite 规则 rewrite攻、nginx location rewrite 优先级问题的有用信息。

本文目录一览:

nginx 配置 location 总结及 rewrite 规则写法(nginx配置location总结及rewrite规则写法)

nginx 配置 location 总结及 rewrite 规则写法(nginx配置location总结及rewrite规则写法)

location 正则写法

一个示例:

location  = / {
  # 精确匹配 / ,主机名后面不能带任何字符串
  [ configuration A ] 
}

location  / {
  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  # 但是正则和最长字符串会优先匹配
  [ configuration B ] 
}

location /documents/ { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration C ] } location ~ /documents/Abc { # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration CC ] } location ^~ /images/ { # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配所有以 gif,jpg或jpeg 结尾的请求 # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则 [ configuration E ] } location /images/ { # 字符匹配到 /images/,继续往下,会发现 ^~ 存在 [ configuration F ] } location /images/abc { # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在 # F与G的放置顺序是没有关系的 [ configuration G ] } location ~ /images/abc/ { # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用 [ configuration H ] } location ~* /js/.*/\.js 
  • = 开头表示精确匹配
    如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。
  • ^~ 开头表示 uri 以某个常规字符串开头,不是正则匹配
  • ~ 开头表示区分大小写的正则匹配;
  • ~* 开头表示不区分大小写的正则匹配
  • / 通用匹配,如果没有其它匹配,任何请求都会匹配到

顺序 no 优先级:
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

上面的匹配结果
按照上面的 location 写法,以下的匹配示例成立:

  • / -> config A
    精确完全匹配,即使 /index.html 也匹配不了
  • /downloads/download.html -> config B
    匹配 B 以后,往下没有任何匹配,采用 B
  • /images/1.gif -> configuration D
    匹配到 F,往下匹配到 D,停止往下
  • /images/abc/def -> config D
    最长匹配到 G,往下匹配 D,停止往下
    你可以看到 任何以 /images/ 开头的都会匹配到 D 并停止,FG 写在这里是没有任何意义的,H 是永远轮不到的,这里只是为了说明匹配顺序
  • /documents/document.html -> config C
    匹配到 C,往下没有任何匹配,采用 C
  • /documents/1.jpg -> configuration E
    匹配到 C,往下正则匹配到 E
  • /documents/Abc.jpg -> config CC
    最长匹配到 C,往下正则顺序匹配到 CC,不会往下到 E

实际使用建议

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / { proxy_pass http://tomcat:8080/index } # 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项 # 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用 location ^~ /static/ { root /webroot/static/; } location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; } #第三个规则就是通用规则,用来转发动态请求到后端应用服务器 #非静态文件请求就默认是动态请求,自己根据实际把握 #毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了 location / { proxy_pass http://tomcat:8080/ } 

http://tengine.taobao.org/book/chapter_02.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Rewrite 规则

rewrite 功能就是,使用 nginx 提供的全局变量或自己设置的变量,结合正则表达式和标志位实现 url 重写以及重定向。rewrite 只能放在 server {},location {},if {} 中,并且只能对域名后边的除去传递的参数外的字符串起作用,例如 http://seanlook.com/a/we/index.php?id=1&u=str 只对 /a/we/index.php 重写。语法 rewrite regex replacement [flag];

如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用 proxy_pass 反向代理。

表明看 rewrite 和 location 功能有点像,都能实现跳转,主要区别在于 rewrite 是在同一域名内更改获取资源的路径,而 location 是对一类路径做控制访问或反向代理,可以 proxy_pass 到其他机器。很多情况下 rewrite 也会写在 location 里,它们的执行顺序是:

  1. 执行 server 块的 rewrite 指令
  2. 执行 location 匹配
  3. 执行选定的 location 中的 rewrite 指令

如果其中某步 URI 被重写,则重新循环执行 1-3,直到找到真实存在的文件;循环超过 10 次,则返回 500 Internal Server Error 错误。

flag 标志位

  • last : 相当于 Apache 的 [L] 标记,表示完成 rewrite
  • break : 停止执行当前虚拟主机的后续 rewrite 指令集
  • redirect : 返回 302 临时重定向,地址栏会显示跳转后的地址
  • permanent : 返回 301 永久重定向,地址栏会显示跳转后的地址

因为 301 和 302 不能简单的只返回状态码,还必须有重定向的 URL,这就是 return 指令无法返回 301,302 的原因了。这里 last 和 break 区别有点难以理解:

  1. last 一般写在 server 和 if 中,而 break 一般使用在 location 中
  2. last 不终止重写后的 url 匹配,即新的 url 会再从 server 走一遍匹配流程,而 break 终止重写后的匹配
  3. break 和 last 都能组织继续执行后面的 rewrite 指令

if 指令与全局变量

if 判断指令
语法为 if(condition){...},对给定的条件 condition 进行判断。如果为真,大括号内的 rewrite 指令将被执行,if 条件 (conditon) 可以是如下任何内容:

  • 当表达式只是一个变量时,如果值为空或任何以 0 开头的字符串都会当做 false
  • 直接比较变量和内容时,使用 =!=
  • ~ 正则表达式匹配,~* 不区分大小写的匹配,!~ 区分大小写的不匹配

-f!-f 用来判断是否存在文件
-d!-d 用来判断是否存在目录
-e!-e 用来判断是否存在文件或目录
-x!-x 用来判断文件是否可执行

例如:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下 if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1; } //如果cookie匹配正则,设置变量$id等于正则引用部分 if ($request_method = POST) { return 405; } //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302 if ($slow) { limit_rate 10k; } //限速,$slow可以通过 set 指令设置 if (!-f $request_filename){ break; proxy_pass http://127.0.0.1; } //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查 if ($args ~ post=140){ rewrite ^ http://example.com/ permanent; } //如果query string中包含"post=140",永久重定向到example.com location ~* \.(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jefflei.com www.leizhenfang.com; if ($invalid_referer) { return 404; } //防盗链 } 

全局变量
下面是可以用作 if 判断的全局变量

  • $args : #这个变量等于请求行中的参数,同 $query_string
  • $content_length : 请求头中的 Content-length 字段。
  • $content_type : 请求头中的 Content-Type 字段。
  • $document_root : 当前请求在 root 指令中指定的值。
  • $host : 请求主机头字段,否则为服务器名称。
  • $http_user_agent : 客户端 agent 信息
  • $http_cookie : 客户端 cookie 信息
  • $limit_rate : 这个变量可以限制连接速率。
  • $request_method : 客户端请求的动作,通常为 GET 或 POST。
  • $remote_addr : 客户端的 IP 地址。
  • $remote_port : 客户端的端口。
  • $remote_user : 已经经过 Auth Basic Module 验证的用户名。
  • $request_filename : 当前请求的文件路径,由 root 或 alias 指令与 URI 请求生成。
  • $scheme : HTTP 方法(如 http,https)。
  • $server_protocol : 请求使用的协议,通常是 HTTP/1.0 或 HTTP/1.1。
  • $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。
  • $server_name : 服务器名称。
  • $server_port : 请求到达服务器的端口号。
  • $request_uri : 包含请求参数的原始 URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
  • $uri : 不带请求参数的当前 URI,$uri 不包含主机名,如”/foo/bar.html”。
  • $document_uri : 与 $uri 相同。

例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:/var/www/html
$request_filename:/var/www/html/test1/test2/test.php

常用正则

  • . : 匹配除换行符以外的任意字符
  • ? : 重复 0 次或 1 次
  • + : 重复 1 次或更多次
  • * : 重复 0 次或更多次
  • \d :匹配数字
  • ^ : 匹配字符串的开始
  • $ : 匹配字符串的介绍
  • {n} : 重复 n 次
  • {n,} : 重复 n 次或更多次
  • [c] : 匹配单个字符 c
  • [a-z] : 匹配 a-z 小写字母的任意一个

小括号 () 之间匹配的内容,可以在后面通过 $1 来引用,$2 表示的是前面第二个 () 里的内容。正则里面容易让人困惑的是 \ 转义特殊字符。

rewrite 实例

例 1

http {
    # 定义image日志格式
    log_format imagelog ''[$time_local] '' $image_file '' '' $image_type '' '' $body_bytes_sent '' '' $status; # 开启重写日志 rewrite_log on; server { root /home/www; location / { # 重写规则信息 error_log logs/rewrite.log notice; # 注意这里要用‘’单引号引起来,避免{} rewrite ''^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$'' /data?file=$3.$4; # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行 set $image_file $3; set $image_type $4; } location /data { # 指定针对图片的日志格式,来分析图片类型和大小 access_log logs/images.log mian; root /data/images; # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里 try_files /$arg_file /image404.html; } location = /image404.html { # 图片不存在返回特定的信息 return 404 "image not found\n"; } } 

对形如 /images/ef/uh7b3/test.png 的请求,重写到 /data?file=test.png,于是匹配到 location /data,先看 /data/images/test.png 文件存不存在,如果存在则正常响应,如果不存在则重写 tryfiles 到新的 image404 location,直接返回 404 状态码。

例 2

rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;

对形如 /images/bla_500x400.jpg 的文件请求,重写到 /resizer/bla.jpg?width=500&height=400 地址,并会继续尝试匹配 location。

例 3
见 ssl 部分页面加密 。

001-nginx基础配置-location、Rewrite、return 直接返回码、全局变量

001-nginx基础配置-location、Rewrite、return 直接返回码、全局变量

一、基础语法

1.1、location block 的基本语法形式是:

    location [=|~|~*|^~|@] pattern { ... } 即:location [=|~|~*|^~|...] /url/  {... ...}

[=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)

选项参数

  =   表示精准匹配

  ^~   表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

  ~   表示区分大小写正则匹配

  ~*   表示不区分大小写正则匹配

  !~   分别为区分大小写不匹配

  !~*  不区分大小写不匹配

其他参数:

  -f 判断文件是否存在

  -d 判断目录是否存在

  -x 判断文件是否可执行

  -e 判断文件、目录、链接是否存在

1.2、正则基础

  ^  匹配字符串的开始位置;

  $  匹配字符串的结束位置;

  .*   .匹配任意字符,*匹配数量0到正无穷;

  \.  斜杠用来转义,\.匹配 .    特殊使用方法

  (值1|值2|值3|值4)  或匹配模式,例:(jpg|gif|png|bmp)匹配jpg或gif或png或bmp

  !  非 

二、基本示例

2.1、静态资源匹配

    # 静态资源
        charset utf-8;
        index index.html index.htm;

        #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
            root /export/html/webapps;
            #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
            expires      7d;
        }

    说明: ~ 区分大小写正则匹配;.*    匹配可以任意字符,以及不论个数;\. 匹配一个 .;(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 匹配任意括号中模式并且结束;

三、要点

3.1、location匹配顺序

  1. "="前缀指令匹配,如果匹配成功,则停止其他匹配
  2. 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配)
  3. 正则表达式指令匹配,按照配置文件里的顺序,成功就停止其他匹配
  4. 如果第三步中有匹配成功,则使用该结果,否则使用第二步结果

  多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

3.2、注意点

  1. 匹配的顺序是先匹配普通字符串,然后再匹配正则表达式。另外普通字符串匹配顺序是根据配置中字符长度从长到短,也就是说使用普通字符串配置的location顺序是无关紧要的,反正最后nginx会根据配置的长短来进行匹配,但是需要注意的是正则表达式按照配置文件里的顺序测试。找到第一个比配的正则表达式将停止搜索。

  2. 一般情况下,匹配成功了普通字符串location后还会进行正则表达式location匹配。有两种方法改变这种行为,其一就是使用“=”前缀,这时执行的是严格匹配,并且匹配成功后立即停止其他匹配,同时处理这个请求;另外一种就是使用“^~”前缀,如果把这个前缀用于一个常规字符串那么告诉nginx 如果路径匹配那么不测试正则表达式。

3.3、匹配模式及顺序location modifier 【依次匹配】

  location = /uri    =开头表示精确匹配,只有完全匹配上才能生效。

  location ^~ /uri   ^~ 开头对URL路径进行前缀匹配,并且在正则之前。

  location ~ pattern  ~开头表示区分大小写的正则匹配。

  location ~* pattern  ~*开头表示不区分大小写的正则匹配。

  location /uri     不带任何修饰符,也表示前缀匹配,但是在正则匹配之后。

  location /      通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default。 

3.3.1、【=】精确匹配

server {
    server_name website.com;
    location = /abcd {
    […]
    }
}

匹配情况:

    http://website.com/abcd        # 正好完全匹配

    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配

    http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2

    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配

    http://website.com/abcde    # 不匹配,因为不是完全匹配

3.3.2、【none】不写location modifier

可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。

server {
    server_name website.com;
    location /abcd {
    […]
    }
}

匹配情况:

    http://website.com/abcd        # 正好完全匹配

    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配

    http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2

    http://website.com/abcd/    # 末尾存在反斜杠(trailing slash)也属于匹配范围内

    http://website.com/abcde    # 仍然匹配,因为 URI 是以 pattern 开头的

3.3.3、【~】对大小写敏感,且 pattern 须是正则表达式

server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}

匹配情况:

    http://website.com/abcd        # 完全匹配

    http://website.com/ABCD        # 不匹配,~ 对大小写是敏感的

    http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2

    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$

    http://website.com/abcde    # 不匹配正则表达式 ^/abcd$

注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

3.3.4、【~*】与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式

server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}

匹配情况:

    http://website.com/abcd        # 完全匹配

    http://website.com/ABCD        # 匹配,这就是它不区分大小写的特性

    http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2

    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$

    http://website.com/abcde    # 不匹配正则表达式 ^/abcd$

3.3.5、【^~】

匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)

3.3.6、【@】

用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page

3.4、搜索顺序以及生效优先级

写在配置文件中每个 Server 块中的 Location 块的次序是不重要的,Nginx 会按 location modifier 的优先级来依次用 URI 去匹配 pattern ,顺序如下:

    1. =

    2. (None)    如果 pattern 完全匹配 URI(不是只匹配 URI 的头部)

    3. ^~

    4. ~ 或 ~*

    5. (None)    pattern 匹配 URI 的头部

3.5、匹配案例

location  = / {

  # 精确匹配 / ,主机名后面不能带任何字符串

  [ configuration A ] 

}

location  / {

  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求

  # 但是正则和最长字符串会优先匹配

  [ configuration B ] 

}

location /documents/ {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration C ] 

}

location ~ /documents/Abc {

  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索

  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条

  [ configuration CC ] 

}

location ^~ /images/ {

  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。

  [ configuration D ] 

}

location ~* \.(gif|jpg|jpeg)$ {

  # 匹配所有以 gif,jpg或jpeg 结尾的请求

  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则

  [ configuration E ] 

}

location /images/ {

  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在

  [ configuration F ] 

}

location /images/abc {

  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在

  # F与G的放置顺序是没有关系的

  [ configuration G ] 

}

location ~ /images/abc/ {

  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用

    [ configuration H ] 

}

location ~* /js/.*/\.js

四、nginx  Rewrite相关

4.1、rewrite指令的最后一项参数为flag标记

flag标记有:

  1.last    相当于apache里面的[L]标记,表示rewrite。

  2.break本条规则匹配完成后,终止匹配,不再匹配后面的规则。

  3.redirect  返回302临时重定向,浏览器地址会显示跳转后的URL地址。

  4.permanent  返回301永久重定向,浏览器地址会显示跳转后的URL地址。

  使用last和break实现URI重写,浏览器地址栏不变。而且两者有细微差别,使用alias指令必须用last标记;使用proxy_pass指令时,需要使用break标记。Last标记在本条rewrite规则执行完毕后,会对其所在server{......}标签重新发起请求,而break标记则在本条规则匹配完成后,终止匹配。

  例如:如果我们将类似URL/photo/123456 重定向到/path/to/photo/12/1234/123456.png

  rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})"/path/to/photo/$1/$1$2/$1$2$3.png ;

4.2、Rewrite 规则相关指令

1.break指令

  使用环境:server,location,if;
  该指令的作用是完成当前的规则集,不再处理rewrite指令。

2.if指令

  使用环境:server,location
  该指令用于检查一个条件是否符合,如果条件符合,则执行大括号内的语句。If指令不支持嵌套,不支持多个条件&&和||处理。

3.return指令

  语法:return code ;
  使用环境:server,location,if;
  该指令用于结束规则的执行并返回状态码给客户端。
  示例:如果访问的URL以".sh"或".bash"结尾,则返回403状态码

location ~ .*\.(sh|bash)?$ {
  return 403;
}

4.rewrite 指令

  语法:rewrite regex replacement flag
  使用环境:server,location,if
  该指令根据表达式来重定向URI,或者修改字符串。指令根据配置文件中的顺序来执行。注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句,示例如下:

if( $host ~* www\.(.*) ){
    set $host_without_www $1;
    rewrite ^(.*)$  http://$host_without_www$1permanent;
}

5.Set指令

  语法:setvariable value ; 默认值:none; 使用环境:server,location,if;
  该指令用于定义一个变量,并给变量赋值。变量的值可以为文本、变量以及文本变量的联合。
  示例:set$varname "hello world";

6.Uninitialized_variable_warn指令

  语法:uninitialized_variable_warnon|off
  使用环境:http,server,location,if
  该指令用于开启和关闭未初始化变量的警告信息,默认值为开启。

4.3、示例

1.当访问的文件和目录不存在时,重定向到某个php文件

if( !-e $request_filename ){
    rewrite ^/(.*)$ index.php last;
}

 

2.目录对换 /123456/xxxx  ====>  /xxxx?id=123456

rewrite ^/(\d+)/(.+)/  /$2?id=$1 last;

 

3.如果客户端使用的是IE浏览器,则重定向到/ie目录下

if( $http_user_agent  ~ MSIE){
    rewrite ^(.*)$ /ie/$1 break;
}

4.禁止访问多个目录

location ~ ^/(cron|templates)/ {
    deny all;
    break;
}

5.禁止访问以/data开头的文件

location ~ ^/data {
    deny all;
}

6.禁止访问以.sh,.flv,.mp3为文件后缀名的文件

location ~ .*\.(sh|flv|mp3)$ {
    return 403;
}

7.设置某些类型文件的浏览器缓存时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    expires 30d;
}
location ~ .*\.(js|css)$ {
    expires 1h;
}

8.给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

location ~(favicon.ico) {
    log_not_found off;
    expires 99d;
    break;
}
location ~(robots.txt) {
    log_not_found off;
    expires 7d;
    break;
}

9.设定某个文件的过期时间;这里为600秒,并不记录访问日志

location ^~ /html/scripts/loadhead_1.js {
    access_log  off;
    root /opt/lampp/htdocs/web;
    expires 600;
    break;
}

10.文件反盗链并设置过期时间
这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求

“rewrite ^/ http://img.linuxidc.net/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
location ~*^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
    valid_referers none blocked *.linuxidc.com*.linuxidc.net localhost 208.97.167.194;
    if ($invalid_referer) {
        rewrite ^/ http://img.linuxidc.net/leech.gif;
        return 412;
        break;
    }
    access_log  off;
    root /opt/lampp/htdocs/web;
    expires 3d;
    break;
}

11.只允许固定ip访问网站,并加上密码

root /opt/htdocs/www;
allow  208.97.167.194; 
allow  222.33.1.2; 
allow  231.152.49.4;
deny  all;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;

12将多级目录下的文件转成一个文件,增强seo效果

/job-123-456-789.html 指向/job/123/456/789.html

rewrite^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;

13.文件和目录不存在的时候重定向:

if (!-e $request_filename) {
  proxy_pass http://127.0.0.1;
}

14.将根目录下某个文件夹指向2级目录
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

上面例子有个问题是访问/shanghai时将不会匹配

rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

if (-d $request_filename){
    rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent;
}
rewrite ^/([0-9a-z]+)job$ /$1job/permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

15.域名跳转

server{
    listen      80;
    server_name  jump.linuxidc.com;
    index index.html index.htm index.php;
    root  /opt/lampp/htdocs/www;
    rewrite ^/ http://www.linuxidc.com/;
    access_log  off;
}

16.多域名转向

server_name  www.linuxidc.com www.linuxidc.net;
index index.html index.htm index.php;
root  /opt/lampp/htdocs;
if ($host ~ "linuxidc\.net") {
    rewrite ^(.*) http://www.linuxidc.com$1permanent;
}

五、return

1: 返回错误码

eg:  单独配置返回返回码场景

nginx 配置如下:

location = /test {
         return 403 ;
    }

 

通过返回的页面为: 403 Forbidden 正常的403错误返回码报错

可以看出 对于单独返回返回码的场景, 可以进行过滤拦截场景使用)

2: 通过return  返回 文本信息和 json

location ^~ /pic {
          default_type text/html ;
          return 200  ''test test test '';
 }

 

进行 http://192.168.1.19:8080/pic/test 进行测试,页面返回编辑的内容: test test test 

location ^~ /pic {
          default_type application/json ;
          return 200  ''{"name":"nanjing_wuxu","result":"success"}'';
        }

 

http://192.168.1.19:8080/pic/test 进行测试,页面返回编辑的内容:{"name":"nanjing_wuxu","result":"success"}

可以理解,可以通过return 为请求方之间返回编辑的文本等信息

3: 直接跳转功能

location ^~ /pic {
          return http://192.168.1.19:8080/aa.jpg;
        }

 

进行http://192.168.1.19:8080/pic/test 进行测试,返回的是aa.jsp图片, 从日志中也可以进行查看,进行了跳转

 [26/Jan/2018:17:05:33 +0800] "GET /pic/test HTTP/1.1" 302 153 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
 [26/Jan/2018:17:05:33 +0800] "GET /aa.jpg HTTP/1.1" 200 879394 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"

五、nginx全局变量

arg_PARAMETER    #这个变量包含GET请求中,如果有变量PARAMETER时的值。
args                    #这个变量等于请求行中(GET请求)的参数,如:foo=123&bar=blahblah;
binary_remote_addr #二进制的客户地址。
body_bytes_sent    #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
content_length    #请求头中的Content-length字段。
content_type      #请求头中的Content-Type字段。
cookie_COOKIE    #cookie COOKIE变量的值
document_root    #当前请求在root指令中指定的值。
document_uri      #与uri相同。
host                #请求主机头字段,否则为服务器名称。
hostname          #Set to themachine’s hostname as returned by gethostname
http_HEADER
is_args              #如果有args参数,这个变量等于”?”,否则等于”",空值。
http_user_agent    #客户端agent信息
http_cookie          #客户端cookie信息
limit_rate            #这个变量可以限制连接速率。
query_string          #与args相同。
request_body_file  #客户端请求主体信息的临时文件名。
request_method    #客户端请求的动作,通常为GET或POST。
remote_addr          #客户端的IP地址。
remote_port          #客户端的端口。
remote_user          #已经经过Auth Basic Module验证的用户名。
request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
request_method    #GET或POST
request_filename  #当前请求的文件路径,由root或alias指令与URI请求生成。
request_uri          #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
scheme                #HTTP方法(如http,https)。
server_protocol      #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
server_addr          #服务器地址,在完成一次系统调用后可以确定这个值。
server_name        #服务器名称。
server_port          #请求到达服务器的端口号。

 

Linux Nginx——地址重写Rewrite、Rewrite 指令、last,break详解、Nginx https 、Apache https、location详解

Linux Nginx——地址重写Rewrite、Rewrite 指令、last,break详解、Nginx https 、Apache https、location详解

Nginx 地址重写 rewrite

  • 什么是Rewrite

Rewrite对称URL Rewrite,可理解为URL重写,是把传入Web的请求重定向到其他URL。

URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。
从安全角度上看,如果在URL中暴露太多的参数,会导致信息泄露,会被一些***利用对系统造成一定的破坏,故静态化的URL地址可以带来更高的安全性。

  • Rewrite 相关指令

Nginx Rewrite 相关指令有:if、rewrite、set、return

if 语句
应用环境

server,location

语法:

if (condition) { … }if 支持的条件判断匹配符号
~ 					正则匹配 (区分大小写)    ~* 				    正则匹配 (不区分大小写)!~                  正则不匹配 (区分大小写)!~*		            正则不匹配  (不区分大小写)-f 和!-f 		    用来判断是否存在文件
-d 和!-d 		    用来判断是否存在目录
-e 和!-e 		    用来判断是否存在文件或目录
-x 和!-x 		    用来判断文件是否可执行#在匹配过程中可以引用一些Nginx的全局变量$args				请求中的参数$document_root	    针对当前请求的根路径设置值$host				请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名$limit_rate			对连接速率的限制$request_method		请求的方法,比如"GET"、"POST"等$remote_addr		客户端地址$remote_port		客户端端口号$remote_user		客户端用户名,认证用$request_filename   当前请求的文件路径名$request_uri		当前请求的文件路径名$query_string		与$args相同$scheme				用的协议,比如http或者是https$server_protocol	请求的协议版本,"HTTP/1.0"或"HTTP/1.1"$server_addr 		服务器地址,若没有指明服务器地址,使用这个变量将发起一次系统调用以取得地址$server_name		请求到达的服务器名$document_uri 		URI地址$server_port 		请求到达的服务器端口号

Rewrite flag
rewrite 指令根据表达式来重定向URI、或者修改字符串,可应用于server,location, if环境下每行rewrite指令最后跟一个flag标记。

支持的flag标记:

last 		相当于Apache里的[L]标记,表示完成rewrite默认为lastbreak 		本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 	返回302浏览器地址会显示跳转后的URL地址
permanent 	返回301浏览器地址会显示跳转后URL地址

www.vc.com --> www.jd.com

redirect 和 permanent区别是返回的不同方式的重定向,对于客户端一般状态下是无区别的;而对于搜索引擎,相对来看301的重定向更加友好,如果把一个地址采用301跳转方式跳转,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。使用302重定向时,搜索引擎有时会判断跳转前后哪个网址更直观,然后显示哪个,如果它判断跳转前的URL更好,也许地址栏不会更改。

  • Rewrite匹配参考示例
#本地解析hosts文件(windows)192.168.62.153 www.testpm.com[root@Nginx html]# pwd/html[root@Nginx html]# lsa  b[root@Nginx html]# cat a/1.html 1.html[root@Nginx html]# cat b/2.html 22# http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.htmlserver {
    listen       80;
    server_name  www.testpm.com;

        location /a {
        root /html;
        index   1.html index.htm;
        rewrite .* /b/2.html permanent;
        }

        location /b {
        root    /html;
        index   2.html index.htm;
        }}---------------------[root@mycat html]# pwd/var/www/html 
[root@mycat html]# ls2020  2021[root@mycat html]# cat 2020/a/1.html 2020[root@mycat html]# cat 2021/a/1.html 2021# http://www.testpm.com/2021/a/1.html ==> http://www.testpm.com/2020/a/1.htmlserver {
    listen       80;
    server_name  www.testpm.com;

    location /2021/a {
        root    /var/www/html;
        index   1.html index.hml;
        rewrite ^/2021/(.*)$ /2020/$1 permanent;
    }

    location /2020/a {
        root    /var/www/html;
        index   1.html index.htl;
    }}---------------------# http://www.vc.com/a/1.html ==> http://jd.comlocation /a {
        root    /html;
        if ($host ~* vc.com ) {
        rewrite .* http://jd.com permanent;
        }}---------------------# http://www.youngfit.com/a/1.html ==> http://jd.com/a/1.htmllocation /a {
        root /html;
        if ( $host ~* youngfit.com ){
        rewrite .* http://jd.com$request_uri permanent;
        }}---------------------# 在访问目录后添加/  (如果目录后已有/,则不加/)[root@Nginx-server c]# pwd/usr/share/Nginx/html/a/b/c

http://www.vc.com/a/b/c--->http://www.vc.com/a/b/c/# http://www.vc.com/a/b/c# $1: /a/b/# $2: c# http://$host$1$2/location /a/b/c {
        root    /usr/share/Nginx/html;
        index   index.html index.hml;
        if (-d $request_filename) {
        rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
        }}---------------------[root@Nginx html]# pwd/usr/share/Nginx/html[root@Nginx html]# ls50x.html  index.html  index.html.bak1  reg[root@Nginx html]# cat reg/login.html login# http://www.vc.com/login/qf.html ==>  http://www.vc.com/reg/login.html?user=qf
    location /login {
        root   /usr/share/Nginx/html;
        rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;
        }
    location /reg {
        root /usr/share/Nginx/html;
        index login.html;
        }---------------------[root@Nginx-server 33]# pwd/html/qf/11/22/33[root@Nginx-server 33]# cat 1.html hello Nginx#http://www.vc.com/qf/11-22-33/1.html  ==>  http://www.vc.com/qf/11/22/33/1.htmllocation /qf {
            rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent;
        }

        location /qf/11/22/33 {
                root /html;
                index   1.html;
        }

  • set 指令

set 指令是用于定义一个变量,并且赋值

应用环境:

server,location,if

  • 应用示例
#http://alice.testpm.com ==> http://www.testpm.com/alice#http://jack.testpm.com ==> http://www.testpm.com/jack[root@Nginx-server conf.d]# cd /usr/share/Nginx/html/[root@Nginx-server html]# mkdir jack alice[root@Nginx-server html]# echo "jack.." >> jack/index.html[root@Nginx-server html]# echo "alice.." >> alice/index.html本地解析域名host文件
192.168.62.153 www.testpm.com
192.168.62.153 alice.testpm.com
192.168.62.153 jack.testpm.com
编辑配置文件:
server {
    listen       80;
    server_name  www.testpm.com;

    location / {
         root   /usr/share/Nginx/html;
         index  index.html index.htm;
         if ( $host ~* ^www.testpm.com$) {
                break;
                }

         if ( $host ~* "^(.*)\.testpm\.com$" ) {
                set $user $1;
                rewrite .* http://www.testpm.com/$user permanent;
                }
        }
    location /jack {
         root /usr/share/Nginx/html;
         index  index.html index.hml;
        }
    location /alice {
         root /usr/share/Nginx/html;
         index index.html index.hml;
        }}

  • return 指令

return 指令用于返回状态码给客户端

server,location,if

  • 应用示例:
#如果访问的.sh结尾的文件则返回403操作拒绝错误http://www.testpm.com/1.sh     返回403
server {
    listen       80;
    server_name  www.testpm.com;
    #access_log  /var/log/Nginx/http_access.log  main;

    location / {
        root   /usr/share/Nginx/html;
        index  index.html index.htm;
        }

    location ~* \.sh$ {
        return 403;
        }}---------------------#80 ======> 443:80转443端口server {
    listen       80;
    server_name  www.testpm.cn;
    access_log  /var/log/Nginx/http_access.log  main;
    return 301 https://www.testpm.cn$request_uri;}server {
    listen 443 ssl;
    server_name www.testpm.cn;
    access_log  /var/log/Nginx/https_access.log  main;

    #ssl on;
    ssl_certificate   /etc/Nginx/cert/2447549_www.testpm.cn.pem;
    ssl_certificate_key  /etc/Nginx/cert/2447549_www.testpm.cn.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;

    location / {
        root  /usr/share/Nginx/html;
        index index.html index.htm;
    }}[root@Nginx-server ~]# curl -I http://www.testpm.cnHTTP/1.1 301 Moved Permanently
Server: Nginx/1.16.0
Date: Wed, 03 Jul 2021 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.testpm.cn/

  • ast,break详解

在这里插入图片描述

[root@localhost test]# cat  /etc/Nginx/conf.d/last_break.conf server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/Nginx/html;
        index  index.html index.htm;
    }
 
    location /break/ {
        root /usr/share/Nginx/html;
        rewrite .* /test/break.html break;
    }

    location /last/ {
        root /usr/share/Nginx/html;
        rewrite .* /test/last.html last;
    }

    location /test/ {
        root /usr/share/Nginx/html;
        rewrite .* /test/test.html break;
    }}[root@localhost conf.d]# cd /usr/share/Nginx/html/[root@localhost html]# mkdir test[root@localhost html]# echo "last" > test/last.html[root@localhost html]# echo "break" > test/break.html[root@localhost html]# echo "test" > test/test.htmlhttp://192.168.62.159/break/break.html
http://192.168.62.159/last/last.html

  • 注意:

last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
break 标记则在本条规则匹配完成后停止匹配,不再做后续的匹配。

  • Nginx 的 https ( rewrite )
server {
        listen       80;
        server_name  *.vip9999.top vip9999.top;

        if ($host ~* "^www.vip9999.top$|^vip9999.top$" ) {
                return 301 https://www.vip9999.top$request_uri;
        }

        if ($host ~* "^(.*).vip9999.top$" ) {
                set $user $1;
                return 301 https://www.vip9999.top/$user;
        }

    }

    # Settings for a TLS enabled server.
    server {
        listen       443 ssl;
        server_name  www.vip9999.top;

        location / {
                root      /usr/share/Nginx/html;
                index     index.PHP index.html;
        }

        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.PHP$ {
            root           /usr/share/Nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.PHP;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        ssl on;
        ssl_certificate cert/214025315060640.pem;
        ssl_certificate_key cert/214025315060640.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        }

  • Apache 的 https ( rewrite )拓展
[root@localhost ~]# yum -y install httpd mod_ssl[root@localhost ~]# vim /etc/httpd/conf.d/vip9999.conf

在这里插入图片描述

  • Nginx的location指令详解

Nginx 的 HTTP 配置主要包括三个区块,结构如下:

http {                      # 这个是协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
    server {             # 这个是服务级别
      listen 80;
      server_name localhost;
        location / {  # 这个是请求级别
          root html;
          index index.html index.htm;
        }
      }}

  • location详解

1、location 区段
location 在 server 模块中配置,根据不同的 URl使用不同的配置,处理不同的请求。
location 安装顺序匹配,会被第一个匹配的location 处理。

语法如下:

location [=|~|~*|^~|@] pattern{……}

2、location 前缀

=    表示精确匹配,优先级也是最高的 
^~   表示url以某个常规字符串开头,理解为匹配url路径即可 
~    表示区分大小写的正则匹配  
~*   表示不区分大小写的正则匹配!~   表示区分大小写不匹配的正则!~*  表示不区分大小写不匹配的正则
/    通用匹配,任何请求都会匹配到

3、location 配置示例

本地解析域名host
1、没有修饰符 表示:必须以指定模式开始

[root@Nginx-server Nginx]# pwd/home/www/Nginx[root@Nginx-server Nginx]# cat abc/2.html 2.html

server {
    listen       80;
    server_name  www.vc.com;

    location  /abc {
        root    /home/www/Nginx;
        index   2.html;
        }}

以下是对的:
http://www.vc.com/abc

2、=表示:必须与指定的模式精确匹配

server {
    listen       80;
    server_name  www.testpm.cn;
    access_log  /var/log/Nginx/http_access.log  main;

    location / {
        root /usr/share/Nginx/html;
        index a.html index.htm;
    }
    
    location = / {
        root /usr/share/Nginx/html;
        index b.html index.htm;
    }}

进行测试:
http://www.testpm.cn
http://www.testpm.cn/a.html

3、~ 表示:指定的正则表达式要区分大小写

[root@Nginx-server Nginx]# pwd/home/www/Nginx[root@Nginx-server Nginx]# lsabc  ABC[root@Nginx-server Nginx]# cat abc/2.html abc[root@Nginx-server Nginx]# cat ABC/2.html ABC

server {
        server_name localhost;
        location ~ /abc {
                root /home/www/Nginx;
                index 2.html index.html;
        }}

http://192.168.62.153/abc/

4、~* 表示:指定的正则表达式不区分大小写

[root@Nginx-server Nginx]# pwd/home/www/Nginx[root@Nginx-server Nginx]# lsabc  ABC[root@Nginx-server Nginx]# cat abc/2.html abc[root@Nginx-server Nginx]# cat ABC/2.html ABC

server {
        server_name localhost;
        location ~* /abc {
                root /home/www/Nginx;
                index 2.html index.html;
        }}

http://192.168.62.153/ABC/

5、^~ :类似于无修饰符的行为,也是以指定模式开始。
查找顺序和优先级

#  = 大于 ^~  大于 ~|~*|!~|!~* 大于 /#  location配置的情况下匹配顺序:首先匹配 =,其次匹配^~, 其次正则匹配,最后是 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。-------------------------------------------(1) =:表示完全匹配;(2) ^~:匹配URI的前缀,如果一个URI同时满足两个规则的话,匹配最长的规则;(3) ~:匹配正则表达式,大小写敏感;(4) ~*:匹配正则表达式,大小写不敏感;
优先级:(1)> (2) > (3) = (4)---------------------------
location 区段匹配示例

location = / {
  # 只匹配 / 的查询.
  [ configuration A ]}location / {
  # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。
  [ configuration B ]}location ^~ /images/ {
  # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
  [ configuration C ]}location ~* \.(gif|jpg|jpeg)$ {
  # 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将在Configuration C中处理。
  [ configuration D ]} 各请求的处理如下例:
    / → configuration A
    /documents/document.html → configuration B
    /images/1.gif → configuration C
    /documents/1.jpg → configuration D

4、root 、alias 指令区别

location /img {
    alias /var/www/image;}#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件location /img {
    root /var/www/image;} #若按照这种配置的话,则访问/img/目录下的文件时,Nginx会去/var/www/image/img/目录下找文件。

alias 是一个目录别名(自定义名称)的定义;

root 则是最上层目录的定义。

另一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root有无都可。

nginx https配置以及 api接口版本号rewrite apache rewrite nginx rewrite 规则 rewrite攻

nginx https配置以及 api接口版本号rewrite apache rewrite nginx rewrite 规则 rewrite攻

  • 在做APP的接口设计时,需要考虑不同版本会采用不同的接口API,调研了几种方式之后,采用了在http header里面增加application/json;version=vxx的方式来实现版本控制,这样做的好处是地址不用做变更,客户端只需要在header中增加声明使用的版本即可. 由于采用https是大势所趋,后台也增加了对https的支持, APP和后台的前端机器采用https通信, 前端机器到内网之间的通信还是走正常的http.
  • 版本控制部分的例子
  • https 部分配置,需要nginx增加openssl支持,相关的key生成步骤网上比较多.

以上就介绍了nginx https配置以及 api接口版本号rewrite,包括了rewrite,https方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

nginx location rewrite 优先级问题

nginx location rewrite 优先级问题

目的: www.test.com/abcd   跳转到  www.test.com/search/searchWd.do?wd=abcd

abcd 要符合正则 [0-9A-Za-z]{4,30}

location ~ ^/[0-9A-Za-z]{4,30}$/ {

rewrite "^/(.+)$" http://test.com/ search/searchWd.do?wd=$1 last;

}

 

        location / {

#if ($uri ~ "^/[0-9]{1,30}$") {

#rewrite "^/(.+)$" http://test.com/ search/searchWd.do?wd=$1 last;

#}

proxy_pass http://test.com;

}

目前情况:运行下面一个 location

小弟菜鸟,学习中……………… 请各位指教

 

关于nginx 配置 location 总结及 rewrite 规则写法nginx配置location总结及rewrite规则写法的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于001-nginx基础配置-location、Rewrite、return 直接返回码、全局变量、Linux Nginx——地址重写Rewrite、Rewrite 指令、last,break详解、Nginx https 、Apache https、location详解、nginx https配置以及 api接口版本号rewrite apache rewrite nginx rewrite 规则 rewrite攻、nginx location rewrite 优先级问题的相关知识,请在本站寻找。

本文标签: