nginx url 重写 实现伪静态

 server {
        listen       80;
        server_name  www.xxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://192.168.1.11:8080;
	   proxy_redirect off;  
	   proxy_set_header Host $host;  
           proxy_set_header X-Real-IP $remote_addr;  
           proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for; 
	   rewrite "/zixun/([0-9]+)(/*).html$" /zixun/$1/ last;
	   rewrite "/loanProduct/([0-9]+).html$" /loanProduct/show?id=$1 last;
	  
        }
}

解释:

  rewrite "/zixun/([0-9]+)(/*).html$" /zixun/$1/ last;

"/zixun/([0-9]+)(/*).html$"为正则表达式匹配你输入的url地址,表示/zixun/任意数字,至少出现一次,/出现0次或者多次,已.html结尾

/zixun/$1/ last; 符合以上规则的url 转发到 /zixun/$1/到这个链接上,这个就是你实现要获得数据的链接了 ,last为后面的不进行匹配了

如:http://www.xx.con/zixun/56.html 会把这个请求转发到 www.xx.con/zixun/56的servlet上获得数据


Rewrite的Flags

last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301

规则:

一般在非根的location中配置rewrite,都是用的break;而根的location使用last比较好,因为如果配置了fastcgi或代理访问jsp文件的话,在根location下用break是访问不到


正则表达式形式的模式匹配,如~*和~

  • ~ is case-sensitive match;
  • ‘~’表示大小写敏感的匹配
  • ~* specifies a case-insensitive match (firefox matches FireFox)
  • ‘~*’表示大小写不敏感的匹配(例如:“firefox”字符串可以成功匹配“FireFox”)
  • !~ and !~* mean the opposite, "doesn't match"
  •  !~和!~*代表跟后面的正则匹配规则相反的规则,表示不能匹配当前正则表达式规则的字符串执行后面的处理语句
  • checking for the existence of a file using the -f and !-f operators;使用-f参数以及!-f参数检测一个文件是否存在
  • checking existence of a directory using the -d and !-d operators;使用-d参数以及!-d参数检测一个目录(路径)是否存在
  • checking existence of a file, directory or symbolic link using the -e and!-e operators;使用-e以及!-e检测是否存在一个文件,一个目录或者一个符号链接。
  • checking whether a file is executable using the -x and !-x operators.使用-x以及!-x检测一个文件是否可执行

Parts of the regular expressions can be in parentheses, whose value can then later be accessed in the$1 to $9 variables.

正则表达式部分可以嵌套,表达式后面的部分如果用到前面的表达式可以用 $1 到$9 变量表示。







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飓风zj

感谢打赏,thanks

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值