2015-10-29 140 views
1

我已经在我的应用程序控制器中声明了全局变量,并且我正在每个http请求上增加它。Rails全局变量:是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; 
    # } 
    #} 

} 

感谢,

+0

这是一个接近空的主nginx配置。实际的乘客配置在哪里?我想,我们需要一个'/etc/nginx/conf.d/ *。conf' –

+0

请参阅更新配置文件。 – user2274074

+0

'worker_processes 1' –

回答

1

乘客为每一个HTTP请求

创建过程中这将是非常效率低下。乘客不这样做。相反,它会创建一个保持运行并处理请求的工作者池。每个工作人员都将拥有自己的全局变量值,它将在多个请求中更新。

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

+0

这意味着如果有两个或更多的工人,那么我可能会在http请求 – user2274074

+0

@ user2274074上得到不同的全局变量值:是的,这就是我所期望的。 –

+0

http://stackoverflow.com/a/2972610/2274074这个答案表示乘客共享全局变量。 – user2274074