Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>