How to get automatically the value of the backends in nginx (e.g. listen)?

The situation is as follows.. there are hundreds of sites their proxy nginx. Each section is connected to the file server, so was able to quickly register for all the configs at once.. To this day prescribed for various conditions, everything was fine... until he came to a location and proxy_pas.

The fact that proxy_pas requires to register the backend. In my case, for hundreds of sites written different backends in the form of IP addresses. These IP, for example, coincide with listen in the section server. If the value proxy_pas is not the same as listen, it gives a 404 for the specified location.

How is it possible to automatically determine the appropriate backend of the site..can only listen from the value of the IP can be pulled out..?

Here is the code included to 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;
}
July 8th 19 at 11:27
3 answers
July 8th 19 at 11:29
Don't use any logic in configs
Don't be afraid to use copy-paste the configs

Make a template and substitute back the variables

In your case it is better to use sockets for communication with the FPM
sorry of course, but little realized, but the first two lines)) more? - uriel.Roh commented on July 8th 19 at 11:32
:
and if so?
ip=127.0.0.1; echo "upstream my_upstream { server $ip:123457} server { listen: $ip; } location / { proxy_pass <a href="http://my_upstream;}">http://my_upstream;}</a> location /2 { proxy_pass http://localhost:12346567;}" > my_nginx.conf ; cat my_nginx.conf
- marilyne_Roh commented on July 8th 19 at 11:35
July 8th 19 at 11:31
If everything is on one server, then let the backend listens to 127.0.0.1 interface.
Then proxy_pass http://127.0.0.1:81;
July 8th 19 at 11:33
$server_addr
the address of the server which accepted a request
Getting the value of this variable usually requires one system call. To avoid a system call, the listen directives must specify addresses and use the bind parameter.

the list of variables is nginx

ie proxy_pass http://$server_addr:81;

Find more questions by tags CentOSServersNginx