nginx变量与实列

nginx内置变量

内置变量存放在  ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的。总而言之,这些变量表明着客户端请求头的内容,例如$http_user_agent, $http_cookie, 等等。下面是nginx支持的全部内置变量:php

 

$arg_name
请求中的的参数名,即“?”后面的arg_name=arg_value形式的arg_namehtml

$args
请求中的参数值linux

$binary_remote_addr
客户端地址的二进制形式, 固定长度为4个字节nginx

$body_bytes_sent
传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的“%B”参数保持兼容web

$bytes_sent
传输给客户端的字节数 (1.3.8, 1.2.5)正则表达式

$connection
TCP链接的序列号 (1.3.8, 1.2.5)apache

$connection_requests
TCP链接当前的请求数量 (1.3.8, 1.2.5)后端

$content_length
“Content-Length” 请求头字段浏览器

$content_type
“Content-Type” 请求头字段安全

$cookie_name
cookie名称

$document_root
当前请求的文档根目录或别名

$document_uri
同 $uri

$host
优先级以下:HTTP请求行的主机名>”HOST”请求头字段>符合请求的服务器名

$hostname
主机名

$http_name
匹配任意请求头字段; 变量名中的后半部分“name”能够替换成任意请求头字段,如在配置文件中须要获取http请求头:“Accept-Language”,那么将“-”替换为下划线,大写字母替换为小写,形如:$http_accept_language便可。

$https
若是开启了SSL安全模式,值为“on”,不然为空字符串。

$is_args
若是请求中有参数,值为“?”,不然为空字符串。

$limit_rate
用于设置响应的速度限制,详见 limit_rate。

$msec
当前的Unix时间戳 (1.3.9, 1.2.6)

$nginx_version
nginx版本

$pid
工做进程的PID

$pipe
若是请求来自管道通讯,值为“p”,不然为“.” (1.3.12, 1.2.7)

$proxy_protocol_addr
获取代理访问服务器的客户端地址,若是是直接访问,该值为空字符串。(1.5.12)

$query_string
同 $args

$realpath_root
当前请求的文档根目录或别名的真实路径,会将全部符号链接转换为真实路径。

$remote_addr
客户端地址

$remote_port
客户端端口

$remote_user
用于HTTP基础认证服务的用户名

$request
表明客户端的请求地址

$request_body
客户端的请求主体
此变量可在location中使用,将请求主体经过proxy_pass, fastcgi_pass, uwsgi_pass, 和 scgi_pass传递给下一级的代理服务器。

$request_body_file
将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。若是须要之一开启此功能,须要设置client_body_in_file_only。若是将次文件传递给后端的代理服务器,须要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off 。

$request_completion
若是请求成功,值为”OK”,若是请求未完成或者请求不是一个范围请求的最后一部分,则为空。

$request_filename
当前链接请求的文件路径,由root或alias指令与URI请求生成。

$request_length
请求的长度 (包括请求的地址, http请求头和请求主体) (1.3.12, 1.2.7)

$request_method
HTTP请求方法,一般为“GET”或“POST”

$request_time
处理客户端请求使用的时间 (1.3.9, 1.2.6); 从读取客户端的第一个字节开始计时。

$request_uri
这个变量等于包含一些客户端请求参数的原始URI,它没法修改,请查看$uri更改或重写URI,不包含主机名,例如:”/cnphp/test.php?arg=freemouse”。

$scheme
请求使用的Web协议, “http” 或 “https”

$sent_http_name
能够设置任意http响应头字段; 变量名中的后半部分“name”能够替换成任意响应头字段,如须要设置响应头Content-length,那么将“-”替换为下划线,大写字母替换为小写,形如:$sent_http_content_length 4096便可。

$server_addr
服务器端地址,须要注意的是:为了不访问linux系统内核,应将ip地址提早设置在配置文件中。

$server_name
服务器名,www.cnphp.info

$server_port
服务器端口

$server_protocol
服务器的HTTP版本, 一般为 “HTTP/1.0” 或 “HTTP/1.1”

$status
HTTP响应代码 (1.3.2, 1.2.2)

$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
客户端TCP链接的具体信息

$time_iso8601
服务器时间的ISO 8610格式 (1.3.12, 1.2.7)

$time_local
服务器时间(LOG Format 格式) (1.3.12, 1.2.7)

$uri
请求中的当前URI(不带请求参数,参数位于$args),能够不一样于浏览器传递的$request_uri的值,它能够经过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如”/foo/bar.html”。

 

1,将www.myweb.com/connect 跳转到connect.myweb.com

rewrite ^/connect$ http://connect.myweb.com permanent;

rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;

 

2,将connect.myweb.com 301跳转到www.myweb.com/connect/ 

if ($host = "connect.myweb.com"){

rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;

    }

 

3,myweb.com 跳转到www.myweb.com

if ($host != 'www.myweb.com' ) { 

rewrite ^/(.*)$ http://www.myweb.com/$1 permanent; 

    }

 

4,www.myweb.com/category/123.html 跳转为 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

 

5,www.myweb.com/admin/ 下跳转为www.myweb.com/admin/index.php?s=

if (!-e $request_filename){

rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;

    }

 

6,在后面添加/index.php?s=

if (!-e $request_filename){

    rewrite ^/(.*)$ /index.php?s=/$1 last;

    }

 

7,www.myweb.com/xinwen/123.html  等xinwen下面数字+html的连接跳转为404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

 

8,http://www.myweb.com/news/radaier.html 301跳转 http://www.myweb.com/strategy/

rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;

 

9,重定向 连接为404页面

rewrite http://www.myweb.com/123/456.php /404.html last;

 

10, 禁止htaccess

location ~//.ht {

         deny all;

     }

 

11, 能够禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data {

     deny all;

     }

 

12, 禁止单个文件

location ~ /www/log/123.log {

      deny all;

     }

 

13, http://www.myweb.com/news/activies/2014-08-26/123.html 跳转为 http://www.myweb.com/news/activies/123.html

 

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;

 

14,nginx多条件重定向rewrite

若是须要打开带有play的连接就跳转到play,不过/admin/play这个不能跳转

        if ($request_filename ~ (.*)/play){ set $payvar '1';}
        if ($request_filename ~ (.*)/admin){ set $payvar '0';}
        if ($payvar ~ '1'){
                rewrite ^/ http://play.myweb.com/ break;
        }

 

15,http://www.myweb.com/?gid=6 跳转为http://www.myweb.com/123.html

 if ($request_uri ~ "/\?gid\=6"){return  http://www.myweb.com/123.html;}

 

正则表达式匹配,其中:

* ~ 为区分大小写匹配

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

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

文件及目录匹配,其中:

* -f和!-f用来判断是否存在文件

* -d和!-d用来判断是否存在目录

* -e和!-e用来判断是否存在文件或目录

* -x和!-x用来判断文件是否可执行

flag标记有:

* last 至关于Apache里的[L]标记,表示完成rewrite

* break 终止匹配, 再也不匹配后面的规则

* redirect 返回302临时重定向 地址栏会显示跳转后的地址

* permanent 返回301永久重定向 地址栏会显示跳转后的地址

相关文章
相关标签/搜索