我有一台 Express 服务器,其配置如下:
if (NODE_ENV === "production") {
app.set("trust proxy", 1);
}
app.use(
session({
store: new SQLiteStore({
db: "database.sqlite",
concurrentDB: true
}),
secret: SESSION_SECRET!,
resave: false,
saveUninitialized: false,
cookie: {
domain:
NODE_ENV === "production" ? "akira-bot.netlify.com" : "localhost",
secure: NODE_ENV === "production",
httpOnly: true
}
})
);
我的 Nginx 配置如下
location /akira/ {
proxy_pass http://localhost:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
}
从外部身份验证服务重定向时,未设置会话https://discordapp.com/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2Fakira-bot.netlify.com%2Fdashboard&scope=identify%20guilds&client_id=647860115239731220这可能是什么原因呢?
答案1
会话通常通过 cookie 来维护,这些 cookie 仅发送到 cookie 中指定的域。根据您的上一个问题,akira-bot.netlify.com
不是你服务器的域名。所以修改这部分:
cookie: {
domain:
NODE_ENV === "production" ? "akira-bot.netlify.com" : "localhost",
secure: NODE_ENV === "production",
httpOnly: true
}
备注:如果将 cookie 设置为安全的,仅当通过 SSL 连接时才会发送。