How to achieve local cache of static pages with Nginx

        When talking about how to speed up large-scale site speed, cache must bear the brunt of ways, such as adding CDN, Redis, separation from the main way. In general large-scale systems, customers will use static and dynamic separation approach for local caching to accelerate. Today we talk about customers often using Nginx configuration for web acceleration solution.

One, first of all talk about the reasons for the accelerated

Users access the site through a reverse proxy server, the reverse proxy server initiates the access request and returns the results to the client according to the real server load balancing policy. In return to the customer at the same time, the static content is cached locally, when other client initiated the same needs, the locally cached results are returned to the customer, we will not continue to pass the request to the real server.

 

Second, the reverse proxy server Nginx configuration approach

1, LVS load balancing configuration approach discussed in the previous article shall not repeat.

2, configured in the reverse proxy server ngin.conf

[root@ecs-maxing1 7d]# vim /usr/local/webserver/nginx/conf/nginx.conf

Add the following code in the http section

proxy_cache_path /data/nginx/cache_item levels=1:2  keys_zone=cache_item:10m max_size=1000g inactive=7d;

Add the following code location / {in

proxy_cache cache_item;

proxy_cache_valid 200 206 304 301 302 10d;

proxy_cache_key $uri;

proxy_set_header Host $host:$server_port;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

3, check the configuration is successful

[root@ecs-maxing1 7d]#  /usr/local/webserver/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful

 

Third, the reverse proxy server Nginx cache directory new

mkdir /data/nginx/cache_item

 

Fourth, the validation results

1, restart nginx

./nginx -s stop

./nginx

2, access from the client to conduct business

I found that no matter how I refresh, never appeared webServer2, should be cached into effect.

3. Verify the real server shut down service would not affect business

In webServer1, webServer2 close Nginx Service

./nginx - stop

After closing the business still will not be interrupted.

 

    4. Is there a file exists in the cache directory authentication server

We found cache file already exists.

I hope this article can help you.

More real-time updates, visit public number.    

 

Click here to get the highest ¥ 1888 Ali cloud offering universal vouchers

Guess you like

Origin blog.csdn.net/qq_29718979/article/details/90544517