[PATCH] examples/nginx.conf: use try_files directive

Eric Wong normalperson at yhbt.net
Tue Jan 25 17:09:50 EST 2011


I'm lazy but I finally updated from using nginx 0.6.x myself for
http://unicorn.bogomips.org/ and http://bogomips.org/unicorn.git
to 0.8.54...

>From 09afcf2ce9fc89d77b6b282bbf00a78c73741a4b Mon Sep 17 00:00:00 2001
From: Eric Wong <normalperson at yhbt.net>
Date: Tue, 25 Jan 2011 13:58:29 -0800
Subject: [PATCH] examples/nginx.conf: use try_files directive

This feature is in nginx 0.7.x and 0.8.x and optimized
better than the "if" directive in nginx.conf

ref: http://wiki.nginx.org/Pitfalls
ref: http://wiki.nginx.org/IfIsEvil
---
 examples/nginx.conf |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/examples/nginx.conf b/examples/nginx.conf
index 70d1851..52ec245 100644
--- a/examples/nginx.conf
+++ b/examples/nginx.conf
@@ -98,7 +98,16 @@ http {
     # path for static files
     root /path/to/app/current/public;
 
-    location / {
+    # Prefer to serve static files directly from nginx to avoid unnecessary
+    # data copies from the application server.
+    #
+    # try_files directive appeared in in nginx 0.7.27 and has stabilized
+    # over time.  Older versions of nginx (e.g. 0.6.x) requires
+    # "if (!-f $request_filename)" which was less efficient:
+    # http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
+    try_files $uri/index.html $uri.html $uri @app;
+
+    location @app {
       # an HTTP header important enough to have its own Wikipedia entry:
       #   http://en.wikipedia.org/wiki/X-Forwarded-For
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -122,12 +131,7 @@ http {
       # clients, really.
       # proxy_buffering off;
 
-      # Try to serve static files from nginx, no point in making an
-      # *application* server like Unicorn/Rainbows! serve static files.
-      if (!-f $request_filename) {
-        proxy_pass http://app_server;
-        break;
-      }
+      proxy_pass http://app_server;
     }
 
     # Rails error pages
-- 
Eric Wong


More information about the mongrel-unicorn mailing list