nginx 配置后缀

nginx设置通用访问后缀

需求A描述:比如说服务器的访问路径中含有(.json,.axp,.php), 我们访问可以在nginx中设置拦截规则,定义正则表达式进行拦截,转发到对应的服务器上去。

服务器接口访问路径:

@GetMapping("/isCollection.json")
public String getString(){
   return "OK";
}

浏览器访问方式:  

http://47.102.42.198:8888/isCollection.json

nginx配置方式:

location ~* \.(json)$ {
   proxy_pass http://localhost:8081;   #......
   proxy_connect_timeout 3000s;
   proxy_send_timeout 3000s;
   proxy_read_timeout 3000s;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $host:$server_port;
   client_max_body_size    100m;
}

需求B描述:比如说访问路径中含有(.json,.axp,.php),而服务器中并不存在后缀,我们访问可以在nginx中配置截取后缀,转发到对应的服务器上去。

服务器接口路径: 

@GetMapping("/risk/isCollection")
public String getString(){
   return "OK";
}

浏览器访问方式: 

http://47.102.42.198:8888/isCollection.json

nginx配置方式:

location / {
   rewrite '^(.*)\.json$' /risk/$1 last;
}

location /risk/ {
   proxy_pass http://localhost:8081;   #服务器地址|域名
   proxy_connect_timeout 3000s;
   proxy_send_timeout 3000s;
   proxy_read_timeout 3000s;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $host:$server_port;
   client_max_body_size 100m;
}

Location 的匹配规则详解 

句法:location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
默认:-
内容:server, location

=:开头表示精确匹配

^~:开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~:开头表示区分大小写的正则匹配

~*:开头表示不区分大小写的正则匹配

!~!~*:分别为区分大小写不匹配及不区分大小写不匹配 的正则

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

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

例子,有如下匹配规则:

location = /  {
    [ configuration A ]
}

location = /login {
    [ configuration B ]
}

location ^~ /static/ {
    [ configuration C ]
}

location ~ \.(gif|jpg|png|js|css)$ {
    [ configuration D ]
}

location ~* \.png$ {
    [ configuration E ]
}

location !~ \.xhtml$ {
    [ configuration F ]
}

location !~* \.xhtml$ {
    [ configuration G ]
}

location / {
    [ configuration H ]
}

那么产生的效果如下:

  • 访问根目录/, 比如 http://localhost/  将匹配 [configuration A]
  • 访问 http://localhost/login 将匹配[configuration B],http://localhost/register 则匹配 [configuration H]
  • 访问 http://localhost/static/font.html 将匹配[configuration C]
  • 访问 http://localhost/font.gif, http://localhost/font.jpg 将匹配[configuration D][configuration E],但是[configuration D]顺序优先,[configuration E]起作用, 而 http://localhost/static/c.png 则优先匹配到 [configuration C]
  • 访问 http://localhost/font.PNG 则匹配[configuration E], 而不会匹配[configuration D],因为[configuration E]不区分大小写。
  • 访问 http://localhost/font.xhtml 不会匹配[configuration F][configuration G],http://localhost/font.XHTML不会匹配[configuration G],因为不区分大小写。[configuration F][configuration G]属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
  • 访问 http://localhost/category/id/1234则最终匹配到[configuration H],因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

官网文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#location

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值