Problem with requests that have a . in the end of the domain

hi

i am currently experiencing some strange behavior if a user requests
our domain with a . in the end of the domain. the actual problem is
that our php code can’t really get along with that . in the end of the
domain. so i tried creating an nginx rule that fixes this by doing a
redirect to the correct domain. i tried multiple things, first i just
left our actual production vhost untouched and added an additional one
with the same server_name but with a . in the end of the domain, like
this:

server {
server_name mydomain;

}

server {
server_name mydomain.;

rewrite . http://mydomain$request_uri permanent;
}

but i found out that the requests never reach the vhost mydomain., i
also tried to switch the order of those two, but all requests always
go to the vhost mydomain without .

then another thing i tried was to catch the bad request based on the
$host variable like this:

server {
if ($host ~ “^mydomain.$”)
{
rewrite . http://mydomain$request_uri permanent;
}
}

but now when i check the rewrite log i see that this $host variable
doesn’t contain the . even if the request did. this means i request
http://mydomain. in my browser and in the rewrite log i see:

2009/10/01 12:23:55 [notice] 8238#0: *3 “^mytdomain.$” does not match
“mydomain”, client: 192.168.56.1, server: mydomain, request: “GET /
HTTP/1.1”, host: “mydomain.”

i am sure that the browser does not parse this . out, because the php
can see it. and in the end of the above log line you can also see
host: “mydomain.”.

does anybody maybe have an idea what else i could do to make sure that
http://mydomain. gets redirected to http://mydomain?

thanks a lot for help

mauro

On Fri, Oct 02, 2009 at 11:34:57AM +0800, Mauro Stettler wrote:

rewrite . http://mydomain$request_uri permanent;

i am sure that the browser does not parse this . out, because the php
can see it. and in the end of the above log line you can also see
host: “mydomain.”.

does anybody maybe have an idea what else i could do to make sure that
http://mydomain. gets redirected to http://mydomain?

Yes, server_name ignores trailing dot. The dot is removed from $host
too.
Therefore you should unchanged $http_host:

server {

  • if ($host ~ “^mydomain.$”)
  • if ($http_host = mydomain.)
    {
  • rewrite . http://mydomain$request_uri permanent;
    
  • rewrite . http://mydomain$request_uri? permanent;
    
    }
    }

Also if you use proxy for PHP, then you may edit “Host” header:
proxy_set_header Host $host;

however, you can do the same with FastCGI.

thanks a lot, that works perfectly, even with my old version 0.6.35

mauro

2009/10/2 Igor S. [email protected]: