Cookies from a PHP backend vanish behind an nginx proxy
Set-Cookie is in the backend response but the browser's cookie storage stays emptyShort 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.
Fix it fast
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-cookie2Match what you see, then jump to its fix
- 1
Set-Cookie: ...; domain=localhost (or an internal host name)The cookie's domain attribute names the backend hostFix - 2
Set-Cookie: ...; path=/app/ while the site serves the app under /The cookie path does not match the public URL prefixFix - 3
Set-Cookie includes Secure or SameSite=None and the browser rejects itThe cookie has Secure or SameSite flags that the public connection cannot satisfyFix - 4
The 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
- 1
What does the error look like?
Set-Cookie is in the backend response but the browser's cookie storage stays emptyLogin 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-cookieConfirmed by nginx.org
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
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-cookie2Fix
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-cookieConfirmed by nginx.org
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-cookieConfirmed by nginx.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
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 fixServers
nginx and PHP-FPM show File not found or Primary script unknown
File not found.
PHP-FPM received a script path it could not turn into a file.
4 causes, each with a fixServers
nginx still returns 504 although the PHP script has a long time limit
upstream timed out (110: Connection timed out) while reading response header from upstream
set_time_limit() only controls PHP.
4 causes, each with a fixServers
nginx cannot open the PHP-FPM socket: Permission denied
502 Bad Gateway
nginx runs as one user and PHP-FPM creates its socket for another.
3 causes, each with a fixServers
display_errors has no effect on PHP behind nginx and PHP-FPM
500 Internal Server Error with no PHP message in the browser
With nginx there is no web-server PHP setting.
4 causes, each with a fixServers
nginx cannot find the PHP-FPM socket file
502 Bad Gateway
nginx and the PHP-FPM pool must name the same address.
2 causes, each with a fix