Rails 4.2.6 + nginx + puma 会话存储和自定义标头为发出请求的第一个用户数据提供服务

问题描述 投票:0回答:1

我对 nginx + puma 或 ruby 线程有问题,添加 nginx 后,服务器显示第一个具有自定义标头的用户 从其他应用程序转发

HTTP_MY_APP_REQUEST_USER
谁发出了请求,例如,如果我 Henry 在使用自定义标头启动服务器后首先发出请求,然后其他人Jane,请求的还是我的用户数据。

我不确定问题出在哪里 这段代码与 niginx 相关吗?或者轨道

session_store :active_record_store

尝试添加

add_header Cache-Control "no-cache, no-store, must-revalidate";
没有任何改变。直接请求 puma,它运行正确,我可以看到两个不同的用户 Henry 和 Jane。

如果您内存中的 Nginx 服务器 cookie 具有相同的 IP? application_controller.rb

  def check_authentication(method_name)
    begin
      user_id = (request.headers['HTTP_MY_APP_REQUEST_USER'].to_i / 317) rescue 0
      # Thread.current is used other places to check user 
      Thread.current[:user_id] = (user_id > 0) ? user_id : session[:user].counter
    end
  end

Nginx 服务器配置

upstream puma {
   server 0.0.0.0:3000;
}

server {
    listen 9100;
    server_name localhost;
    client_max_body_size 100m;
    gzip             on;
    gzip_comp_level  4;
    gzip_min_length  1000;
    gzip_proxied     no-cache no-store;
    gzip_types       text/plain application/javascript application/json application/x-javascript text/xml text/css application/xml text/javascript;
    keepalive_timeout 10;
    proxy_cache_key "$scheme$request_method$host$request_uri";
    server_tokens off;
    add_header Cache-Control "no-cache, no-store, must-revalidate";
    add_header Pragma "no-cache";
    add_header Expires 0;  



    root /home/apps/server/current/public;

    location / {
        try_files $uri @puma;
    }

    location @puma {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_pass http://puma;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access_log;
}

知道什么会导致这种情况吗? 非常感谢!

ruby-on-rails nginx ruby-on-rails-4.2 ruby-2.3
1个回答
0
投票

问题出在带有下划线的自定义标头中,默认情况下不允许使用下划线标头!

HTTP_MY_APP_REQUEST_USER

添加

underscores_in_headers on;
解决了这个问题

© www.soinside.com 2019 - 2024. All rights reserved.