Welcome! Log In Create A New Profile

Advanced

regex to redirect multiple domain only works if domain not declared elsewere

Posted by illwieckz 
regex to redirect multiple domain only works if domain not declared elsewere
April 04, 2014 12:47AM
Hello, I encounter a strange problem while trying to redirect multiple similar domains. I explain.

Imagine this DNS config:

lollipop A 127.0.0.1
chocolate A 127.0.0.1
*.lollipop CNAME lollipop
*.chocolate CNAME chocolate

Imagine this Nginx config:

# www.domain → domain
server {
listen 80;
server_name "~^www\.(?<domain>.*)$";
return 301 $scheme://$domain$request_uri;
}

# proxy_pass
server {
listen 80;
server_name .chocolate;
location / {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:81;
}
}

# fallback
server {
listen 80;
root /var/www/fallback;
index index.html;
location / {
rewrite ^.+$ / permanent;
}
}

What I expect:
-------------------

GET chocolate → proxy_pass
GET www.chocolate → chocolate → proxy_pass

GET milk.chocolate → proxy_pass
GET www.milk.chocolate → milk.chocolate → proxy_pass

GET lollipop → fallback
GET www.lollipop → lollipop → fallback

GET bubblegum.lollipop → fallback
GET www.bubblegum.lollipop → bubblegum.lollipop → fallback

What I get:
--------------

GET chocolate → proxy_pass [ok]
GET www.chocolate → proxy_pass [not ok][does not redirect]

GET milk.chocolate → proxy_pass [ok]
GET www.milk.chocolate → proxy_pass [not ok][does not redirect]

GET lollipop → fallback [ok]
GET www.lollipop → lollipop → fallback [ok][redirects]

GET bubblegum.lollipop → fallback [ok]
GET www.bubblegum.lollipop → bubblegum.lollipop → fallback [ok][redirects]

You see it works for undeclared domain names (handled by the fallback rule after beeing handled by the redirect rule), but not for declared domain names.

However, I write the 'www.domain'→'domain' redirection rule BEFORE the rule handling a specific domain. If I add logging, the redirection log sees nothing in this case, this code is not used.

I have tried that too: (yes I know http://wiki.nginx.org/IfIsEvil )

server {
listen 80;
server_name www.*;
if ($host ~* ^www\.(.+)) {
set $domain $1;
return 301 $scheme://$domain$request_uri;
}
}

But it does the same (i.e. does not work).

I’m using nginx 1.2.1 (Debian).

If Anyone have an idea, do not hesitate to share. ;)

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

Click here to login

Online Users

Guests: 125
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready