Home Operation and Maintenance Nginx Nginx cache cleaning configuration to keep website content updated

Nginx cache cleaning configuration to keep website content updated

Jul 05, 2023 pm 10:28 PM
clean up nginx cache renew.

Nginx cache cleaning configuration to keep website content updated

When building a website, we often use Nginx as a reverse proxy server to accelerate website access and cache static files. However, when we update website content, we need to clear Nginx's cache in time to keep users accessing the latest content. This article will introduce how to configure Nginx cache cleaning to keep website content updated.

1. Configure the cache path

First, we need to configure the cache path of Nginx. Open the Nginx configuration file, usually /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf, and find http section, add the following configuration:

http {
    ...
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        ...
    }
    ...
}
Copy after login

The above configuration stores cache files in the /var/cache/nginx directory, and sets the cache size to 10GB and the cache expiration time to 60 minutes. Need to be adjusted according to actual situation.

2. Configure the cache cleaning interface

Next, we need to configure an interface to clean the Nginx cache.

In the Nginx configuration file, find the place where the cache needs to be cleared, such as a POST request:

http {
    ...
    server {
        ...
        location /update {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;

            # 清理缓存
            proxy_cache_purge my_cache GET POST;
        }
        ...
    }
    ...
}
Copy after login

In the above configuration, location /update is used to match the need to update URL path, such as /update. The proxy_cache_purge directive is used to clear the cache, and the parameter GET POST means to clear the cache of GET and POST requests at the same time. It can be adjusted according to the actual situation.

3. Install the Purge module

The default installation of Nginx does not include the proxy_cache_purge module, we need to install it manually.

Use the following command to install Nginx’s module management tool:

yum install -y yum-utils
Copy after login

Use the following command to add Nginx’s extension source:

yum-config-manager --add-repo=https://nginx.org/packages/centos/7/x86_64/
Copy after login

Finally, use the following command to install Nginx’s nginx-module-cache-purgeModule:

yum install -y nginx-module-cache-purge
Copy after login

4. Restart the Nginx service

After completing the above configuration, you need to restart the Nginx service to make the configuration take effect. Use the following command to restart Nginx:

systemctl restart nginx
Copy after login

5. How to clear the cache

Now, we can clean the Nginx cache by accessing the cache clearing interface. For example, if we want to clear the cache in the /update path, we can use the following command:

curl -X POST http://yourdomain.com/update
Copy after login

This will trigger Nginx to clear the cache in the /update path.

6. Automatically clear the cache

In order to ensure that the website content is updated, we can automatically maintain the consistency between the cache in Nginx and the actual website content by regularly cleaning the cache.

We can use scheduled task tools (such as Cron) to regularly execute cache clearing commands. For example, to set the cache to be cleared every day at 4 a.m., you can use the following command to edit the scheduled task:

crontab -e
Copy after login

Then add the following line of command:

0 4 * * * curl -X POST http://yourdomain.com/update >> /tmp/nginx_cache_clear.log 2>&1
Copy after login

Save and exit the editor, so that every day at 4 a.m. An operation to clear the cache will be triggered, and the execution results will be saved in the /tmp/nginx_cache_clear.log file.

Summary

Through the above configuration, we can automatically clean up the Nginx cache and keep the website content updated. At the same time, we can also flexibly adjust cache paths and cleaning methods as needed to adapt to different website needs.

The above is the detailed content of Nginx cache cleaning configuration to keep website content updated. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to effectively clean junk and cache in win7 system How to effectively clean junk and cache in win7 system Dec 27, 2023 pm 07:53 PM

When we use Win7 computers on a daily basis, a bunch of system cache garbage often accumulates over time, which requires us to clean it manually. So how to clean the garbage and cache of each system disk? Let’s take a look. How to clean up junk and cache in Windows 7 1. Find the disk you want to clean, right-click and select Properties. 2. The computer will pop up the detailed properties of the hard disk. Here we continue to click "Disk Cleanup" 3. After clicking, we select the option you want to clean here and click Clean.

Where to check win11 disk cleanup Where to check win11 disk cleanup Jun 29, 2023 pm 09:25 PM

Where can I check disk cleanup in win11? As the system is used for a long time, there will often be more than ten gigabytes or more useless junk files in the c drive or other computer disks. At this time, using the disk cleanup function that comes with win11 can help us quickly clean up these useless files. However, many users do not know where the disk cleanup function of win11 is and how to operate it. Today, the editor will bring you the steps to check the junk cleaning function that comes with Win 11, and let me explain it to you. Steps to check win11’s built-in junk cleaning function 1. First, press the [Win] key on the keyboard, or click [Start Menu] on the taskbar, and then select [Settings] under pinned applications; 2. The current path is: System >storage,

Are administrator rights required: win10 required to clean C drive Are administrator rights required: win10 required to clean C drive Jan 12, 2024 pm 03:45 PM

Many users have recently encountered the problem that when cleaning files in the C drive, a pop-up window will appear, indicating that permissions need to be granted to delete them. Click to grant permissions, but they still cannot be deleted. We only need to make a slight modification in the group policy. It can be solved. Does cleaning up the C drive in Win10 require administrator rights? Answer: Cleaning up the C drive in Win10 requires administrator rights. 1. Enter the command using the win+r shortcut key in the pop-up window, and press the Enter key. 2. Click to open----. 3. Then, on this page, change it to. 4. Similarly, set the options. 5. Finally, save the settings, close all pages, restart the computer, and then clean the C drive directly.

Nginx cache cleaning configuration to keep website content updated Nginx cache cleaning configuration to keep website content updated Jul 05, 2023 pm 10:28 PM

Nginx cache cleaning configuration to keep website content updated When building a website, we often use Nginx as a reverse proxy server to speed up website access and cache static files. However, when we update website content, we need to clear Nginx's cache in time to keep users accessing the latest content. This article will introduce how to configure Nginx cache cleaning to keep website content updated. 1. Configure the cache path. First, we need to configure the cache path of Nginx. Open Nginx configuration

How to clear cache data of QQ Browser How to clear cache data of QQ Browser Jan 29, 2024 pm 06:03 PM

How to clear QQ browser cache data? QQ Browser is a search software with a large number of users. Its excellent speed and rich functions make many users use this software regularly. We all know that after using software for a long time, it will leave a lot of cache files and other junk information. If not cleaned up in time, it will slow down the browser's response speed. At this time, we need to clean it regularly and in time. After clearing the cache, it will be more convenient to use and the response speed will be better. Let’s take a look at how to clear the cache when using QQ Browser! QQ Browser cache data cleaning method and steps are introduced. Step 1: Open QQ Browser and click the "Menu" bar in the upper right corner of the main page. Step 2: In the drop-down menu bar option, click Play

Linux server log is too large, how to solve it? Linux server log is too large, how to solve it? Jun 29, 2023 pm 11:09 PM

Common problems of too-large log files on Linux servers and their solutions. With the rapid development of the Internet and the widespread use of servers, it has become a common problem that server log files are getting larger and larger. A large amount of log data not only takes up disk space, but may also affect the performance and operational stability of the server. This article will discuss the common problem of too large log files on Linux servers and provide some solutions. 1. Common log files on Linux servers. Common log files include system logs, application logs, We

How to clean up win11 hibernation files How to clean up win11 hibernation files Jun 29, 2023 am 11:35 AM

How to clean win11 hibernation files? When many friends clean up the disk of their win11 computer, they find that new junk files are generated every time they are restarted. This is the computer's hibernation file. These hibernation files are automatically generated by the system. After we clean up these hibernation files, we can reduce a very large burden on the disk. However, many friends do not know how to clean up hibernation files. Below, the editor will bring you win11 hibernation. File cleaning tutorial. Win11 hibernation file cleaning tutorial 1. Find the CMD program on the C drive. The path is as follows: C: WindowsSystem32 and then right-click: Run as administrator. 2. Enter the command: powercfg-hoff and press Enter. You will see the following:

Introduction to the tutorial of cleaning and deleting hiberfil files in win10 Introduction to the tutorial of cleaning and deleting hiberfil files in win10 Mar 28, 2024 pm 03:16 PM

Many win10 users can see the hiberfil.sys file in the C drive, and this file often occupies a large amount of the system's C drive. Therefore, many users who do not have enough C drive want to delete this file. Please see the introduction below for details. . 1. View the hibernation file and remove the check mark in front of the following mark in the pop-up window. 2. Check the c drive again and find several large files, among which hiberfil.sys is the hibernation file. Open the cmd command window here. Some computers may not have administrator rights. You can find the cmd command directly in the system directory and right-click to execute it as an administrator. 3. Entering powercfg-hoff in the command window will automatically turn off the hibernation function, and the hibernation file will also automatically

See all articles