Agile web-serving with nginx
Now, everytime I think of nginx, I’ll think of agile. Period.
This tiny web server is not only tiny in its implementation, it’s also tiny in the sense of being simple.
Just now I had to setup a Rails app running in a subdomain locally, because first, I was mapping my IP to a dyndns domain in order to test a google gadget, and second, the gadgets.io.makeRequest doesn’t support a URL param with a port number (probably a bug in the iGoogle OpenSocial implemention, but anyway, not the point here), what I did was to add the following to the already configured virtualhost on my nginx.conf:
server {
listen 80
server_name localhost
location ~ ^/railssite/(.*)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-FORWARDED_PROTO https;
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass http://localhost:3000/$1;
}
Restarted nginx and… had the rails site there. I have an issue though, since all resources are mapped to a relative URL to the host and the host doesn’t include /railssite, all of them fail to load (css, js, images), which is not a problem in this specific case, since I only need to access an action that returns some XML to the gadget (But if you know a way to run an app in a path and still load resources without modifying the app itself, please, share!).
Now, I’m sure it would be easy to do that with Apache, I know because I’ve done before. However, what amazed me was the “principle of least surprise” — I didn’t really look into the documentation, just used some common-sense based on previous skills and it worked.
I just like nginx a tad more
