programing

Nginx가 쿠키를 프록시로 전달하지 않음

codeshow 2023. 7. 31. 21:59
반응형

Nginx가 쿠키를 프록시로 전달하지 않음

나는 모든 하위 도메인인 .example.com 에서 작동하는 쿠키 세트를 가지고 있습니다. 나는 nginx agax 호출이 proxy_pass를 통과하지만 쿠키는 남아 있지 않습니다.내 구성은 다음과 같습니다.

server {
    listen   80;
    server_name  www.example.com;

    location / {
        root   /data/sites/www.example.com/widgets/public_html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?rt=$uri&$args;
    }

    location ~ .php$ {
      root          /data/sites/www.example.com/site/public_html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param REQUEST_URI    $uri;
      fastcgi_index  index.php;
      include        fastcgi_params;
      fastcgi_param ENV staging;
    }


    location /api {
        proxy_pass_header  Set-Cookie;
        proxy_cookie_domain $host example.com;
        proxy_pass_header  P3P;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Fowarded-Host $host;
        proxy_pass http://api.example.com/;
        proxy_connect_timeout 1;
    }

}

그러나 Ajax 호출을 확인해보니 다음과 같습니다.

enter image description here

위의 그림은 전송되는 쿠키가 .example.com 이어야 할 때 도메인이 N/A임을 보여줍니다. 이 쿠키는 Apache/PHP 구성에서는 작동하지만 nginx/dll 구성에서는 작동하지 않습니다.내가 뭘 잘못하고 있는 거지?

location / {
  proxy_pass http://localhost:8000;
  proxy_set_header Host $host; # MAGIC
}

만약 당신의 쿠키가 DOMAIN.com/wp-admin 처럼 작동하지만 당신이 DOMAIN.com 으로 돌아갈 때 당신의 쿠키는 도움이 될 수 있습니다.

SSL이 없는 포트 80에서 이것은 내 문제를 해결했습니다.

호스트 프록시 URL에 $uri 추가:

    location / {
        proxy_pass http://192.168.1.12;
        proxy_set_header Host $host$uri;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Scheme $scheme;
        

}

하지만 제 경우에는 SSL을 사용하여 다른 서버에 대해 동일한 위치를 시도했을 때 400개의 오류가 있었고 이 답변을 기반으로 했습니다. https://stackoverflow.com/a/64126257/19293776

나는 $uri를 삭제하고 그 행들을 추가했습니다.

            location / {
        proxy_pass http://192.168.2.12;
        proxy_set_header Host $host;
         ######
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
         ######
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Scheme $scheme;
       }

언급URL : https://stackoverflow.com/questions/22175000/nginx-does-not-pass-cookies-to-proxy

반응형