J
J
jeruthadam2019-08-07 17:43:45
Nginx
jeruthadam, 2019-08-07 17:43:45

How to replace part of the url with a variable?

Is it possible to somehow change the proxy_pass url depending on the host?
Here it works (hardcode)

proxy_pass http://media.static/mysite.com/static/robots.txt;

But this doesn't work anymore
proxy_pass http://media.static/$host/static/robots.txt;

At the same time, $host is mysite.com

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2019-08-07
@ky0

Using variables in proxy_pass implies that the URI specified is a full one. Quoting ​the documentation:
A server name, its port and the passed URI can also be specified using variables:
proxy_pass http://$host$uri;
This behavior is not very intuitive, though it's how it works. Variables in proxy_pass were introduced
If you want nginx to use URI of the original request instead of the one specified in the proxy_pass, avoid specifying URI at all (note no trailing slash):
proxy_pass http://$host;
If you want to pass some changed URI, you can do so by using rewrite ... break in the same location.

https://trac.nginx.org/nginx/ticket/1067

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question