Using nginx with Resin
There was a lively discussion a couple of weeks ago on the Resin interest mailing list about why people are using Apache with Resin and whether it’s actually necessary. Of course, we recommend that our users use Resin as both the HTTP server and the app server, but sometimes that’s not possible because of other legacy apps, non-Java apps on the same host, or simply internal politics. The trend in the non-Java world lately has been to move away from Apache to nginx because of its simple configuration and raw speed. For backend applications, it uses simple proxying or FastCGI instead of the mod_* model of Apache. I decided to check out nginx this morning and see how to hook up Resin. Turns out that it’s pretty easy!
What I tried was to run WordPress on Quercus and Resin, with nginx on the frontend. The setup for Resin is essentially the default included configuration with Resin listening on port 8080. I set up nginx to listen on port 80 and proxy all the connections to Resin at 8080. nginx serves specified static files while Resin does the rest. Here’s the nginx configuration:
user nobody nogroup;
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /var/www/hosts/default/webapps/ROOT;
}
}
}
I did a quick round of load testing by using ApacheBench (ab), doing 10000 requests with concurrency of 4. nginx appears to be quite efficient, introducing only about 4% overhead on top of Resin. If you need to run other non-Java applications side-by-side, nginx might be a better choice for you than Apache. I tried varying the number of worker_processes from 1 to 4 and found no significant difference in performance for my little test, but it may matter in production.
The advantage of using mod_caucho is that it provides load balancing based on number of connections, grabs the cluster configuration from a Resin instance in the backend, and provides failover. Nginx doesn’t have automatic configuration from Resin, but it appears that there are efforts to provide failover and connection-based load balancing.
Does anyone else have experience using nginx with Resin? If so, please comment below and let us know if you have any tips or best practices. I may be including some instructions in the next revision of the Resin documentation that I’m working on, so any tips would be useful.
