NGINX - настройка location
Плавно перевожу сайт с Apache на Django, фронтует NGINX То есть когда определенные страницы готовы я добавляю правило в location. Но уже правил стало слишком много, и если внутри вносятся изменения их надо дублировать.
Решил попробовать такую конструкцию через переменную, то есть если переменная установлена - редиректим на одно, если нет то на другой:
#1123
set $django "yes";
#set $django "no";
if ( $request_uri = "/new_page.html") { set $django "yes"; }
if ( $django = "yes" ) {
proxy_pass http://unix:/run/django.sock;
proxy_redirect off;
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
break;
}
#321
location / {
proxy_pass http://127.0.0.1:8081;
}
в итоге правило не срабатает. куда копать?
Ответы (1 шт):
Автор решения: nörbörnën
→ Ссылка
upstream backend_old {
server 127.0.0.1:8081;
}
upstream backend_new {
server unix:/run/django.sock;
}
map $uri $backend {
~/new_page.html backend_new;
^/second_page.html$ backend_new;
/other_page.html$ backend_new;
default backend_old;
}
server {
proxy_redirect off;
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_pass http://$backend;
}
}
upstream: https://nginx.org/ru/docs/http/ngx_http_upstream_module.html
map: https://nginx.org/ru/docs/http/ngx_http_map_module.html
If is Evil...: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/