nginx常用if判断语句与变量set使用方法
[root@www conf]# tail nginx.conf location / { stub_status on; access_log off; } } include vhost/*.conf; //指定虚拟主机配置文件路径 } [root@www conf]# cd /usr/local/nginx/conf/vhost 编辑虚拟主机配置文件 [root@www vhost]#vim dedecms.conf server { listen 80; server_name www.wokao.com; index index.html index.htm index.php; root /data/www/wokao; location ~ .*.(php|php5)?$ { Include test.conf; fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } } [root@www conf]# pwd /usr/local/nginx/conf [root@www conf]# vim test.conf //如果访问request的method值为POST则返回“o” If ($request_method ~* "POST") { set $test o; } if ($remote_addr = '188.188.3.171') //如果访问地址等于188.188.3.171则返回“k” 同理 #if ($remote_addr != '188.188.3.171') //如果访问地址非188.188.3.171则返回“k” { Set $test "${test}k"; } //如果满足上述条件则返回“ok”处理方式为返回403 if ( $test = ok ) { Return 403; #rewrite ^(.*) http://www.fengyuba.com permanent; #如果满足条件则重定向到www.fengyuba.com }
参数注释如下:
正则表达式匹配,其中:
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有:
* last 相当于apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址
nginx常用变量
$args 此变量与请求行中的参数相等
$content_length 等于请求行的“Content_Length”的值。
$content_type 等同与请求头部的”Content_Type”的值
$document_root 等同于当前请求的root指令指定的值
$document_uri 与$uri一样
$host 与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样
$limit_rate 允许限制的连接速率
$uri 等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index
$server_protocol 等同于request的协议,使用“HTTP/1.0”或“HTTP/1.1”
$server_port 请求到达的服务器的端口号
$server_name 请求到达的服务器名
$server_addr request到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。
$request_uri 含有参数的完整的初始URI
$request_method 等同于request的method,通常是“GET”或“POST”
$request_filename 当前请求的文件的路径名,由root或alias和URIrequest组合而成
$request_body_file
$remote_user 等同于用户名,由ngx_http_auth_basic_module认证
$remote_port 客户端port
$remote_addr 客户端ip
$query_string 与$args一样