簡體   English   中英

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

[英]Use nginx to block certain requests with regex in location doesn't work

我們使用已遷移到另一個系統的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;
}       

}

嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM