鍍金池/ 問答/Java  Linux  HTML/ nginx 做反向代理時(shí) localhost無法保存cookie

nginx 做反向代理時(shí) localhost無法保存cookie

在做vue+springboot+nginx的時(shí)候,發(fā)現(xiàn)使用localhost登錄后無法訪問接口,每次都會(huì)提示沒有登錄而被拒絕訪問,感覺每次都是另外一臺(tái)服務(wù)器在訪問一樣。查看請(qǐng)求,發(fā)現(xiàn)第二次請(qǐng)求并沒有發(fā)送登錄后的sessionId過去,所以判斷是cookie未保存。測試發(fā)現(xiàn)localhost和127.0.0.1都無法保存上cookie,換成內(nèi)網(wǎng)IP,在本地hosts文件映射一個(gè)域名到本機(jī),或者直接使用postman訪問springboot都是可以的。原因未知,大家可以討論下。nginx配置如下:`

worker_processes 1;

events {

worker_connections  1024;

}

http {

include       mime.types;
default_type  application/octet-stream;

keepalive_timeout  65;

gzip  on;

server {
    listen       80;
    server_name  localhost;
    proxy_set_header Host $host; 
    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    
    location  ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff)$ {
        rewrite /login.html /index.html permanent;
        root D:/Vue/bmsfront/dist;
        index index.html;
    }
    
    location = / {
        root D:/Vue/bmsfront/dist;
        index index.html;
    }
    
    location / {
        proxy_pass http://back_app;            
    }

}

upstream back_app {
    ip_hash;
    server localhost:8080;
    
    
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}
`

回答
編輯回答
情皺

你們代碼有沒設(shè)置白名單。是否設(shè)置只允許指定域名可以攜帶cookie

2017年5月30日 11:02