nginx的全局变量

变量

说明

$args

请求中的参数,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2

$content_length

HTTP请求信息里的"Content-Length"

$conten_type

HTTP请求信息里的"Content-Type"

$document_root

nginx虚拟主机配置文件中的root参数对应的值

$document_uri

当前请求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含后面的参数

$host

主机头,也就是域名

$http_user_agent

客户端的详细信息,也就是浏览器的标识,用curl -A可以指定

$http_cookie

客户端的cookie信息

$limit_rate

如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0

$remote_addr

客户端的公网ip

$remote_port

客户端的port

$remote_user

如果nginx有配置认证,该变量代表客户端认证的用户名

$request_body_file

做反向代理时发给后端服务器的本地资源的名称

$request_method

请求资源的方式,GET/PUT/DELETE等

$request_filename

当前请求的资源文件的路径名称,相当于是$document_root/$document_uri的组合

$request_uri

请求的链接,包括$document_uri和$args

$scheme

请求的协议,如ftp,http,https

$server_protocol

客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等

$server_addr

服务器IP地址

$server_name

服务器的主机名

$server_port

服务器的端口号

$uri

和$document_uri相同

$http_referer

客户端请求时的referer,通俗讲就是该请求是通过哪个链接跳过来的,用curl -e可以指定

rewrite实战

域名跳转(域名重定向)

示例1(不带条件的):
server{
listen 80;
server_name www.aminglinux.com;
rewrite /(.*) http://www.aming.com/$1 permanent;
....... } 示例2(带条件的):
server{
listen 80;
server_name www.aminglinux.com aminglinux.com;
if ($host != 'www.aminglinux.com')
{
rewrite /(.*) http://www.aminglinux.com/$1 permanent;
}
....... }
示例3(http跳转到https):
server{
listen 80;
server_name www.aminglinux.com;
rewrite /(.*) https://www.aminglinux.com/$1 permanent;
....... }
示例4(域名访问二级目录)
server{
listen 80;
server_name bbs.aminglinux.com;
rewrite /(.*) http://www.aminglinux.com/bbs/$1 last;
....... }
示例5(静态请求分离)
server{
listen 80;
server_name www.aminglinux.com;
location ~* ^.+.(jpg|jpeg|gif|css|png|js)$
{
rewrite /(.*) http://img.aminglinux.com/$1 permanent;
} ....... }
或者:
server{
listen 80;
server_name www.aminglinux.com;
if ( $uri ~* 'jpg|jpeg|gif|css|png|js$')
{
rewrite /(.*) http://img.aminglinux.com/$1 permanent;
} ....... }

防盗链

示例6
server{
listen 80;
server_name www.aminglinux.com;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.aminglinux.com aminglinux.com *.aming.com aming.com;
if ($invalid_referer)
{
rewrite /(.*) http://img.aminglinux.com/images/forbidden.png;
}
} ....... }
说明:*这里是通配,跟正则里面的*不是一个意思,none指的是referer不存在的情况(curl -e 测试),
blocked指的是referer头部的值被防火墙或者代理服务器删除或者伪装的情况,
该情况下,referer头部的值不以http://或者https://开头(curl -e 后面跟的referer不以http://或者https://开头)。
或者:
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.aminglinux.com *.aming.com aminglinux.com aming.com;
if ($invalid_referer)
{
return 403;
}
}

伪静态

示例7(discuz伪静态):
location / {
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
}

rewrite多个条件的并且

示例8: location /{

       set $rule 0;

       if ($document_uri !~ '^/abc') {

         set $rule "${rule}1";

       }

       if ($http_user_agent ~* 'ie6|firefox') {

         set $rule "${rule}2";

       }

       if ($rule = "012") {

         rewrite /(.*) /abc/$1 redirect;

       }

     }

nginx的localtion配置

安装第三方模块echo-nginx-module

nginx location语法规则:location [=|~|~*|^~] /uri/ { … }
nginx的location匹配的变量是$uri

符号

说明

=

表示精确匹配

^~

表示uri以指定字符或字符串开头

~

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

~*

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

/

通用匹配,任何请求都会匹配到

规则优先级

=  高于  ^~  高于  ~* 等于 ~  高于  /

规则示例

location = "/12.jpg" { ... }
如:
www.aminglinux.com/12.jpg 匹配
www.aminglinux.com/abc/12.jpg 不匹配 location ^~ "/abc/" { ... }
如:
www.aminglinux.com/abc/123.html 匹配
www.aminglinux.com/a/abc/123.jpg 不匹配 location ~ "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.png 匹配
www.aminglinux.com/aaa/png/123.html 匹配 location ~* "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配
www.aminglinux.com/aaa/png/123.html 匹配 location /admin/ { ... }
如:
www.aminglinux.com/admin/aaa/1.php 匹配
www.aminglinux.com/123/admin/1.php 不匹配

小常识

有些资料上介绍location支持不匹配 !~,

如: location !~ 'png'{ ... }

这是错误的,location不支持 !~

如果有这样的需求,可以通过if来实现,

如: if ($uri !~ 'png') { ... }

注意:location优先级小于if

nginx第五天的更多相关文章

  1. zabbix 与 nginx (五)

    zabbix监控nginx的大概流程为:   1:被监控端的nginx开启stub_status模块 2:通过脚本的方式获取nginx的状态值 3:修改被监控端的配置文件,Userparameter= ...

  2. LINUX测试环境部署nginx(五)

    安装配置nginx 安装编译环境:yum -y install pcre-devel openssl openssl-devel 拷贝nginx压缩文件到目标目录后,解压tar -zxvf nginx ...

  3. nginx(五)nginx与php的安装配置

    经过前面学习,对nginx有个大概的了解,来配置LNMP;只要是在系统安装过程中选择安装比较齐全的包,基本上系统都能满足安装要求,下面是我一个一个测试的,基本上全部安装所需的库文件,放心安装: [ro ...

  4. nginx的五种负载算法模式

    nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight  指定轮询几率,weight和访问比率成正比 ...

  5. 我眼中的 Nginx(五):Nginx — 子请求设计之道

    张超:又拍云系统开发高级工程师,负责又拍云 CDN 平台相关组件的更新及维护.Github ID: tokers,活跃于 OpenResty 社区和 Nginx 邮件列表等开源社区,专注于服务端技术的 ...

  6. nginx实践(五)之代理服务(正向代理与反向代理介绍)

    正向代理 正向代理代理是为客户端服务,代理负责DNS解析域名到对应ip,并进行访问服务端,返回响应给客户端 反向代理 客户端自己负责请求DNS解析域名到对应ip,服务端通过代理分发流量,进行负载均衡 ...

  7. nginx介绍(五) - 高可用

    前言 即便想 nginx 这么牛B的服务器, 也不能保证他不挂啊, 就算不是机器出故障, 比如, 停电了, 那么机器挂了, 很正常啊. 这时候, 没有分发服务器, 网站就不能正常访问了, 咋搞? 网站 ...

  8. Nginx(五)-- 配置文件之Rewrite

    Rewrite支持URL重写 1.常用指令以及语法 1) if指令    if语法: if 空格 (condition) {}     条件:     1. “=” 来判断相等,用于字符的比较     ...

  9. zabbix日常监控项nginx(五)

    1.开启nginx监控 2.编写脚本来进行数据采集 3.设置用户自定义参数 4.重启zabbix-agent 5.添加item.创建图形.创建触发器 6.创建模板 注:第一次使用可以按需求制定好模板, ...

  10. nginx实战五

    nginx用户认证 https://coding.net/u/aminglinux/p/nginx/git/blob/master/access/auth.md 当访问一些私密资源时,最好配置用户认证 ...

随机推荐

  1. web学习(2019-10)

    @“fuzz一下”:所有注入爆破题/其他题,必fuzz 在安全测试中,模糊测试(fuzz testing)是一种介于完全的手工渗透测试与完全的自动化测试之间的安全性测试类型 模糊测试(fuzz tes ...

  2. mysql常用排查命令

    1.查看服务器使用状态 mysqladmin -uroot -p -S /tmp/mysql.sock -r -i 1 ext |\ awk -F"|" \ "BEGIN ...

  3. 安装VMWare tools 及安装后/mnt中有hgfs但没共享文件的解决办法

    一.首先是安装VMWare tools打开虚拟机软件,在菜单栏‘虚拟机’子菜单下‘安装VMware Tools' 1.以root身份进入Linux 2.此时把linux的/dev/cdrom设备挂载到 ...

  4. ssh配置互信不成功,两台服务器有一台需要输入密码

    在客户现场,某国产平台,配置机器互信时发现,A可以免密访问B,而B不能免密访问A,且B不能免密访问B,进行了下面的几个处理后搞定. 1)确定目录权限:/root/ 755/root/.ssh 700/ ...

  5. Makefile 书写规则

    1.1 Makefile的规则 在讲述这个Makefile之前,还是让我们先来粗略地看一看Makefile的规则. target ... : prerequisites ...   command   ...

  6. PHP空对象 空数组

    PHP定义空对象:$obj = (object)null;或$obj = (object)array(); 定义空数组:$arr = array();或$arr = [];//自 5.4 起可以使用短 ...

  7. Android渐变色xml配置

    这里渐变色: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&quo ...

  8. 给Repater增加等号

    //不改变数据结构的情况下,增加行号.对Application服务器压力增大,减少DB服务器压力.    protected void repShow_ItemDataBound(object sen ...

  9. 111、什么是stack (Swarm18)

    参考https://www.cnblogs.com/CloudMan6/p/8119150.html   什么是 stack ?    在将这个之前先回顾一下前面部署WordPress的过程:     ...

  10. 15 Python之内置函数

    思维导图: https://www.processon.com/mindmap/5c10cb5ee4b0090a2c9db92f 1. 匿名函数统一的名字是:<lambda> 使用场景: ...