lchl0388 发表于 2018-11-10 08:00:09

nginx技术(4)nginx地址重写

server{  
listen   80;
  
server_namewww.codesky.net;
  
location/{
  
proxy_pass   http://192.168.1.11:8080;
  
proxy_redirectoff;
  
proxy_set_headerHost $host;
  
proxy_set_header X-Real-IP $remote_addr;
  
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
rewrite"/zixun/(+)(/*).html$"/zixun/$1/last;
  
rewrite "/loanProduct/(+).html$"/loanProduct/show?id=$1last;
  
}
  
}
  解释:
  rewrite"/zixun/(+)(/*).html$" /zixun/$1/last;
  "/zixun/(+)(/*).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是访问不到
  正则表达式形式的模式匹配,如~*和~
  ~iscase-sensitivematch;
  ‘~’表示大小写敏感的匹配
  ~*specifiesacase-insensitivematch(firefoxmatchesFireFox)
  ‘~*’表示大小写不敏感的匹配(例如:“firefox”字符串可以成功匹配“FireFox”)
  !~and!~*meantheopposite,"doesn'tmatch"
  !~和!~*代表跟后面的正则匹配规则相反的规则,表示不能匹配当前正则表达式规则的字符串执行后面的处理语句
  checkingfortheexistenceofafileusingthe -f and! f operators;使用-f参数以及!-f参数检测一个文件是否存在
  checkingexistenceofadirectoryusingthe -d and! -d operators;使用-d参数以及!-d参数检测一个目录(路径)是否存在
  checkingexistenceofafile,directoryorsymboliclinkusingthe -e and! -e operators;使用-e以及!-e检测是否存在一个文件,一个目录或者一个符号链接。
  checkingwhetherafileisexecutableusingthe -x and! -x operators.使用-x以及!-x检测一个文件是否可执行
  Partsoftheregularexpressionscanbeinparentheses,whosevaluecanthenlaterbeaccessedinthe $1 to $9 variables.
  正则表达式部分可以嵌套,表达式后面的部分如果用到前面的表达式可以用$1到$9变量表示。


页: [1]
查看完整版本: nginx技术(4)nginx地址重写