Nginx中的变量
变量可以分为内置变量和自定义变量
内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值
root@nginx \~\]# cd /usr/local/nginx/ \[root@nginx nginx\]# cd conf.d/ \[root@nginx conf.d\]# ls status.conf vhost.conf \[root@nginx conf.d\]# vim vars.conf server { listen 80; server_name var.hh.org; root /data/web/html; index index.html; location /var { default_type text/html; echo "why not let me go oh"; } } \[root@nginx conf.d\]# vim /etc/hosts 在Linux中做解析 172.25.254.100 nginx.hui.org www.huihui.org hx.hx.org var.hh.org 测试: \[root@nginx conf.d\]# curl var.hh.org/var why not let me go oh > #nginx的内置变量 > > server { > > listen 80; > > server_name var.timinglee.org; > > root /data/web/html; > > index index.html; > > location /var { > > default_type text/html; > > echo $remote_addr; > > echo $args; > > echo $is_args; > > echo $document_root; > > echo $document_uri; > > echo $host; > > echo $remote_port; > > echo $remote_user; > > echo $request_method; > > echo $request_filename; > > echo $request_uri; > > echo $scheme; > > echo $server_protocol; > > echo $server_addr; > > echo $server_name; > > echo $server_port; > > echo $http_user_agent; > > echo $http_cookie; > > echo $cookie_key2; > > } > > } #nginx自定义变量 server { listen 80; server_name var.timinglee.org; root /data/web/html; index index.html; location /var { default_type text/html; set $hh hui; echo $hh; } } 返回值 \[root@nginx conf.d\]# curl -b "key1=x,key2=y1" -u lee:lee var.hh.org/var?name=hui\&\&id=6666 why not let me go oh 172.25.254.100 name=hui ? /data/web/html /var var.hh.org 34140 lee GET /data/web/html/var /var?name=hui http HTTP/1.1 172.25.254.100 var.hh.org 80 curl/7.76.1 key1=x,key2=y1 ### Nginx Rewrite模块功能 **if** **指令** 注意: #如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。 #nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false eg:if判定 \[root@nginx conf.d\]# vim vars.conf location /test2 { if ( !-e $request_filename ){ echo "$request_filename is not exist"; return 409; } } \[root@nginx conf.d\]# nginx -s reload > \[root@nginx conf.d\]# curl var.hh.org/test2 > > \ > > \
\409 Conflict\
\\
301 Moved Permanently

why not let me go oh
你没事吧你没事吧
测试:   \[root@nginx conf.d\]# vim jiam.conf server { listen 80; listen 443 ssl; server_name www.hhhoo.org; root /data/web/html; index index.html; ssl_certificate /usr/local/nginx/certs/hhhoo.org.crt; ssl_certificate_key /usr/local/nginx/certs/hhhoo.org.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; location / { valid_referers none blocked server_names *.hhhoo.org ~/.baidu/.; if ( $invalid_referer ){ return 404; } } } 测试:   但是直接访问