Skip to content

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

Description

@linjifu

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',
],
];`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions