My Setup
I have a full stack application with a backend powered by NestJS, Redis for session management, and Postgres for the database. The backend is hosted on a VPS server using nginx and available at backend.exampleurl.com
, while the frontend is accessible at frontend.exampleurl.com
. I utilize cookie-based authentication with sessions stored in Redis.
Backend Configuration
app.use(
session({
secret: config.getOrThrow<string>('SESSION_SECRET'),
name: config.getOrThrow<string>('SESSION_NAME'),
resave: true,
saveUninitialized: false,
cookie: {
domain: '.exampleurl.com',
maxAge: 604800000,
httpOnly: true,
secure: true,
sameSite: 'none',
},
store: new RedisStore({
client: redis,
prefix: config.getOrThrow<string>('SESSION_FOLDER'),
}),
}),
);
app.enableCors({
credentials: true,
exposedHeaders: ['Set-Cookie'],
origin: 'https://frontend.exampleurl.com',
allowedHeaders: 'Content-Type, Accept, Authorization',
});
Nginx Configuration
location / {
proxy_pass http://127.0.0.1:8001;
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;
proxy_set_header Cookie $http_cookie;
proxy_pass_request_headers on;
proxy_pass_header Set-Cookie;
proxy_pass_header Access-Control-Allow-Origin;
proxy_pass_header Access-Control-Allow-Credentials;
proxy_pass_header Access-Control-Allow-Headers;
proxy_pass_header Access-Control-Expose-Headers;
proxy_pass_header Access-Control-Allow-Methods;
add_header 'Cache-Control' "no-store, no-cache, must-revalidate, max-age=0";
}
The Issue
When I attempt to authenticate from the frontend, the auth cookie is not sent back from the backend; the response lacks the Set-Cookie header entirely.
I’ve tested it locally, running both the backend at https://localhost:8001
and the frontend at https://localhost:3000
with identical cookie settings, and everything works flawlessly. However, once deployed, the cookies fail to transfer. Is it possible that nginx is causing this issue?