C
C
Cyber-Man2016-06-28 11:04:31
Nginx
Cyber-Man, 2016-06-28 11:04:31

How to automatically get backend value in nginx (for example, from listen)?

The situation is as follows.. there are hundreds of sites, nginx proxies them. A file is included in each server section so that it is possible to quickly register configs for everyone at once. Until now, I have prescribed various conditions, everything was fine ... until I came across location and proxy_pas.
The matter is that proxy_pas demands to register bekend. In my case, hundreds of sites have different backends in the form of IP addresses. These IPs, for example, match the listen in the server section. If the value of proxy_pas does not match listen, then return 404 for the specified location.
How can I automatically determine the backend corresponding to the site .. can I somehow extract the IP value from listen ..?
Here is the code that is included for each site:

location ~ ^/(wp-admin|wp-login\.php|admin|administrator) {
                        limit_req  zone=wp burst=4 nodelay;
                        proxy_pass http://IP:81;
                        proxy_redirect http://IP/ /;
                        proxy_set_header Host $host;
                        proxy_set_header Host $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        proxy_set_header X-Real-IP $remote_addr;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2016-06-28
@sim3x

Don't use any logic in configs
Don't be afraid to use copy-paste in configs
Make a template and substitute variables there
In your case, it's better to use sockets to communicate with fpm

A
Alexey, 2016-06-28
@alsopub

If everything is on the same server, then let the backend listen on the 127.0.0.1 interface.
Then - proxy_pass http://127.0.0.1:81;

K
kolu4iy, 2016-07-07
@kolu4iy

$server_addr is
the address of the server that accepted the request
. Getting the value of this variable usually requires one system call. To avoid a system call, listen directives should include addresses and use the bind option.
list of nginx variables
i.e. proxy_pass http://$server_addr:81;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question