使用Nginx阻止使用regex定位的某些请求不起作用

时间:2018-10-14 22:07:14

标签: regex nginx

我们使用已迁移到另一个系统的Tomcat应用程序。还附带了MSSQL报表服务器。该应用程序最终需要是只读的。目前,我们仍然需要为某些客户输入一些数据。我使用nginx,以便Tomcat服务和Report服务都只需要一台服务器。现在,我想使用nginx定位方法来阻止某些请求。

借助regex101.com,我创建了一个可在nginx中使用的正则表达式。我已经尝试过更改位置块的顺序。但是,我无法使其正常工作。这是正则表达式的链接,您可以在其中看到只有被阻止的URL带有颜色:https://regex101.com/r/w3oy0M/1/

代码如下:

server {
listen 443 ssl;
server_name subdomain.domainname.com;

ssl_certificate         c:\cert\public.crt;
ssl_certificate_key     c:\cert\private.rsa;

location / {
    proxy_pass https://subdomain.domainname.com:8443;
}

location ~ "https:\/\/subdomain\.domainname\.com:8443\/(?!.*customer\/510|.*customer\/10638)(.*\/edit|.*\/new|.*\/archive)" {
    # TEST URL FOR NOT ALLOWED REQUESTS
    return 301 https://www.google.com;
}       

location /Reports {
    return 301 https://subdomain.domainname.com:7443/Reports;
}       

}

1 个答案:

答案 0 :(得分:0)

尝试

location ~ /.+?(?=customer\/)(?!customer\/510|customer\/10638)(.*\/edit|.*\/new|.*\/archive) {
      # TEST URL FOR NOT ALLOWED REQUESTS
      return 301 https://www.google.com;
}

不过,请注意#号之后的所有内容都会被删除,因为它是一个锚定元素,因此请求类似

https://subdomain.domainname.com/#/customer/521/installations/new将作为GET /发送到nginx并被路由到您的第一个位置块

但是,像https://subdomain.domainname.com/hello/customer/521/installations/new这样的请求将被路由到https://google.com