How to set up Nginx as a caching reverse proxy?

Better Stack Team
Updated on November 23, 2023

Setting up Nginx as a caching reverse proxy can significantly improve web server performance by serving cached content to users, reducing the load on the upstream servers. Here's a basic guideline on how to configure Nginx as a caching reverse proxy:

Step 1: Install Nginx

Make sure Nginx is installed on your server. You can install it using your package manager. For instance, on Ubuntu, you can use:

sudo apt update
sudo apt install nginx

Step 2: Configure Nginx as a Caching Reverse Proxy

  1. Edit Nginx configuration:

    sudo nano /etc/nginx/nginx.conf
  2. Inside the configuration file, add or modify the following blocks:

    http {
        # Define caching path and zone
        proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;
        # Define proxy settings
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m; # Cache successful responses for 10 minutes
        proxy_cache_valid 404      1m; # Cache 404 errors for 1 minute
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        server {
            listen 80;
            # Proxy requests to the upstream server
            location / {
                proxy_pass http://your_upstream_server;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_cache_bypass $http_upgrade;
                # Enable caching for this location
                proxy_cache my_cache;
                proxy_cache_key $scheme$proxy_host$request_uri$is_args$args;

Step 3: Reload Nginx Configuration

After making changes, save the configuration file and test for syntax errors:

sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 4: Testing and Monitoring

  • Test your setup by accessing your website and checking the HTTP response headers to verify caching is working. Look for X-Cache headers to see if content is served from the cache or not.
  • Monitor the cache status and performance using Nginx's status module or other monitoring tools.

Additional Considerations:

  • Adjust caching directives based on your specific requirements.
  • Tailor cache settings like proxy_cache_valid and proxy_cache_use_stale according to your application's needs.
  • Ensure you have enough disk space allocated for caching (max_size in proxy_cache_path).
  • Regularly monitor and adjust the caching configuration for optimal performance.

This configuration serves as a starting point. You might need to tweak it based on your specific use case and requirements.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github