重定向 Rewrite
一、介绍
- Rewrite根据nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写和者重定向。
- Rewrite和location类似,都可以实现跳转,区别是rewrite是在同一域名内更改url,而location是对同类型匹配路径做控制访问,或者proxy_pass代理到其他服务器。
- Rewrite和location执行顺序:
- 执行server下的rewrite
- 执行location匹配
- 执行location下的rewrite
二、语法和参数说明
rewrite语法格式
| rewrite <regex> <replacement> <flag>; |
| 关键字 正则表达式 代替的内容 重写类型 |
| |
| Rewrite:一般都是rewrite |
| Regex:可以是字符串或者正则来表示想要匹配的目标URL |
| Replacement:将正则匹配的内容替换成replacement |
| Flag:flag标示,重写类型: |
| - last:本条规则匹配完成后,继续向下匹配新的location URI规则;相当于Apache里德(L)标记,表示完成rewrite,浏览器地址栏URL地址不变;一般写在server和if中; |
| - break:本条规则匹配完成后,终止匹配,不再匹配后面的规则,浏览器地址栏URL地址不变;一般使用在location中; |
| - redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址; |
| - permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址; |
使用示例如下
| server { |
| |
| rewrite /last.html /index.html last; |
| |
| |
| rewrite /break.html /index.html break; |
| |
| |
| rewrite /redirect.html /index.html redirect; |
| |
| |
| rewrite /permanent.html /index.html permanent; |
| |
| |
| rewrite ^/html/(.+?).html$ /post/$1.html permanent; |
| |
| |
| rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent; |
| |
| |
| rewrite ^/(.*) http://www.jd.com/$1 permanent; |
| } |
IF判断和内置全局环境变量
- if判断
当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false直接比较变量和内容时,使用=
或!=~
正则表达式匹配,~*
不区分大小写的匹配,!~
区分大小写的不匹配 2. 全局环境变量
| $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相同。 |
| |
| 例子: |
| URL:http://localhost:81/download/stat.php?id=1585378&web_id=1585378 |
| Server_Dir:/var/www/html |
| $host:localhost |
| $server_port:81 |
| $request_uri:/download/stat.php?id=1585378&web_id=1585378 |
| $document_uri:/download/stat.php |
| $document_root:/var/www/html |
| $request_filename:/var/www/html/download/stat.php |
| |
| |
| if (!-f $request_filename) { |
| return 400; |
| } |
| |
| |
| if ( $host != "www.jd.com" ){ |
| rewrite ^/(.*)$ https://www.jd.com/$1 permanent; |
| } |
| |
| |
| if ($request_method = POST) { |
| return 405; |
| } |
| |
| |
| if ($args ~ a=1) { |
| rewrite ^ http://example.com/ permanent; |
| } |
例如,文件名及参数重写。当访问/index.html
时,会转发到/test.html
| |
| location = /index.html { |
| |
| set $name test; |
| |
| |
| if ($args ~* name=(\w+?)(&|$)) { |
| set $name $1; |
| } |
| |
| |
| rewrite ^ /$name.html permanent; |
| } |
例如,隐藏真实目录。用 /html_test
来掩盖真实路径/html
| |
| server { |
| root /var/www/html; |
| |
| location / { |
| |
| rewrite /html_test /html break; |
| } |
| |
| |
| location /html { |
| return 403; |
| } |
| } |
禁止指定IP访问
| location / { |
| if ($remote_addr = 192.168.1.253) { |
| return 403; |
| } |
| } |
如果请求的文件不存在,则反向代理到localhost 。这里的break也是停止继续rewrite
| if (!-f $request_filename){ |
| break; |
| proxy_pass http: |
| } |
如果请求的文件不存在,则反向代理到localhost 。这里的break也是停止继续rewrite
| if (!-f $request_filename){ |
| break; |
| proxy_pass http: |
| } |
对/images/bla_500x400.jpg文件请求,重写到/resizer/bla.jpg?width=500&height=400地址,并会继续尝试匹配location。
rewrite ^/images/(.*)_(\d+)x(\d+)\.(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last;
反向代理 Proxy_Pass
Proxy_pass作用是nginx的反向代理,用的是nginx的Proxy模块。 具体使用示例如下,仅改动 proxy_pass
部分。
| |
| location /proxy/ { |
| proxy_pass http://127.0.0.1; |
| } |
| |
| |
| |
| |
| location /proxy/ { |
| proxy_pass http://127.0.0.1; |
| } |
| |
| |
| |
| |
| location /proxy/ { |
| proxy_pass http://127.0.0.1/aaa/; |
| } |
| |
| |
| |
| |
| location /proxy/ { |
| proxy_pass http://127.0.0.1/aaa; |
| } |
| |
参数
proxy_set_header Host $host;
作用web服务器上有多个站点时,用该参数header来区分反向代理哪个域名。比如下边的代码举例。proxy_set_header X-Forwarded-For $remote_addr;
作用是后端服务器上的程序获取访客真实IP,从该header头获取。部分程序需要该功能。
| |
| http { |
| include mime.types; |
| default_type application/octet-stream; |
| sendfile on; |
| |
| |
| upstream servers { |
| |
| server 192.168.1.253:80 weight=5 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 weight=1 max_fails=3 fail_timeout=30; |
| server 192.168.1.251:80 backup; |
| } |
| |
| server { |
| |
| listen 80; |
| |
| server_name www.test.com; |
| |
| location / { |
| proxy_pass http://servers; |
| proxy_set_header Host $host; |
| } |
| } |
| } |
负载均衡
当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃。为了避免服务器崩溃,让用户有更好的体验,就需要通过负载均衡的方式来分担服务器压力。
假设建立很多的服务器,组成一个服务器集群,当用户访问网站时,先访问一个中间服务器,在让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入该服务器。如此以来,用户的每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。
Nginx负载均衡的几种模式
- 轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除,就不在分配;
| upstream servers { |
| server 192.168.1.253:80 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 max_fails=3 fail_timeout=30; |
| } |
- weight权重轮询:根据后端服务器性能不同配置轮询的权重比,权重越高访问的比重越高,如下例,访问分配率分别是20%,80%;
| upstream servers { |
| server 192.168.1.253:80 weight=2 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 weight=8 max_fails=3 fail_timeout=30; |
| } |
| #假如有十个请求,八个会指向第二台服务器,两个指向第一台; |
- ip_hash:根据请求的ip地址hash结果进行分配,第一次分配到A服务器,后面再请求默认还是分配到A服务器。可以解决Session失效重新登录问题;
| upstream servers { |
| ip_hash; |
| server 192.168.1.253:80 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 max_fails=3 fail_timeout=30; |
| } |
| # 如果客户已经访问了某个服务器,当用户再次访问时,会将该请求通过哈希算法,自动定位到该服务器。 |
- fair:按后端服务器的响应时间来分配请求,响应时间短的优先分配;
| upstream servers { |
| fair; |
| server 192.168.1.253:80 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 max_fails=3 fail_timeout=30; |
| } |
- Url_hash:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效;
| upstream servers { |
| hash $request_uri; |
| hash_method crc32; |
| server 192.168.1.253:80 max_fails=3 fail_timeout=30; |
| server 192.168.1.252:80 max_fails=3 fail_timeout=30; |
| } |
每个设备的状态设置为:
- down 表示这个server不参与负载均衡分配。
- weight 默认为1.weight越大,负载的权重就越大。
- max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误。
- fail_timeout:max_fails次失败后,暂停的时间。
- backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。