2

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.

Waffle
  • 143
  • 1
  • 4
  • You can't redirect without changing the URL -- that's the entire *point* of a redirect. I suggest brushing up on your terminology a bit. – womble Apr 08 '12 at 07:47

1 Answers1

4

Capturing the subdomain and passing it as query argument is fairly easy.

test.server.com --> server.com/resolve.php?sub=test

server {    
    listen 80 default;
    server_name   ~^(?<sub>.+)\.server\.com$;
    rewrite ^ http://www.server.com/resolve.php?sub=$sub
}

Essentially, use a named regex capture to assign the subdomain to a variable, and then pass that variable as a query parameter to the your redirect.

To redirect and keep the same server_name would essentially entail overriding the default assignment of SERVER_NAME $server_name that nginx does (in your fastcgi_params). The difficulty would be in passing your subdomain name between the server blocks.

cyberx86
  • 20,805
  • 1
  • 62
  • 81
  • So for the sake of simplicity and performance, you suggest I take the regex capture route as opposed to trying to redirect while trying to keep the server name? – Waffle Apr 08 '12 at 04:08
  • Yeah, I understand where you are coming from and I can start seeing where I may run in to issues down the road if I try and find some strange way of keeping the URL the same. The first part works absolutely perfect and I just want to thank you very much for your response. Very much appreciated. – Waffle Apr 08 '12 at 04:21
  • @Waffle: Yes, the simplest solution may be the best. In essence, you want to 'fake' a value. PHP's `$_SERVER[]` variables are set by the fastcgi_params. You want `$_SERVER['SERVER_NAME']='test.server.com'` when the nginx block that load the PHP script is actually 'server.com'. I expect that you could use some convoluted method to pass the subdomain from one server block to the other, but you would need a compelling reason to do so. (You might still need to use `server_name_in_redirect off;` to prevent some problems) You're welcome; glad it helped you. – cyberx86 Apr 08 '12 at 04:26