Using map directive for multiple virtual hosts

Nikolai Lusan nikolai at lusan.id.au
Mon Oct 2 19:28:28 UTC 2017


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi,
   I have a requirement to rewrite incoming URL requests for multiple
virtual hosts, each with a unique (possibly overlapping) set of incoming
requests. We have each vhost in a server block, but since the map directive
lives in the http block I think that the behaviour we are currently seeing
is a result of having multiple map declarations not working - currently the
first virtual host with a configured map is working as expected, subsequent
maps are just returning the default. 

Each of our server directives, and it's associated map, are living in one
file per hostname. These files are included using a globbed include in the
main nginx.conf files http directive.

So the configuration looks something like this (these are abbreviated
examples with all the php processing stuff pulled out of it)

nginx.conf:
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  access_log  /var/log/nginx/access.log;

  log_format  main  '$status $request';
  sendfile    on;

  server_tokens on;
  ...
  include /etc/nginx/our-sites/*;
}

/etc/nginx/our-sites/site1.com:
map $request_uri $new_uri_site1 {
	/store/product1 /store/new_products;
	/contact/us /contact/about;
	/services/consulting /contact/consulting;
}

server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;
	ssl on;

	access_log  /var/log/nginx/site1.com.access-tls.log combined;
	error_log  /var/log/nginx/site1.com.error-tls.log;

	server_name site1.com;
	root /var/www/site1.com;
	index index.php;

	location / {
		try_files $uri $uri/ index.php?part=$uri&$args;
	}      
        ...
}


/etc/nginx/our-sites/site2.com:
map $request_uri $new_uri_site2 {
	/contact/us /contact/about;
	/services /contact/consulting;
	/people /staff/all;
}

server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;
	ssl on;

	access_log  /var/log/nginx/site2.com.access-tls.log combined;
	error_log  /var/log/nginx/site2.com.error-tls.log;

	server_name site2.com;
	root /var/www/site2.com;
	index index.php;

	location / {
		try_files $uri $uri/ index.php?section=$uri;
	}      
        ...
}


Apart from moving the map directives inside each relevant server block as
rewrite rules does anyone know of a working solution for this situation?
- -- 
Nikolai Lusan

Email: nikolai at lusan.id.au
Phone: 0425 661 620
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEVfd4GW6z4nsBxdLo4ZaDRV2VL6QFAlnSk1wACgkQ4ZaDRV2V
L6Sy0w//fkjJGg5BNHT92bsgNgZQFYO1wHsx5AoWwFUm4vdYeKCjlaHppID/u5U2
BbBP0WhDTi+nlH1vf7I9JWjSzWcgmRYkmS81JtJKBULM4GILl9OEB90IETGOkQSt
DO43KyeWfJ6joGCb7OqA1MVVApmvlUzyWkiR9v84qSalF7iGcGVOB0Ru4r95iAka
priuY1SLJToQF9MBjEexX72xh8gTjgJstFBtB1ENHsaV0nxDGo18j7z1I8SUIwPB
cWrxCEsLoz2aJiMqP+hQm5uuTd0bvdLILvwJ7q+akVPdPVJeQ6YbGoX9TD9Micap
wEHbgFVGCL5tmTLQDzq4OtA1Qxb5xBZUgOlNsWFGJB43VHk/+zGFKlOZsbadAmhL
1SnqLuVx2J6QmWlhzZnChj7NLPalvZBDw8KrantYdIszIRRmxG2II4KTwO6GqS0H
p96yXKIEOVLkf0mC9FjWINrhN5FOa4h2Fxua1VYuwth66MGz12w+Qh8MHoaJvCTW
qyotERM1kUsEiGUFKSN334uvr034WCNOZacLn0yJF74Cfr/NsAQlAk6uS784XLI+
QoAjh+qjhuuZHnicq1oCCXpakQfwj3kSPjOFy70Rfw/hJuTzsiv66cVrvrztRaBD
5ARnMZtGXIVyfQ/eNGtWnBiV3J3GKabIB9wO+WQ1jmDOwZImFgQ=
=ZjS6
-----END PGP SIGNATURE-----



More information about the nginx mailing list