Nginx Reverse Proxying Multiple Domains Using map Module

Instead of writing multiple server blocks repeatedly in configuration, we can simplified configuration using nginx map module

ie:

    map_hash_bucket_size 128;

    map $http_host $backend_servers {
        hostnames;
        default							www.example.com;
        frontend.example2.com			backend.example2.com
        frontend.example3.com			backend.example3.com
        www.example.org					backend.example.org
    }

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

	server {
		location / {
			proxy_pass  http://$backend_servers
		}
	}

source: taken from nginx mailinglist



Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *