Serving static files statically with GET, everything else to backend

michelem nginx-forum at
Thu Feb 13 20:57:03 UTC 2014

Hello folks,

Maybe this will save some time to someone.

I have a setup where I serve a web application as follows:

* server A with nginx handles directly as much static content as possible
* only requests for URLs requesting dynamic processing go to server B
hosting the application server

This improves performance and keeps the presentational content online if the
application server fails.

nginx' try_files takes you 95% there: just put on A a filesystem hierarchy
containing the static contents.
The missing 5% is how to serve pages containing forms posting to themselves.
This means, the same
URL gets served statically upon GETs or HEADs, and dynamically otherwise.

Here's the gist of a working setup for this (sorry if the forum messes up

server {
   root /var/www/;      # put files for static content here

    location / {
        error_page 405 = @appsrv;
        # remove if() and @appsrv will get as $uri the last try of
        if ($request_method != GET) { return 405; }
        try_files $uri $uri/ $uri/index.html @appsrv;

    # pass everything to FastCGI
    location @appsrv { fastcgi_pass; }


Posted at Nginx Forum:,247532,247532#msg-247532

More information about the nginx mailing list