Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to alias m.example.com so that the requests are forwarded/aliased to example.com/m/ but without the user being redirected to the /m/ directory.

For example, the user accesses m.example.com, m.example.com/1.htm, m.example.com/2.htm but nginx is forwarding/aliasing the requests to example.com/m/, example.com/m/1.htm and example.com/m/2.htm.

I have tried to use the rewrite directive, but this appears to perform a client-side redirect to example.com/m/.

I have also had a look at the alias directive, but this only seems to modify the location of where the files will be served on the server's filesystem.

I'm sure this must have been done before, but I cannot seem to work this out. Am I using an incorrect flag with the alias directive (I've tried all 4: last, break, redirect, permanent). Have I missed the correct directive to use altogether?

Snippet of config using rewrite:

server_name       example.com m.example.com;

...

if ($host ~* m\.(.*))
{
    rewrite ^(.*)$ http://example.com/m$1 break;
}

Another attempt:

server
{
    listen            80;
    server_name       m.example.com;

    location /
    {
        rewrite ^ http://example.com/m$request_uri permanent;
    }
}

Thanks in advance.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

This is not possible in Nginx. You can use the m subdomain but you will have to pass to your backend in that server block. This is because Nginx cannot rewrite the URL without using an actual HTTP redirect, even if you tell it to it will ignore that and force a HTTP redirect.

If you use a fastcgi backend it shouldn't really matter, though, and even if you proxy pass to Apache you should still be able to configure it to not care.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.