Rocket-Nginx Documentation

repository·master·Indexed 20 days ago

https://github.com/satellitewp/rocket-nginx

An Nginx configuration layer for the WP Rocket WordPress plugin that enables high-performance static file serving by bypassing PHP for cached content. It includes a configuration parser for .ini files, support for custom configuration includes (v3.0+), debug headers for cache hit/miss analysis, and guidelines for managing system-level cron jobs and SSL/HTTPS support.

Tokens
1.7K
Snippets
4
Records
12
Agent score
20%

What's inside Rocket-Nginx

  1. What is Rocket-Nginx?

    master
    Rocket-Nginx is an Nginx configuration designed for the WP Rocket WordPress cache plugin. It optimizes performance by allowing Nginx to serve previously cached files directly, bypassing WordPress and PHP execution. It also implements headers to leverage browser caching for CSS, JS, and media files.
  2. New features in Rocket-Nginx v3.x

    master

    Version 3.x introduced several significant changes and features:

    • Query String Support: You can now specify query strings to cache via the .ini file. Refer to WP Rocket's Cache query strings documentation for configuration details.
    • Query String Ignoring: You can specify query strings to ignore via the .ini file. Refer to WP Rocket's Cache query strings to ignore documentation.
    • Custom Expirations: Support for custom expiration settings for CSS, JS, and media files.
    • Section Includes: Custom configurations can now be included in every section.
    • Removed Features:
      • The default HSTS value has been removed.
      • Adding headers directly from the config file has been removed (use the hook principle instead).
  3. Use custom configuration includes in Rocket-Nginx

    master

    Starting with version 3.0, you can inject custom Nginx configuration snippets into specific stages of the request lifecycle by placing files in the rocket-nginx/conf.d/default/ directory using specific filename patterns:

    Filename PatternInjection Point
    start.*.confBefore Rocket-Nginx starts
    global.*.confGlobally in every section
    http.*.confIn the HTTP section
    preprocess.*.confPreprocess (after all variables are set)
    css.*.confIn the CSS section
    js.*.confIn the JS section
    media.*.confIn the Media section
  4. Upgrade from Rocket-Nginx v1 or v2 to v3

    master

    Version 3.x is not backward-compatible with versions 1 or 2. To upgrade:

    1. Save your existing configuration as a backup.
    2. Start with a fresh configuration to ensure all new features and structural changes are correctly implemented.
    3. Review the new configuration options, as many behaviors have changed.
  5. Debug Rocket-Nginx cache hits and misses

    master

    To inspect how Nginx is serving files, enable debug mode in rocket-nginx.ini:

    debug = true

    Response Headers

    All requests include the following header:

    • X-Rocket-Nginx-Serving-Static: Indicates if the file was served directly (HIT, MISS, or BYPASS).

    When debug = true, the following additional headers are added:

    • X-Rocket-Nginx-Reason: If the status is not HIT, explains why WordPress was called (e.g., Post request, Arguments found, Maintenance mode, Cookie, Specific mobile cache activated, or File not cached). If HIT, shows the URL used.
    • X-Rocket-Nginx-File: If HIT, shows the absolute path to the file on disk.
    • X-Rocket-Nginx-Device: Indicates the detected device (desktop or mobile).
    debug = true
  6. Install Rocket-Nginx

    master

    To install Rocket-Nginx, clone the repository into your Nginx configuration directory (typically /etc/nginx/).

    cd /etc/nginx
    git clone https://github.com/satellitewp/rocket-nginx.git

    Since version 2.0, the configuration must be generated using the included parser:

    cd rocket-nginx
    cp rocket-nginx.ini.disabled rocket-nginx.ini
    php rocket-parser.php

    This generates a default.conf file. You must then include this file in your Nginx server block:

    server {
      ...
      # Rocket-Nginx configuration
      include rocket-nginx/conf.d/default.conf;
      ...
    }

    Finally, test and reload Nginx:

    nginx -t
    service nginx reload
    cd /etc/nginx
    git clone https://github.com/satellitewp/rocket-nginx.git
  7. Disable WordPress Cron and set up a real Cron job

    master

    Because Rocket-Nginx serves cached files without executing PHP, standard WP-Cron jobs (which rely on site visits) may not trigger. It is strongly recommended to disable WordPress's internal cron and use a system-level cron job instead.

    1. Add this to your wp-config.php:
    define( 'DISABLE_WP_CRON', true );
    1. Set up a manual cron job (e.g., every 15 minutes) using one of these methods:

    Using wget:

    */15 * * * * wget -q -O - http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null

    Using curl:

    */15 * * * * curl http://www.website.com/wp-cron.php?doing_wp_cron &>/dev/null

    Using PHP CLI:

    */15 * * * * cd /home/user/public_html; php wp-cron.php &>/dev/null
    define( 'DISABLE_WP_CRON', true );
  8. Configure Rocket-Nginx via .ini file

    master

    Rocket-Nginx is configured using a rocket-nginx.ini file. You can add site-specific configurations by creating a new section. For example, to add a custom cookie that invalidates the cache:

    [example.com : default]
    cookie_invalidate[] = "my_custom_cookie"

    Important: Every time you modify the .ini file, you must regenerate the Nginx configuration and reload the service:

    1. Regenerate:
      php rocket-parser.php
    2. Test Nginx config:
      nginx -t
    3. Reload Nginx:
      service nginx reload
    [example.com : default]
    cookie_invalidate[] = "my_custom_cookie"
  9. Resolve lost Nginx headers using the hook principle

    master

    If you find that add_header directives are missing after enabling Rocket-Nginx, this is due to how Nginx manages headers. To fix this, use the 'hook' principle:

    1. Define your custom headers in a separate configuration file.
    2. Include that file within your Rocket-Nginx configuration.
    3. If the header file is located outside the expected directory, you can use a symbolic link to ensure it is accessible.

    Refer to the Configuration section of the documentation to identify the correct placement for these headers.

  10. Known limitations of Rocket-Nginx

    master

    Encoded Slugs

    Encoded slugs (e.g., non-Latin characters) may not be served correctly due to case-sensitivity differences between browsers and Nginx. Note: Version 3.1.0+ provides a preprocess.*.conf include to help mitigate this by forcing $rocket_uri_path to lowercase.

    WebP Compatibility

    Rocket-Nginx cannot serve WebP files generated by the WP Rocket WebP Compatibility feature. If this feature is enabled, Rocket-Nginx will bypass the cache and let WordPress/WP Rocket handle the request to ensure the correct image format is served.