Nginx can't resolve server names when loading them from a file

370 views Asked by At

I'm new to nginx and trying to achieve the following behaviour: I want to pass an header to nginx and pass the request to another server based on the given header. I want to load my map from a file so i'm using the following code


worker_processes  1;

error_log  logs/error.log;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;

   map $http_dest $new_server{
    include port_to_address.map;
    default google.com;
   }


    server {
        listen       200;
        server_name  localhost;


        access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://$new_server;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

with the port_to_address.map in the following format

value_1 yahoo.com;
value_2 neflix.com;

With This configuration nginx starts normally but when i pass him an header that exists in the file he returns the following error

2021/04/18 21:52:49 [error] 17352#20504: *3 no resolver defined to resolve yahoo.com, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:200"

When i'm using the ip address and not the server's name it works perfectly. I also got another nginx thats round-robin requests between nodes it reads from a file (with upstream) and there i doesn't get any exception although im using the server's name and not their ip.

I read about using resolver but it doesn't work and i prefer to avoid it. Is there any other way to make it work without using the resolver or the servers ip? (I dont mind changing the map file structure if needed)

0

There are 0 answers