nginx学习1.4 nginx常用语法之root、alias、proxy_pass、rewrite等

环境:

centos6/7,nginx-1.9.15.

摘要说明:

上一篇主要讲述nginx下server节点中location进行详细的解析;

本章节主要讲述nginx其他常用语法:

步骤:

1.root、alias及index

root

语法:root path 
默认值:root html 
使用字段:http, server, location ,location中的if字段
请求到达后的文件根目录。
下例中:

#相对路径
location /static/a/ {
  root   html;
}
#绝对路径
location /static/b {
   root   /usr/local/nginx/html;
}

如果请求"/static/a/a/test.html"文件,nginx将转到"/usr/local/nginx/html/static/a/a/test.html"文件。你可以在参数中使用变量。
注意:这里面的相对路径是相对于nginx安装目录下的基础路径

alias

语法:alias file-path|directory-path; 
默认值:no
使用字段:location
这个指令指定一个路径使用某个某个,注意它可能类似于root,但是document root没有改变,请求只是使用了别名目录的文件。

#一般匹配
location  /c/ {
   alias  /usr/local/nginx/html/static/c/;
}

上个例子总,请求"/c/a/test.html"将返回这个文件: " /usr/local/nginx/html/static/c/a/test.html"。
Alias同样可以用于带正则表达式的location,如:

#正则匹配
location  ~^/cccc/(.*)$ {
   alias  /usr/local/nginx/html/static/b/b/$1;
}

请求"/cccc/test.html"将返回"/usr/local/nginx/html/static/b/b/test.html"。
同样,也可以在别名目录字段中使用变量。

index:

指定初始首页:

location /static/c {
   root   /usr/local/nginx/html;
   index  a/test.html
}

url 以 /结尾时,nginx会认为打开此目录下的首页,此时就启用index;访问的路径为url+index;与root和alias配置无关

url 不是以 / 结尾,nginx认为它是个文件,尝试打开这个文件,此时index命令不启用

总结:将请求路径分为匹配路径+后续路径;root转的是匹配路径+后续路径;alias转的是后续路径

2.proxy_pass

反向代理(proxy_pass)主要就是进行请求的转发;

反向代理只要注意的点就是proxy_pass针对ip+port之后有没有/后面的地址是否以/结尾

#proxy_pass
#proxy_pass的ip+port之后以非/结尾 访问http://xxxx/test/a/a/test.html
location  /test/ {
  proxy_pass http://127.0.0.1:8888;#此处未关闭,传递整个路径/test/a/a/test.html到目标ip:port
}
#proxy_pass的ip+port以/结尾 访问http://xxxx/bbbb/a/a/test.html
location  /bbbb/ {
  proxy_pass http://127.0.0.1:8888/test/;#此处关闭,只传递/a/a/test.html到目标ip:port
}

总结:proxy_pass的关闭与不关闭。针对ip+port之后有没有“/”;proxy_pass的ip+port之后以“/”结尾只传递后续路径;不以“/”结尾传递完整路径

3.rewrite

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量, 结合正则表达式和标志位实现url重写以及重定向;

语法:rewrite regex replacement [flag];

作用范围:rewrite只能放在server{},location{},if{}中, 并且只能对域名后边的除去传递的参数外的字符串起作用, 例如http://seanlook.com/a/we/index.jsp?id=1&u=str 只对/a/we/index.php重写。

    #基于端口
	server {
        listen       2008;
        server_name  localhost;
		#rewrite
		#break;url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求,地址栏url不变 
		location  =/test {
           rewrite ^/  /a/b/test.html break;
	       root   html/static/;
        }
		#redirect;返回302临时重定向,地址栏显示重定向后的url,爬虫不会更新url(因为是临时) 
		location  =/test1 {
           rewrite ^/  /d/a/test.html redirect;
	       root   html/static/;
        }
		#last;url重写后,马上发起一个新的请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏url不变 
		location  /test2 {
           rewrite ^/test2/test3  /d/b/test.html last;
	       root   html/static/;
        }
		#permanent;返回301永久重定向, 地址栏显示重定向后的url,爬虫更新url
		location  /test3 {
           rewrite ^/  /d/c/test.html permanent;
	       root   html/static/;
        }
        location  /d/ {
           alias   html/static/c/;
        }
        error_page   500 502 503 504  /50x.html;
        location  /50x.html {
            root   html;
        }
    }

总结:rewrite正则匹配上后替换成新的path,根据flag的不同进行不同的处理,其中redirect和permanent进行外部重定向;

beak和last进行内部重定向;其中我们需要注意的是flag可为空,为空则接着往下走直到/break中断/last 中断&& location重匹配

4.总结

结合上章location我们可以总结出nginx的大概执行过程如下

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值