Home  >  Article  >  Backend Development  >  Nginx series (fourteen nginx cache)

Nginx series (fourteen nginx cache)

WBOY
WBOYOriginal
2016-08-08 09:23:431300browse

1. Proxy module caching

1. Configuration file

<code><span>#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区</span>
proxy_temp_path /web/tmp/nginx;
<span>#设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为1GB。</span>
proxy_cache_path /web/cache/nginx levels=<span>1</span>:<span>2</span> keys_z>100m inactive=<span>1</span>d max_size=<span>1</span>g;
</code>
<code>location / {
            <span>#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。</span>
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_cache cache_one;
            <span>#对不同的HTTP状态码设置不同的缓存时间</span>
            proxy_cache_valid <span>200</span><span>304</span><span>1</span>h;
            <span>#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内</span>
            proxy_cache_key <span>$host</span><span>$uri</span><span>$is_args</span><span>$args</span>;
            proxy_pass http://webserver;
            proxy_redirect off;
            proxy_<span>set</span>_header Host <span>$host</span>;
            proxy_<span>set</span>_header X-Real-IP <span>$remote_addr</span>;
            proxy_<span>set</span>_header X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>;
        }</code>

The caching function of the reverse proxy is a redirection, not a function of generating cached static files based on the URL

2. Fastcgi module caching

1. Configuration file

<code>fastcgi_temp_path /web/tmp/fastcgi;
<span>#设置fastcgi缓存路径 levels代表目录层级,1:2会生成16*256,2:2会生成256*256 keys_zone代表缓冲区名称 inactive代表过期时间 max_size代表最多用多少磁盘空间</span>
fastcgi_cache_path /web/cache/fastcgi levels=<span>1</span>:<span>2</span> keys_z>100m inactive=<span>1</span>d max_size=<span>1</span>g;
</code>
rrree

Attention:
1. If the fastcgi response header contains Expires Cache-Control Set-Cookie, fastcgi_cache will not work, so you need to add the fastcgi_ignore_headers configuration item.

2. Add the header information X-Cache-CFC to test whether the cache is working. $upstream_cache_status contains the following statuses:
MISS miss, the request is sent to the backend
HIT cache hit
EXPIRED The cache has expired and the request is sent to the backend
UPDATING Updating cache, old response will be used
STALE backend will get expired response
BYPASS cache is bypassed

Three. Reference

http://www.qttc.net/201307355.html
https://serversforhackers.com/nginx-caching/
http://www.ha97.com/5194.html
http://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter/

The above has introduced the Nginx series (fourteen nginx cache), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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