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

I have this line in my config file

server_name site.com m.site.com

My code checks if the url is m.site.com and serves the mobile version. However when I do this going to m.site.com/abc will show my code site.com/abc which is wrong. If I copy/paste the entire block and change site.com to m.site.com it works fine. But I'd rather not have duplicate config (or code).

Is there a way around this?

share|improve this question
    
How is your code getting the URL? –  polynomial Oct 3 '11 at 22:38
    
@polynomial: via HttpRequest.Uri but rawurl shows the changed host as well –  acidzombie24 Oct 4 '11 at 1:46

3 Answers 3

It is an ineffective way even if you check $http_host in config. So, the best way is put all common bits in a file and include it in separate server block:

server {
    listen       80;
    server_name  site.com;
    root         /var/www/localhost/htdocs/site.com;
    include      asp.conf
}

server {
    listen       80;
    server_name  m.site.com;
    root         /var/www/localhost/htdocs/m.site.com;
    include      asp.conf
    ...
}
share|improve this answer

You have two choices here:

1) Have "duplicate" config blocks for nginx

2) Have your site detect the server name (in PHP, $_SERVER['SERVER_NAME'] or $_SERVER['HTTP_HOST']) and serve up a different site depending on what it is.

share|improve this answer
    
I'm not entirely sure the 2nd would work but i am using asp.net anyways. With asp.net i grabbed the url from both HttpRequest.Uri and .rawurl. They both have the changed domain. –  acidzombie24 Oct 4 '11 at 1:47

If you're using nginx as a reverse proxy, try adding the proxy_set_header directive near your proxy_pass directive, like this:

proxy_pass   http://my.asp.net.hostname:8000;
proxy_set_header Host $http_host;

This ensures that nginx is passing the Host header verbatim, instead of taking the main server_name.

share|improve this answer
    
@michael thanks for the edit. –  Wil Tan Oct 13 '12 at 19:54

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.