How to find regex in referer url? - regex

To deny some spammers who abuse valid referes to attack certain urls, I'd need a regexp rule to deny all google.* referes to /some/target/url
Here is what I've tried:
location ^~ /some/target/url {
if ($http_referer ~ (.*)google(.*))
{ return 400;
}
}
UPDATE:
What I want to avoid in the rule above are all google.* referers, e.g.
google.com
google.de
google.co.uk
google.jp
...
But, based on access.log, the above directive (and others that I've tried) do not work. So I appreciate your help.

No need for the parenthesis, so you can have:
~ google
to be case-insensitive:
~* google
Then you'll probably want to make it more robust, e.g., to avoid http://www.your.com/google.

Related

Regex to redirect two different URLs starting with the same string

Those are my two nginx rules :
rewrite ^(/v1/foobar)(.*)$ /final$2 permanent;
rewrite ^(/v1/foobar-tow)(.*)$ /final$2 permanent;
I expect that /v1/foobar-two/foo will use the second rule and then will redirect to /final/foo but I doesn't : since the URL starts with foobar, the first rule will be used and finally will redirect to final-two/foo : the concatenation of the first target and the difference between the first and second rule !
To solve the problem I've just inversed the rules, but I wonder if there is a better solution !
To sum up, this is what I'm looking for :
/v1/foobar -> /final
/v1/foobar/hello -> /final/hello
/v1/foobar-tow -> /final
/v1/foobar-tow/hola -> /final/hola
To avoid surprises with NGINX, and increase configuration scalability, you should, generally:
try to live without rewrite directive; as per NGINX author it simply appeared before location and in many cases you can now use location with capture groups in lieu of rewrite
design your regular expressions in the way that the order of matching is not important, if possible
use exact matching, where applicable
isolate regular expression locations under a prefixed one
Putting all things together:
location /v1/ {
location = /v1/foobar {
return 301 /final;
}
location ~ ^/v1/foobar/(\w+)$ {
return 301 /final/$1;
}
location = /v1/foobar-tow {
return 301 /final;
}
location ~ ^/v1/foobar-tow/(\w+)$ {
return 301 /final/$1;
}
}

Nginx regular expression on map

Not an expert on regexs,
working on a nginx project, and we are looking for a way to map the first two (2) directories levels of a uri
map $uri $my_uri {
~^(?<base_uri>.*)/[^/]+$ $base_uri;
default $uri;
}
So far this code return the base_uri without the domain/server or any filenames
But I need this original request :
http://example.com/level1/level2/file.ext
http://example.com/level1/level2/level3/file.ext
http://example.com/level1/level2/level3/level4/[...more levels...]/file.ext
to return only the first 2 levels, regex should return
/level1/level2
Please note:
sub-folders (lavel1, lavel2) will be dynamic folder names, not static.
nginx's $url variable does not include protocol/server/port from the original request. so regex is performed against /level1/level2/level3/[...]/file.ext
any help will be appreciated.

What is the best way to handle Nginx rewrite with subdomains?

I need to parse out a subdomain and add it to end of the url, for example:
subdomain.mysite.com needs to rewrite to subdomain.mysite.com/subdomain
subdomain.mysite.com/login needs to rewrite to subdomain.mysite.com/subdomain/login
Trouble arises because I have a set of reserve words that I don't want to match, for example the various different environments.
dev-web.mywebsite.com should not map to mywebsite.com/dev-web
This is what I have so far... am struggling with the nginx syntax and regex in general.
if ($host ~ ^([^.]+)\.(.+)) {
set $subdomain $1;
}
if ($subdomain ~* ^(dev-web|uat-web)$) {
rewrite ^ $scheme://$host/$subdomain$request_uri permanent;
}
The various errors I get are too many redirects or it is just not redirecting at all.
ERR_TOO_MANY_REDIRECTS
subdomain.mysite.com/subdomain/subdomain/subdomain/subdomain...

Nginx location match regex not working

I am unable to match location with below mentioned pattern, I want to set expires header to 24 hrs. but it is not working. It works if I just use below mentioned regex :
location ~* .*abc\.php.* {
expires 24h;
}
Below example does not work.
location ~* .*abc\.php.*xyz=detail.*login_something=.* {
expires 24h;
}
There is lot of content in between and after of "abc.php" & "xyz=detail" & "login_something=" so I have to use .* only.
Thanks in advance!
There are multiple ways to achieve what you are trying to do, but the simplest method is to apply your mega regex to a variable that contains the entire URI (including the query string). This would be $request_uri
The second problem is how to manipulate expires and again, rather than use multiple blocks and have to reimplement PHP directives in each one, just use the map directive as detailed in the expires documentation.
For example:
map $request_uri $expires {
default off;
~*abc\.php.*xyz=detail.*login_something= 24h;
}
server {
...
expires $expires;
...
}

How can I get a param using regex in a nginx server block?

I'm trying to get a param(h=1500 for example) via regex in a server block in an nginx server but it's not working. My last try was this:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)?h=(?<height>\d+)$" { ...... }
Here you can check and it works: https://regex101.com/r/kP9eY9/1
But in my server block file it does't.
If I try something like this, it works:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)/(?<height>\d+)$" { ...... }
Instead a param like "h=300", I just use a "/300" and I can get the value in my server block file.
I'm not a expert using regex so I can't see if there is something wrong. I need your help guys! Thank you!
From the documentation:
locations of all types test only a URI part of request line without
arguments
which means the ? and anything that follows it.
As #Richard mentioned, you can't use request arguments in locations regexps.
If you need to work with request arguments in your nginx config you might use $arg_ and/or $args syntax:
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
I.e
location / {
if ($arg_param = 'someval') {
# some code here
}
}