변수가 있는 여러 위치에 대해 NGINX 구성 선언

변수가 있는 여러 위치에 대해 NGINX 구성 선언

하나의 도메인에 많은 서비스가 있습니다. 예를 들면:

  • example.com/data_first/
  • example.com/data_home/
  • example.com/data_test/

다른 사람

coocie auth를 사용하기 위해 구성된 모든 서비스에 대해 동일한 작업에는 NGINX 구성에 20개 이상의 줄이 포함됩니다.

하지만 새로운 서비스를 쉽게 추가할 수 있도록 하나의 포함된 구성으로 일반화하고 싶습니다.

나는 config next 지시문을 작성합니다:

map $request_uri $xService{
    ~^/data_(?<fp>(first|home|test))/ data_$fp;
    default 0;
}
server{
...
    if ($xService){
        include xService.conf;
    }
...
}

내 xService.conf에는 다음 지시문이 포함되어 있습니다.

location /$xService/ {
    add_header X-Pass-Return $xService;
    ... other directives include if, rewrite, proxy_pass 
}

하지만 if 지시어 내부에서는 위치 지시어를 사용할 수 없습니다. 가능한 모든 위치 옵션을 나열하지 않고도 이러한 문제를 해결할 수 있습니까?

예를 들어. 다음 구성을 사용합니다.

location ^~ /data_first/{
    set $xService data_first;
    include xService.conf;
}

및 기타 위치가 있지만 서비스 URI에 대한 위치를 쓸 필요는 없습니다.

답변1

지도와 if 문을 사용하는 이유는 무엇입니까?

server {
   server_name example.com;
   ...
   location /data_first {
      include snippets/YOUR_COMMON_CONFIG;
      # add the stuff specific to /data_first here...
      add_header X-Pass-Return /data_first/;
      ...
   }
   location /data_home {
      include snippets/YOUR_COMMON_CONFIG;
      # add the stuff specific to /data_home here...
      add_header X-Pass-Return /data_home/;
      ...
   }
   location /data_test {
      include snippets/YOUR_COMMON_CONFIG;
      # add the stuff specific to /data_test here...
      add_header X-Pass-Return /data_test/;
      ...
   }
}

답변2

간단한 것을 사용할 수도 있습니다.

location ~ ^/(data_\w+)/ {
    add_header X-Pass-Return /$1/;
    ...
}

관련 정보