쿠키를 기반으로 Proxy_pass와 uwsgi_pass 사이를 전환하시겠습니까?

쿠키를 기반으로 Proxy_pass와 uwsgi_pass 사이를 전환하시겠습니까?

내 nginx 서버는 현재 다음을 통해 모든 요청을 처리하도록 설정되어 있습니다 uwsgi_pass.

location / {
  uwsgi_pass unix:///tmp/uwsgi-zerocater.sock;
  include uwsgi_params;
  uwsgi_read_timeout 7200;
  client_max_body_size 20M;
}

new_fe내 애플리케이션 코드에서는 값이 "True" 또는 "False" 인 쿠키를 설정했습니다 .

값이 "True"이면 다른 외부 서버로 대신하고 싶지만 proxy_pass특정 경로에 대해서만 가능합니다. 다음과 같은 것의사 코드:

location ~ "^/my/complicated/regex$" {
  if ($cookie_new_fe = "True") {
    # pass the request to my other server
    # at something like http://other.server.com
  } else {
    # do the usual uwsgi stuff
  }
}

어떻게 해야 하나요? 나는 아직 을 처음 접했기 nginx때문에 내가 놓친 간단한 것이 있다면 질문을 용서해 주십시오.

답변1

임시 해결책으로는 다음과 같이 작동합니다.

location ~ "^/my/complicated/regex$" {
  if ($cookie_new_fe = "True") {
    error_page 418 = @proxy;
    return 418;
  }

  # do the usual uwsgi stuff
}

location @proxy {
    # do proxy stuff
}

답변2

나는 지도를 좀 더 살펴보고 작동하는 것처럼 보이는 이 솔루션을 생각해 냈습니다(@womble과 @alexey-ten의 제안을 모두 사용함).

map $cookie_new_fe $serve_request_from {
  default @uwsgi;
  "True" @proxy;
}

server {

  # ...

  # BEGIN Routes to switch based on value of $cookie_new_fe
  location ~ "^/my/complicated/regex$" {
    error_page 418 = $serve_request_from;
    return 418;
  }
  # END

  location / {
    error_page 418 = @uwsgi;
    return 418;
  }

  location @uwsgi {
    uwsgi_pass unix:///tmp/uwsgi-origserver.sock;
    include uwsgi_params;
    uwsgi_read_timeout 7200;
    client_max_body_size 20M;
  }

  location @proxy {
    proxy_pass https://other.server.com;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
  }
}

이렇게 하면 이 마이그레이션이 완료될 때까지 중간에 여기에 추가해야 하는 여러 경로에서 중복이 최소화됩니다.

관련 정보