Welcome! Log In Create A New Profile


Serving static files statically with GET, everything else to backend

February 13, 2014 03:57PM
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 formatting):

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

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

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

Subject Author Posted

Serving static files statically with GET, everything else to backend

michelem February 13, 2014 03:57PM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 333
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready