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.

Assuming my server is www.mydomain.com, on Nginx 1.0.6

I'm trying to proxy all request to http://www.mydomain.com/fetch to other hosts, the destination URL is specified as a GET parameter named "url".

For instance, when user requests either one:

http://www.mydomain.com/fetch?url=http://another-server.mydomain.com/foo/bar

http://www.mydomain.com/fetch?url=http://another-server/foo/bar

it should be proxyed to

http://another-server.mydomain.com/foo/bar

I'm using the following nginx config and it works fine only if the url paramter contains domain name, like http://another-server.mydomain.com/...; but fails on http://another-server/... on error:

another-server could not be resolved (3: Host not found)

nginx.conf is:

http {
...
# the DNS server
resolver 171.10.129.16;
server {
    listen       80;
    server_name  localhost;
    root /path/to/site/root;

    location = /fetch {            
        proxy_pass $arg_url;
    }
}

Here, I'd like to resolve all URL without domain name as host name in mydomain.com, in /etc/resolv.conf, it's possible to specify default search domain name for the whole Linux system, but it doesn't affect nginx resolver:

search mydomain.com

Is it possible in Nginx? Or alternatively, how to "rewrite" the url parameter so that I can add the domain name?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

nginx performs its own DNS resolution and doesn't use the libc library, which is why /etc/resolv.conf doesn't have an effect. I can't find any option to specify a search domain, so rewriting the URL is your only option. Something like this should do the trick:

location /fetch {
    # Don't rewrite if we've already rewritten or the request already contains the full domain
    if ($arg_url !~ mydomain.com) {
        rewrite ^/fetch?url=http://([^/]+)(/?.*)$ /fetch?url=http://$1.mydomain.com$2;
    }
    proxy_pass $arg_url;
}
share|improve this answer
    
It looks the only working solution –  myjpa Apr 16 '12 at 2:59

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.