Nginx - Redirect any Subdomain to File without Rewriting
        Posted  
        
            by 
                Waffle
            
        on Server Fault
        
        See other posts from Server Fault
        
            or by Waffle
        
        
        
        Published on 2012-04-08T03:42:20Z
        Indexed on 
            2012/04/08
            5:32 UTC
        
        
        Read the original article
        Hit count: 355
        
Recently I have switched from Apache to Nginx to increase performance on a web server running Ubuntu 11.10. I have been having issues trying to figure out how certain things work in Nginx compared to Apache, but one issue has been stumping me and I have not been able to find the answer online. My problem is that I need to be able to redirect (not rewrite) any sub-domain to a file, but that file needs to be able to get the sub-domain part of the URL in order to do a database look-up of that sub-domain. So far, I have been able to get any sub-domain to rewrite to that file, but then it loses the text of the sub-domain I need.
So, for example, I would like test.server.com to redirect to server.com/resolve.php, but still remain as test.server.com. If this is not possible, the thing that I would need at the very least would be something such as going to test.server.com would go to server.com/resolve.php?=test . One of these options must be possible in Nginx.
My config as it stands right now looks something like this:
server {
    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default ipv6only=on; ## listen for ipv6
    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    # Make site accessible from http://localhost/
    server_name www.server.com server.com;
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            try_files $uri $uri/ /index.html;
    }
    location /doc {
            root /usr/share;
            autoindex on;
            allow 127.0.0.1;
    }
    location /images {
            root /usr/share;
            autoindex off;
    }
    #error_page 404 /404.html;
    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #       root /usr/share/nginx/www;
    #}
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #       proxy_pass http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}
    }
    server {
         listen 80 default;
         server_name *.server.com;
         rewrite ^ http://www.server.com/resolve.php;
    }
As I said before, I am very new to Nginx, so I have a feeling the answer is pretty simple, but no examples online seem to deal with just redirects without rewrites or rewriting with the sub-domain section included. Any help on what to do would be most appreciated and if any one has a better idea to accomplish what I need, I am also open to ideas. Thank you very much.
© Server Fault or respective owner