K
K
Kirill Petrov2014-08-25 05:49:39
Nginx
Kirill Petrov, 2014-08-25 05:49:39

How to proxy Nginx traffic depending on a request from a DNS server?

How to pull out a domain from a request and insert it into a proxy.Here is the Nginx proxy config

server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:80;
}
}

The DNS server sends the user to my server with Nginx, but I can’t figure out how to make Nginx send it to the right place depending on the site that the user entered.
It is necessary that, when requested on google.com, the config becomes
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://google.com:80;
}
}

When requesting yahoo.com
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://yahoo.com:80;
}
}

Something like determining where a person needs and substituting this value in the nginx proxy. Thanks

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Lynn "Coffee Man", 2014-08-25
@dandyvssega

location / {
    proxy_pass http://$http_host$request_uri;
    proxy_set_header Host $http_host;
    resolver 77.88.8.8;
}

As resolverto specify any DNS server, but not the one that sends everyone to your nginx, otherwise you will get recursion.

S
Sergei Borisov, 2014-08-25
@risik

If I correctly understood what you need, then you need
server_name localhost;
replace localhost with the name of your server.

M
Maxim Vasiliev, 2014-08-25
@qmax

Maybe you need $hostname ?

K
Kirill Petrov, 2014-08-25
@dandyvssega

If I do so

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://$hostname:80;

}
}

I get 502 Bad Gateway

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question