Rails全局变量:是http请求之间的全局变量共享吗?

时间:2015-10-29 10:13:46

标签: ruby-on-rails nginx passenger

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

我已经测试了这个功能,发现全局变量在每个http请求都是有罪的。

我使用nginx作为Web服务器,乘客作为应用程序服务器。我已经阅读了很多关于乘客的文章,并且知道乘客为每个http请求创建进程,并且每个进程都有自己的全局变量,因此全局变量不能跨每个http请求共享。每个http请求都有自己的全局变量副本。这是真的吗?如果是那么为什么在我的情况下全局变量在每个http请求上递增。

**************************** nginx conf ***************** *****************

#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;
    #    }
    #}

}

谢谢,

1 个答案:

答案 0 :(得分:1)

  

乘客为每个http请求创建流程

这将是非常低效的。乘客不这样做。相反,它创建了一个工作池,可以继续运行和处理请求。每个worker都有自己的全局var值,它将在多个请求中更新。

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

相关问题