search
HomeComputer TutorialsComputer Knowledgenginx reverse proxy caching tutorial.

nginx reverse proxy caching tutorial.

Feb 18, 2024 pm 04:48 PM
nginxcachesudo

nginx reverse proxy caching tutorial.

The following is a tutorial on nginx reverse proxy cache:

  1. Install nginx:

    sudo apt update
    sudo apt install nginx
  2. Configure reverse proxy:

    Open nginx configuration file:

    sudo nano /etc/nginx/nginx.conf
  3. Add the following configuration in the
    http block to enable caching:

    http {
        ...
        proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
        proxy_cache_key "$scheme$request_method$host$request_uri";
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        ...
    }
    • proxy_cache_path: Specify the cache path and related parameters.
    • proxy_cache_key: Define the format of the cache key.
    • proxy_cache_valid: Set response code and cache time.
  4. Configure the reverse proxy server:

    Add the following configuration in the
    server block:

    server {
        ...
        location / {
            proxy_pass 
            proxy_set_header Host $host;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        }
        ...
    }
    • proxy_pass: Specify the address of the backend server.
    • proxy_set_header: Set request header information.
    • proxy_cache: Specify the cache area used.
    • proxy_cache_valid: Set response code and cache time.
    • proxy_cache_use_stale: Specifies that stale responses are allowed when updating the cache.
  5. Save and close the configuration file.
  6. Check whether nginx configuration is correct:

    sudo nginx -t
  7. Reload nginx configuration:

    sudo systemctl reload nginx

Now, nginx has configured reverse proxy cache. It will cache the response of the backend server and serve the cached response directly on the next request, reducing the load on the backend server and improving performance.

Please adjust the cache configuration and proxy server address according to your actual needs. Hope this tutorial is helpful!

The above is the detailed content of nginx reverse proxy caching tutorial.. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
How to Fix LSA Database Contains an Internal Inconsistency?How to Fix LSA Database Contains an Internal Inconsistency?May 07, 2025 am 12:50 AM

Is your network drive not accessible? This error occurs might because the LSA database contains an internal inconsistency. Do you have any solutions to get rid of it from your device? Get solutions from this post on php.cn Website now!

192.168.0.2 Admin Login & Change Password – Must-Know Things192.168.0.2 Admin Login & Change Password – Must-Know ThingsMay 07, 2025 am 12:49 AM

What does 192.168.0.2 stand for? If you want to configure the router, how to log in to the admin panel? Besides, how to change the default Wi-Fi password? To find details about this default router IP address, refer to this post from php.cn now.

192.168.11.1 IP Address – How to Log in and Change Password? - MiniTool192.168.11.1 IP Address – How to Log in and Change Password? - MiniToolMay 07, 2025 am 12:48 AM

Every device on the Internet needs a unique identifier called an IP address. This post from php.cn is about the 192.168.11.1 IP address. You can know what it is, how to log into it, and how to change the password. Keep on your reading.

Top-Secret Tips for Outlook Error There Is No Default Mail ClientTop-Secret Tips for Outlook Error There Is No Default Mail ClientMay 07, 2025 am 12:47 AM

What is the “There is no default mail client” error? What methods can be used when this error occurs? This article from php.cn provides you with several feasible and efficient ways worth trying.

What If You Can't Boot Windows 11 After Enabling Hyper-V?What If You Can't Boot Windows 11 After Enabling Hyper-V?May 07, 2025 am 12:46 AM

Why won’t Windows 11 boot after enabling Hyper-V? How to fix this issue? If you can’t boot Windows 11 after enabling Hyper-V, you come to the right place. From this post on php.cn, you can find some possible reasons and workarounds to solve it.

Fixed: A USB Stick Not Transferring FilesFixed: A USB Stick Not Transferring FilesMay 07, 2025 am 12:45 AM

I believe that all of you have transferred files from a USB drive to your computer. Have you ever been bothered by a USB stick not transferring files error? Do you know how to fix this error? If not, you can find answers in this php.cn post.

OneDrive Folder Not Showing Thumbnails? See How to Fix in 3 Ways! - MiniToolOneDrive Folder Not Showing Thumbnails? See How to Fix in 3 Ways! - MiniToolMay 07, 2025 am 12:44 AM

If you are using OneDrive in your Windows 11/10, you may run into thumbnails not showing in OneDrive folder. How to fix this annoying issue? Take it easy and multiple solutions will be introduced in this post by php.cn. Let’s go to see what you shoul

How to Check Disk Partitions: Exact Steps in This Pro GuideHow to Check Disk Partitions: Exact Steps in This Pro GuideMay 07, 2025 am 12:43 AM

Don’t know what partitions your disk has? Now you can learn how to check disk partitions in a few easy ways from this post on php.cn. Also, you can know how to recover deleted or lost partitions effectively.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.