简体   繁体   中英

Cookies are not sent with request in nginx

I have problem with cookies, nginx and the backend server.

What I would like to achieve is something like this:

NGINX AT PORT 70
location / {
If session cookie is not set
return 301 localhost/login.html
else
proxy_pass http://somewhere 
}
location /login.html{
 proxy_pass http://localhost:8080 <- backend server with logging at localhost:8080/login.html
}
location /redirection/ {
return 301 localhost:70
}

so it should go to login.html and proxy the pass to logging server so that I get localhost:8080/login.html and can log in. Then it process the credentials and redirect to /redirection/ in form action (processing is done in JavaScript - it's actually for now setting cookies only). After being in redirection server should send request to itself, but now it has session cookie so it's going to http://somewhere .

But mine problem is that in login.html cookies should be set, but they aren't. If I'm going to localhost:8080/login.html alone and try - then cookies work well. But somehow with proxy_pass cookies are lost.

PS If I use two server block and /login.html and /redirection are on this second server it works well, but I can't have two server blocks in my configuration - I need to have everything on one port.

Well... I did resolve this issue.. Like I said it was js script setting mine cookies. But it seems that js scripts didn't load as it went straight ahead to "/" location. what I did was:

location /js/ {
proxy_pass http://localhost:8080;
}

with this everything is working.

So remember.. if Your html files on backend server contain any css/js files You need to take into account that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM