S
S
Sergey2017-03-22 15:46:06
Nginx
Sergey, 2017-03-22 15:46:06

How to make proxying dynamic in nginx?

Hello.
The task is to dynamically proxy to different servers depending on the subdomain (which server to proxy to is decided based on the information that lies in redis (via lua)). That is, the lua script fills in the $server variable = it is equal to either 1 or 2.
Openresty server.
location /
{
set $server "";
set $redisPort "6379";
set $redisDatabase "22";
set $redisHost "localhost";
set $domain "domain.ru";
# lua script fills $server variable = it is either 1 or 2
access_by_lua_file "/home/lua-scripts/subdomain.lua";
# Here we need code that will do proxying depending on the $server parameter
}
How to make it so that if the server variable $server = 1 - do not do proxying at all (stay on this server for php processing), if 2 - do = proxy_pass http://122.22.22:6789 ?
Tried through if ($server = 2) { proxy_pass http://122.22.22:6789 } - but it doesn't work for some reason.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Konyukhov, 2017-03-28
@piromanlynx

Map module to help: nginx.org/ru/docs/http/ngx_http_map_module.html

A
Andrey Nikolaev, 2017-03-22
@gromdron

Tried:

proxy_pass http://127.0.0.1:6789;

if ( $server ~ 2 ) {
  proxy_pass http://122.22.22:6789;
}

R
ralaton121, 2017-08-15
@ralaton121

For example, through the Lua module.
And you are already studying Lua, judging by your questions
https://habrahabr.ru/post/321864/
https://habrahabr.ru/company/2gis/blog/199504/
https://habrahabr.ru/post/215237/
https://habrahabr.ru/post/326486/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question