Nginx 위치 정규식이 프록시 패스와 함께 작동하지 않습니다

Nginx 에서이 두 위치 지시문을 사용하려고하지만 Nginx를 부팅 할 때 약간의 오류가 발생합니다.

   location ~ ^/smx/(test|production) {
        proxy_pass   http://localhost:8181/cxf;
    }

    location ~ ^/es/(test|production) {
        proxy_pass   http://localhost:9200/;
    }

이것은 내가받는 오류입니다.

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block

누구에게나 친숙하게 들립니까? 내가 여기서 잃어버린 것?



답변

Xaviar 의 위대한 답변에 대한 작은 추가 사항 :

nginx에 대해 잘 알지 못하는 경우 proxy_pass지시문 끝에 슬래시를 추가하는 것 사이에는 중요한 차이점이 있습니다.

다음 작동 하지 않습니다 .

location ~* ^/dir/ {
  rewrite ^/dir/(.*) /$1 break;
  proxy_pass http://backend/;

그러나 이것은 않습니다 :

location ~* ^/dir/ {
  rewrite ^/dir/(.*) /$1 break;
  proxy_pass http://backend;

차이점 /proxy_pass지시문 의 끝에 있습니다.


답변

프록시 패스 지시문 의 URI 를 정규식 위치에서 사용할 수 없음을 알려줍니다 . 이는 nginx가 location블록 의 정규식과 일치하는 URI 부분을 proxy_pass지시문에 전달 된 일반적인 부분으로 대체 할 수 없기 때문 입니다.

단순히 당신의 위치 정규식 상상 /foo/(.*)/bar, 당신은 지정 proxy_pass http://server/test당신이 종료 싶지 않기 때문에 nginx를가 상위에 또 하나 위치 정규식을지도해야 할 것입니다, /foo/test/bar/something하지만 함께 /test/something. 따라서 기본적으로 불가능합니다.

따라서이 부분에서는 다음을 사용하여 작동합니다.

server {

   [ ... ]

    location ~ ^/smx/(test|production) {
        rewrite ^/smx/(?:test|production)/(.*)$ /cxf/$1 break;
        proxy_pass http://localhost:8181;
    }

    location ~ ^/es/(test|production) {
        rewrite ^/es/(?:test|production)/(.*)$ /$1 break;
        proxy_pass http://localhost:9200;
    }

}

그러나 처리중인 현재 URI를 다시 쓰므로 위치 블록 URI 패턴과 일치하도록 경로 재 지정을 다시 작성할 수 없으므로 다시 쓰기 전에 초기 요청Location 에 따라 헤더 를 변경할 수 없습니다 .


답변