Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问一下使用nginx反向代理后sessionid每次请求都会改变 #70

Open
linjifu opened this issue Nov 30, 2024 · 0 comments
Open

Comments

@linjifu
Copy link

linjifu commented Nov 30, 2024

Nginx
`
upstream hyperf {
ip_hash; # 启用会话保持
# Hyperf HTTP Server 的 IP 及 端口
server 127.0.0.1:9581;
}

server {
listen 80;
server_name www.ascore.local;

charset utf-8;

access_log  /usr/local/var/log/nginx/ascore/access.log;
error_log  /usr/local/var/log/nginx/ascore/error.log;

# 忽略 favicon.ico 请求,直接反向代理给 Hyperf
location = /favicon.ico {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cookie_path / "/; secure; HttpOnly; SameSite=Lax";
    proxy_pass http://hyperf;
}

# 处理 CSS 请求,直接反向代理给 Hyperf
location ~* \.css$ {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cookie_path / "/; secure; HttpOnly; SameSite=Lax";
    proxy_pass http://hyperf;
}

# 处理 JS 请求,直接反向代理给 Hyperf
location ~* \.js$ {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cookie_path / "/; secure; HttpOnly; SameSite=Lax";
    proxy_pass http://hyperf;
}

# 处理图片请求,直接反向代理给 Hyperf
location ~* \.(jpg|jpeg|png|gif|svg)$ {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_cookie_path / "/; secure; HttpOnly; SameSite=Lax";
    proxy_pass http://hyperf;
}

location / {
# 重写规则
if ($request_uri ~* ^/en(/|$)) {
        rewrite ^/(.*)$ /404 break;
    }
    rewrite ^/(.*)/$ /$1 permanent;
rewrite ^/(en|hi)$ /$1 break;
    rewrite ^/(en|hi)/(.*)$ /$1/$2 break;
rewrite ^/$ /en last;
    if ($request_uri !~* ^/(en|hi)/) {
        rewrite ^/(.*)$ /en/$1 last;
    }
    # 将客户端的 Host 和 IP 信息一并转发到对应节点  
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Cookie $http_cookie; # 保持原有的Cookie

    # 转发Cookie,设置 SameSite
    proxy_cookie_path / "/; secure; HttpOnly; SameSite=Lax";
    
    # 执行代理访问真实服务器
    proxy_pass http://hyperf;
}

}
sessionreturn [
'handler' => Handler\FileHandler::class,
'options' => [
'connection' => 'default',
'path' => BASE_PATH . '/runtime/session',
'gc_maxlifetime' => 1200,
'session_name' => 'HYPERF_SESSION_ID',
'domain' => null,
'cookie_lifetime' => 5 * 60 * 60,
'cookie_same_site' => 'lax',
],
];`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant