Nginx Does Not Pass Cookies To Proxy
When configuring nginx to proxy API requests behind a load balancer or reverse proxy, one of the most persistent headaches is cookie handling. The scenario you describedâwhere cookies set on .example.com fail to persist when making AJAX calls through an nginx proxy_pass blockâis a classic misconfiguration involving domain rewriting and header forwarding.
In your specific setup, the root cause lies in the interaction between proxy_cookie_domain and how browsers enforce SameSite and Domain policies. When you set proxy_cookie_domain $host example.com, you are instructing nginx to rewrite the Domain attribute of cookies sent back from the backend (api.example.com) to match the frontend host. However, this directive does not automatically fix the domain attribute of incoming cookies sent from the browser to the backend, nor does it ensure the browser will send a cookie with an "N/A" or invalid domain if the initial set-cookie logic is flawed.
The image you referenced showing a cookie domain of "N/A" suggests that the browser is not attaching the expected Set-Cookie header to the outgoing request, or more likely, the backend is rejecting it because the Domain attribute doesn't match what the server expects. If your cookies are set on .example.com (which covers www.example.com and api.example.com), the browser should send them automatically. The fact that they aren't indicates a disconnect in how the request headers are being passed or interpreted during the proxy transaction.
The Proxy Cookie Domain Trap
The directive proxy_cookie_domain is often misunderstood. Its primary function is to modify cookies arriving from the upstream server (the backend) so that clients accept them on the correct frontend domain. It rewrites the Domain attribute of a response header.
It does not, however, influence how the browser constructs the request headers unless the upstream server explicitly sets the cookie with an invalid or missing domain. If your backend API returns a Set-Cookie: session_id=xyz; Domain=.example.com, the browser stores it correctly. When you later make an AJAX call to /api, the browser sends Cookie: session_id=xyz in the header.
If nginx sees this incoming Cookie header and the upstream server is configured strictly (e.g., using Strict-Transport-Security or specific cookie policies), the upstream might reject the request if it doesn't match its own expectations, though usually, browsers handle the client-side domain logic independently of the proxy.
Missing X-Forwarded Headers for Cookie Context
A more subtle issue often arises with session affinity and forwarded headers. In your configuration, you are forwarding X-Forwarded-For and X-Forwarded-Host. While critical for logging and geo-location, these do not directly solve the cookie domain mismatch unless the backend relies on them to determine the client's origin.
However, the real culprit in your specific case is likely how the cookie was originally set versus how it is being sent. If you are seeing "N/A" in your inspection tools, it often means the browser hasn't actually attached the cookie yet, or the Domain attribute in the storage is null because of a previous redirect or an insecure vs. secure mismatch.
Ensure that your original cookie setting (likely done by PHP via session.cookie_domain) explicitly includes the dot:
session.cookie_domain = '.example.com';
Without the leading dot, the cookie is tied strictly to www.example.com. If your AJAX call goes through a proxy that strips or modifies the host header, or if the backend expects the domain to be .example.com but receives www.example.com, the browser might withhold the cookie.
Correcting the Nginx Configuration
To ensure cookies pass correctly, you must verify that nginx is not inadvertently stripping them and that the upstream server accepts the forwarded request. Your current config includes proxy_pass_header Set-Cookie;, which is good for responses, but you need to ensure the Cookie header in the request is passed intact:
location /api {
proxy_pass http://api.example.com/;
# Ensure upstream receives the original cookie header
proxy_set_header Host $host;
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;
# Pass the cookie header from client to upstream
proxy_pass_request_body on;
proxy_set_header Content-Length "";
proxy_http_version 1.1;
# Do not rewrite the domain of incoming cookies unless necessary
# Remove or carefully adjust proxy_cookie_domain if causing issues
proxy_cookie_domain "example.com" ".example.com";
}
Note that proxy_cookie_domain is deprecated in newer versions of nginx (as of 1.25.0) and replaced by proxy_cookie_path. If you are on a modern version, ensure you aren't using the old directive which might behave unexpectedly.
Why Apache Worked
Apache handles cookies slightly differently due to its built-in RewriteEngine. It often preserves the original Host header better and has more lenient default behaviors regarding cookie domain rewriting in .htaccess files compared to nginx's stricter requirement for explicit directives. If your PHP session configuration is identical on both stacks, the difference usually lies in how the reverse proxy passes the request context to the backend.
Final Verification
Before assuming a bug in nginx, inspect the actual network traffic in DevTools. Look at the Request Headers tab. Does the Cookie header exist? If it does, check its value. If it is missing, the browser is withholding it, likely due to the domain attribute not matching the current URL scheme or host exactly.
If the backend API requires specific headers to identify the session (like a user-agent string or IP address) and nginx isn't forwarding them correctly, the backend might be rejecting the session before it can read the cookie. Ensure X-Forwarded-For and X-Real-IP are present if the backend uses them for session validation.
By aligning the Cookie domain attribute with the actual host header passed to the upstream server and ensuring no deprecated directives interfere, you should see the cookies persist correctly across your proxy boundary.