Docs / Hosting / Nginx Config to Serve Cache without PHP

Nginx Config to Serve Cache without PHP

Add Nginx configuration is completely optional and may see performance impact only in high traffic sites

FlyingPress will work out of the box with any web server. In Nginx, cached files will be served via PHP.

This can slightly degrade the performance if there are low server resources or a lot of traffic. By configuring Nginx to server cache, no requests will hit PHP or WordPress!

Add Nginx Configuration

Inside your Nginx configuration directory (usually /etc/nginx/), create a new file named flying-press.conf with the following contents:

location ~* \.html$ {
  charset UTF-8;
  add_header x-flying-press-cache HIT;
  add_header x-flying-press-source Nginx;
  add_header cache-control "no-cache, must-revalidate, max-age=0";
}

set $flying_press_cache 1;
set $flying_press_url "/wp-content/cache/flying-press/$http_host/$request_uri/index.html";
set $flying_press_file "$document_root/wp-content/cache/flying-press/$http_host/$request_uri/index.html";

if ($request_method = POST) {
  set $flying_press_cache 0;
}

if ($is_args) {
  set $flying_press_cache 0;
}

if ($http_cookie ~* "(wp\-postpass|wordpress_logged_in|comment_author|woocommerce_cart_hash|edd_items_in_cart)") {
  set $flying_press_cache 0;
}

if (!-f "$flying_press_file") {
  set $flying_press_cache 0;
}

if ($flying_press_cache = 1) {
  rewrite .* "$flying_press_url" last;
}

Now include that file inside the server block of your main Nginx config:

server {
  ...

  # FlyingPress configuration
  include flying-press.conf;

  ...
}

Verify Configuration

Before you reload your configuration, make sure you test it: nginx -t

Once your test is successful, you must reload the configuration: service nginx reload

You’ll also see a response header x-flying-press-source: Nginx indicating that it’s served via Nginx. Otherwise, it will be PHP.