Welcome! Log In Create A New Profile

Advanced

Negated Regular Expressions in location

Posted by johann 
Negated Regular Expressions in location
September 22, 2012 03:29PM
Not sure I got this right, but could it be there is no way to negate a regular expression in a location block?

For example, with lighttpd, this setup proxies everything except some URLs:

$HTTP["host"] == "foo.com" {
$HTTP["url"] !~ "^/(favicon\.ico$|resources/|robots\.txt)" {
proxy.server = ( "" =>
(
( "host" => "127.0.0.1",
"port" => 8080
)
)
)
}
}

For nginx, it's much more complicated:

location /favicon.ico {
// need to include something here.
}

location /resources/ {
// need to include something here.
}

location /robots.txt {
// need to include something here.
}

location / {
proxy_pass http://tomcat3;
}

This would be really nice to have.
Re: Negated Regular Expressions in location
May 08, 2013 07:33AM
Use negative regex assertion:

location ~ ^/(?!(favicon\.ico|resources|robots\.txt)) {
.... # your stuff
}

Andrejs
loco (at) andrews.lv
Re: Negated Regular Expressions in location
May 08, 2013 01:55PM
Wow, that's a great one. That'll simplify a lot of configurations. Thanks so much!
Re: Negated Regular Expressions in location
May 08, 2013 02:29PM
You are welcome. :)



Edited 1 time(s). Last edit at 05/08/2013 02:30PM by locojohn.
Re: Negated Regular Expressions in location
May 08, 2013 02:32PM
Johann, you are very welcome and I am happy to open you the world of perl regex assertions, but in spite of your original question this should not be a problem because of the order locations are defined and processed. See http://nginx.org/en/docs/http/request_processing.html for the rules about location/request processing order.

So if you define:

location = /favicon.ico {
// need to include something here.
}

location /resources/ {
// need to include something here.
}

location = /robots.txt {
// need to include something here.
}

location ~ ^/ {
proxy_pass http://tomcat3;
}

Then "location ~ ^/" will only capture requests that do not match those specific locations defined above "location /" (with no regex).

Andrejs
loco (at) andrews.lv



Edited 1 time(s). Last edit at 05/08/2013 02:33PM by locojohn.
Re: Negated Regular Expressions in location
May 08, 2013 02:42PM
My config looks like this:

server {

location /favicon.ico {
include caching.conf;
}

location /resources/ {
include caching.conf;
}

location /robots.txt {
include caching.conf;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://tomcat1;
proxy_pass_header server;
gzip_vary off;
}

}

In lighttpd, you could write something like

$HTTP["url"] !~ /resources|favicon.ico|robots.txt/ {
// do the proxy thing
}
Re: Negated Regular Expressions in location
May 08, 2013 02:49PM
Here's the nginx version:


server {

location = /favicon.ico {
include caching.conf;
}

location /resources/ {
include caching.conf;
}

location = robots.txt {
include caching.conf;
}

location ~ ^/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://tomcat1;
proxy_pass_header server;
gzip_vary off;
}

}



Note that "location ~ ^/" will be served last because it is location specified by regex ("~" symbol), so /favicon.ico, /robots.txt and /resources/ will be handled by other locations

Andrejs
loco (at) andrews.lv
Re: Negated Regular Expressions in location
May 08, 2013 02:56PM
Still, I hope I can turn that into a version that only needs the caching.conf once?
Re: Negated Regular Expressions in location
May 08, 2013 03:00PM
location ~ ^/(favicon\.ico$|resources/|robots\.txt$) {
include caching.conf;
}

location ~ ^/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://tomcat1;
proxy_pass_header server;
gzip_vary off;
}
Re: Negated Regular Expressions in location
May 14, 2013 08:11AM
location ~ ^/(favicon\.ico$|resources/|robots\.txt)$ {
include caching.conf;
}

location ~ ^/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://tomcat1;
proxy_pass_header server;
gzip_vary off;
}
##
# Removed $ from parentheses
##
Re: Negated Regular Expressions in location
May 14, 2013 09:06AM
VoodoKobra, with your version location "/resources/images" will not be captured.

Andrejs
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 138
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready