Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Cookies from a PHP backend vanish behind an nginx proxy

ErrorSet-Cookie is in the backend response but the browser's cookie storage stays empty

Short answer

nginx forwards Set-Cookie headers from the backend unchanged, so lost cookies usually come from the cookie itself: a domain or path that does not match the public URL, or Secure and SameSite flags the browser rejects. Or the config hides Set-Cookie or drops the Host header. Rewrite the attributes with proxy_cookie_* directives.

  • Applies to nginx 1.26, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Compare the Set-Cookie header from the backend with the one nginx sends to the browser. The difference shows which cause applies.

    curl -s -D - -o /dev/null http://127.0.0.1:8080/login | grep -i set-cookie
    curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie
  2. 2Match what you see, then jump to its fix

    1. 1Set-Cookie: ...; domain=localhost (or an internal host name)The cookie's domain attribute names the backend hostFix
    2. 2Set-Cookie: ...; path=/app/ while the site serves the app under /The cookie path does not match the public URL prefixFix
    3. 3Set-Cookie includes Secure or SameSite=None and the browser rejects itThe cookie has Secure or SameSite flags that the public connection cannot satisfyFix
    4. 4The backend builds cookie domains from proxy_host (127.0.0.1:8080), or a proxy_hide_header Set-Cookie line existsThe backend sees the wrong Host, or Set-Cookie is hidden in the configFix

What does the error look like?

  • Set-Cookie is in the backend response but the browser's cookie storage stays empty
  • Login works when you call the backend directly but fails through nginx

Cause 1 of 4

The cookie's domain attribute names the backend host

1Check

If the backend writes its own host into domain=, the browser refuses the cookie for example.com. The first curl above shows it.

curl -s -D - -o /dev/null http://127.0.0.1:8080/login | grep -i 'domain='

2Fix

Rewrite the attribute on the way out. The directive replaces the domain value in Set-Cookie headers from the proxied server.

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_cookie_domain localhost example.com;
}

3Verify

Reload nginx and compare the header again.

sudo nginx -t && sudo systemctl reload nginx
curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie

Confirmed by nginx.org

Did this fix it?

Cause 2 of 4

The cookie path does not match the public URL prefix

1Check

When the proxy maps /app/ on the backend to / in public (or the other way round), a cookie scoped to the backend path is not sent back by the browser.

curl -s -D - -o /dev/null http://127.0.0.1:8080/app/login | grep -i 'path='

2Fix

Rewrite the path attribute so it matches the public path.

location / {
    proxy_pass http://127.0.0.1:8080/app/;
    proxy_cookie_path /app/ /;
}

3Verify

The browser shows the cookie with path / after the next login.

curl -s -D - -o /dev/null https://example.com/login | grep -i 'path='

Confirmed by nginx.org

Did this fix it?

Cause 3 of 4

The cookie has Secure or SameSite flags that the public connection cannot satisfy

1Check

Read the flags in the backend response. Flags that do not fit the connection the browser really uses can make it refuse the cookie.

curl -s -D - -o /dev/null http://127.0.0.1:8080/login | grep -i set-cookie

2Fix

Add or remove cookie flags in nginx. Available flags include secure, httponly, samesite=lax and nosecure.

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_cookie_flags ~ secure httponly samesite=lax;
}

3Verify

The public response now shows the flags you set.

sudo nginx -t && sudo systemctl reload nginx
curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie

Confirmed by nginx.org

Did this fix it?

Cause 4 of 4

The backend sees the wrong Host, or Set-Cookie is hidden in the config

1Check

By default nginx sends Host as $proxy_host to the backend. Also search the configuration for directives that hide the header.

sudo grep -rn 'proxy_hide_header\|proxy_set_header\|proxy_pass_request_headers' /etc/nginx/

2Fix

Send the original Host, and remove any line that hides Set-Cookie. If a header was hidden elsewhere, proxy_pass_header lets it through again. Note that proxy_set_header lines at a lower level replace those inherited from above.

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host;
}

3Verify

The backend now receives the public host name and issues cookies for it.

sudo nginx -t && sudo systemctl reload nginx
curl -s -D - -o /dev/null https://example.com/login | grep -i set-cookie

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Set the cookie domain, path and flags in the app configuration where you can, so nginx rewriting is only a bridge.
  • Define all proxy_set_header lines in the one block that needs them, since a block with its own lines drops the inherited ones.
  • Keep one curl command in your deploy notes that prints the Set-Cookie headers from the backend and the public URL.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

nginx gives up waiting for the app and returns 504

upstream timed out (110: Connection timed out) while reading response header from upstream

nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.