关于 nginx 反向代理和 express-session 无法获取到值的问题
发布于 6 年前 作者 bandianliancha 3167 次浏览 来自 问答

我用 nginx 反向代理绑定了域名,但是无论如何发现存储不了session ,后来查出来原因是因为,通过反向代理访问 connect.sid 每次访问都会变,从而一直获取不到,求大神解惑啊。困扰好几天了

express 配置

app.set('trust proxy', 1); // trust first proxy
app.use(session({ 
  name:"DWlive",
  resave: true, 
  sameSite:'false',
  saveUninitialized: true, 
  secret: 'this word key'
}));

nginx 配置

server
{
    listen 80;
    server_name watchlive.dongweinet.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/watchlive.dongweinet.com;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    

    location / 
    {
        proxy_pass http://211.149.207.217:250;
        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 REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-Proto true;
        
        #缓存相关配置
        proxy_cache cache_one;
        proxy_cache_key $host$request_uri$is_args$args;
        proxy_cache_valid 200 304 301 302 1h;
                
        add_header X-Cache $upstream_cache_status;
        
        expires 12h;
    }
     
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
}
5 回复

看来是无人能解了

          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host  $http_host;
          proxy_set_header X-Nginx-Proxy true;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
		  proxy_set_header Connection '';
          proxy_pass http://127.0.0.1:8080;
          proxy_redirect default;

@MiYogurt 大神,还是不行,还有没有办法

@MiYogurt http://watchlive.dongweinet.com/ :这个链接就是转发的 保存不上 http://watchlive.dongweinet.com:250/: 带上端口号不转发的就可以

image.png

有的时候看得到,关掉缓存试一下,我好像没看到缓存的头部。

image.png

回到顶部