NGINX가 있는 사이트를 다른 사이트로 프록시

NGINX가 있는 사이트를 다른 사이트로 프록시

(http://위치:포트/) NGINX를 사용하여 프록시를 사용하려고 합니다.

(에 다른 사이트가 있습니다.http://위치2:포트2) 그리고 그것을 프록시로 사용하고 싶습니다.

  1. http://main.com사이트 1은 다음을 통해 액세스됩니다 .
  2. http://main.com/site2사이트 2는 모든 하위 링크를 통해 액세스됩니다.

내 시도가 실패하고 있습니다.

server {
    listen  80;

    index index.html;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://landing-page:5000;
        proxy_redirect off;
    }

    location /insights {
        return 302 $uri/;
    }

    location /insights/ {
        # proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # proxy_set_header Host $http_host;
        # proxy_set_header X-NginX-Proxy true;

        rewrite ^/insights/?(.*) /$1 break;

        proxy_pass http://ghost:2368/;
        # proxy_redirect off;
    }
}

추가 정보

저는 Docker에서 NGINX를 실행하고 있으며 Mac에서 포트 80을 8080으로 매핑해야 합니다.

제가 찾은 내용은 다음과 같습니다.

  1. http://localhost:8080/insightshttp://localhost/insights/브라우저에 URL을 설정합니다.
  2. http://localhost:8080/insights/ghostURL을 다음으로 설정합니다.http://localhost:8080/ghost/
  3. http://localhost:8080/insights/URL을 다음으로 설정합니다.http://localhost:8080/insights/
  4. http://localhost:8080/insights/ghost/URL을 다음으로 설정합니다.http://localhost:8080/insights/ghost/

끝 슬래시가 있는 모든 URL은 잘 작동하는 것 같습니다. 왜?

답변1

블록 location /은 파일 끝에 있어야 합니다. 위치 블록은 순서대로 처리되며, 일치하는 첫 번째 블록이 실행됩니다. 모든 요청과 일치 하므로 location /항상 실행됩니다.

일반적으로 가장 구체적인 블록이 먼저 있고, 그 다음에는 덜 구체적인 블록이 있고, 마지막으로 location /다른 모든 요청에 ​​대해서는 으로 끝나야 합니다.

파일은 다음과 같이 배치되어야 합니다.

server {
    listen  80;

    index index.html;

    location /insights/ {
        # proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # proxy_set_header Host $http_host;
        # proxy_set_header X-NginX-Proxy true;

        rewrite ^/insights/?(.*) /$1 break;

        proxy_pass http://ghost:2368/;
        # proxy_redirect off;
    }

    location /insights {
        return 302 $uri/;
    }

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://landing-page:5000;
        proxy_redirect off;
    }
}

관련 정보