ruby-on-rails - Rails 全局变量 : is global variable share between http requests?

标签 ruby-on-rails nginx passenger

我已经在我的应用程序 Controller 中声明了全局变量,并且我在每个 http 请求上递增它。

我测试了此功能,发现全局变量在每个 http 请求上都会递增。

我使用 nginx 作为 Web 服务器,并使用乘客作为应用程序服务器。我读过很多关于passenger的文章,并了解到passenger为每个http请求创建进程,每个进程都有自己的全局变量,因此全局变量不能在每个http请求之间共享。每个 http 请求都有自己的全局变量副本。这是真的吗?如果是的话,那么为什么在我的例子中,全局变量在每个 http 请求上都会递增。

nginxconf*****

#user  nobody;
worker_processes  1;
error_log /var/log/nginx-error.log info;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    passenger_root /home/ubuntu/.rvm/gems/ruby-2.0.0-p598/gems/passenger-5.0.7;
    passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.0.0-p598/wrappers/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        passenger_enabled on;
        client_max_body_size 10000M;
        #access_log  logs/host.access.log  main;

        location / {
           
            index  index.html index.htm;
        }
        root /var/www/application/Test/current/public;


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
     

   

谢谢

最佳答案

passenger creates process for every http request

这将是极其低效的。乘客不会这样做。相反,它创建了一个持续运行和处理请求的工作人员池。每个工作人员都有自己的全局变量值,它将在多个请求中更新。

如果您使用线程进行并发,则全局变量将在所有工作人员之间共享,因为它们将位于同一进程中。

关于ruby-on-rails - Rails 全局变量 : is global variable share between http requests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411384/

相关文章:

ruby-on-rails - 我可以将乘客支持添加到现有的 Nginx 而不是重建一个吗?

ruby-on-rails - Rails 显示错误 "uninitialized constant URI::Generic"

ruby-on-rails - Rails + Passenger : Invalid Route name, 已在使用中

ruby-on-rails - 如何使用 CloudFront、Rails、Jammit 让 CDN 为暂存和生产环境工作

regex - nginx:站点重定向期间出现 502 Bad gateway 错误

ruby-on-rails - 通过 rails 中的链接获取标题和内容

html - 如何通过 nginx 将 rtsp 网络摄像机流式传输到网站

nginx - 上传大文件 Flask Nginx Uwsgi

ruby-on-rails - Devise Invitable 根据用户类型发送不同的电子邮件

ruby-on-rails - 为什么设计用户实例没有被 stub ?