Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx caching and custom header

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Proxy_cache:nginx caching module, which is used to set the caching function of nginx

Nginx caching principle:

Nginx cache files are stored according to key:value. Key is stored directly in memory. Value points to the local file system directory, which supports up to three levels of subdirectories.

Proxy_cache_path # is used to set the relevant properties of the nginx cache and can only appear in the http segment

Example: proxy_cache_path / nginx/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g

/ nginx/cache1 # cache directory, owner and group must be users running nginx

Levels=2:3 # sets the number of directories. 2 means that the name of a level 1 subdirectory can only be represented by 2 characters, and 3 means that the name of a level 2 subdirectory can only be represented by 3 characters.

Keys_zone=cache1:100m # cache1 indicates the cache name 100m: indicates the maximum size of the key in memory

Inactive=1d # if no one accesses the cache for a day, then the cache will be cleaned

Max_size=10g # maximum cache size for local file system storage

After configuring proxy_cache_path, call where the cache is needed.

Call proxy_cache_path

Proxy_cache cache1; # calls the cache configuration named cache1, and proxy_cache can appear in the http,server,location segment

Example of nginx cache configuration:

Http {

Include mime.types

Default_type application/octet-stream

Sendfile on

Keepalive_timeout 65

Proxy_cache_path / nginx/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g

# cache1 cache definition

Server {

Listen 80

Server_name localhost

Location / {

Proxy_cache cache1; # calls cache1 cache

Proxy_cache_valid 200 1D; # pages with status code 200 are cached for one day

Proxy_cache_valid 301 302 10m; # pages with status code 301 302 are cached for 10 minutes

Proxy_cache_valid any 1m; # all remaining pages are cached for 1 minute

Proxy_pass http://192.168.0.50/;

Index index.html index.htm

}

}

}

Verify:

1. Client accesses nginx server

2. Check whether the cache directory is generated under the / nginx/cache1 directory

3. Change the page information of the backend server, and use the client access to check whether the page information has changed (if there is no change, the cache will be built successfully)

Proxy_set_header:http header Settin

Sometimes the back-end server of the nginx proxy needs to record information such as the ip address of the client, but because of the nginx proxy, the ip recorded by the back-end server is actually the ip of nginx. If you want the back-end server to be able to obtain the real ip of the client, you need to configure the proxy_set_header instruction.

Proxy_set_header Host $host; # Host custom name, $host storage hostname variable

Proxy_set_header real-ip $remote_addr; # $remote_addr stores variables for client ip

Configuration example:

Nginx server:

Server {

Listen 80

Server_name localhost

Location / {

Proxy_set_header real_ip $remote_addr; # reencapsulates the ip address with the header name real_ip

Proxy_pass http://192.168.0.50/;

Index index.html index.htm

}

}

Backend apache server:

LogFormat "% {real_ip} I% h% l% u% t\" r\ "% > s% b\"% {Referer} I\ "\"% {User-Agent} I\ "combined

# adding% {real_ip} iMagneRealizable IP to LogFormat is the header name of the package

Verify:

1. Check the access_log log of the backend server to see whether the recorded ip is the real ip of the client.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report