Nginx is not only a small and efficient HTTP server, but also an efficient Server Load balancer reverse proxy, using it to receive user requests and distribute them to multiple Mongrel processes can greatly improve the concurrency of Rails Applications. The following describes how to configure Nginx + Mongrel cluster on a server.
To obtain Nginx, let's assume that you have compiled and configured the default compilation parameters. Nginx 0.5.x is used here.
Configure Mongrel cluster
We also need to obtain Mongrel and its Cluster plug-in (to easily start multiple Mongrel processes), and install it through gem as follows:
Gem I-y mongrel mongrel_cluster
Create a configuration file for mongrel_cluster. Enter the Rails application, that is, the root directory of your program (the following assumptions/usr/rails), and run:
Mongrel_rails cluster: configure
Then mongrel_cluster will generate a mongrel_cluster.yml In the config directory. The content is as follows:
---
Log_file: log/mongrel. log
Port: 3000
Pid_file: tmp/pids/mongrel. pid
Servers: 2
You can modify the settings to change the running of mongrel_cluster. Some other parameters are omitted in this example. The specific parameters are described as follows:
Address: Specifies the bound address.
Port: Specifies the port from which the mongrel process running on mongrel_cluster is bound.
Servers: specifies the number of mongrel processes running at the same time. Combined with the port parameter, it indicates that the port from port to port + servers-1 (inclusive) will be used.
Environment: Specifies the configuration environment for running Rails.
User: Specifies the identity of the mongrel process to run.
Group: Specifies the identity of the mongrel process to run.
Cwd: Specifies the root directory for mongrel running.
Log_file: the location of the output logs of each mongrel process. Compared with the cwd directory, the corresponding port number of each process is added before the file extension.
Pid_file: the location of the pid file of each mongrel process. Compared with the cwd directory, the corresponding port number of each process is added before the file extension.
You can modify it based on your actual situation. The following is a complete mongrel_cluster.yml configuration file:
---
User: apache
Cwd:/usr/rails/
Log_file: log/mongrel. log
Port: 3000
Environment: production
Group: apache
Address: 127.0.0.1
Pid_file: tmp/pids/mongrel. pid
Servers: 5
Then you can start mongrel_cluster. The following command controls mongrel_cluster:
Mongrel_rails cluster: start # start
Mongrel_rails cluster: restart # restart
Mongrel_rails cluster: stop # stop
Configure Nginx Server Load balancer reverse proxy
Use the nginx upstream command to configure which servers need Server Load balancer. Here, we can also say that the address and port on which the nginx mongrel_cluster is located should be written in nginx according to the mongrel_cluster configuration above:
...
Http {
...
# Put the upstream segment in the http segment
Upstream mongrel {
Server 127.0.0.1: 3000;
Server 127.0.0.1: 3001;
Server 127.0.0.1: 3002;
Server 127.0.0.1: 3003;
Server 127.0.0.1: 3004;
}
...
}
The mongrel following the upstream command specifies the names of these upstream servers. You can use other names. Each server command specifies a server. The server command also supports other parameters to set the retry times, timeout times, and weights of different servers.
Next, configure which http requests nginx will forward to the mongrel cluster, because nginx processes static files much faster than mongrel, therefore, when the request path does not exist, the request will be forwarded to the mongrel cluster:
Server {
Listen 80;
Server_name example.com;
# Set the root directory of the server to the public directory of the rails application, which stores external static files
Root/usr/rails/public;
Index index.html index.htm;
Location /{
# Retain the IP address and HOST information of the original request during forwarding
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_set_header Host $ http_host;
Proxy_redirect false;
If (-f $ request_filename/index.html ){
Rewrite (. *) $1/index.html break;
}
If (-f then request_filename.html ){
Rewrite (. *) into 1.html break;
}
# When the requested file does not exist, it is forwarded to the mongrel cluster.
If (! -F $ request_filename ){
Proxy_pass http: // mongrel;
Break;
}
}
Error_page 500 502 503 x.html;
Location =/50x.html {
Root html;
}
}
Restart Nginx and the configuration is successful.