Install the cache clearing module for Nginx in CentOS to optimize nginx server performance

Source: Internet
Author: User

Install the cache clearing module for Nginx in CentOS to optimize nginx server performance

Ngx_cache_purge is the third-party module of nginx. It is used to clear the FastCGI, proxy, SCGI and uWSGI cache. nginx is installed by default with the reverse proxy function, but you want to use it better, the ngx_cache_purge module of frickle.com must be configured to clear the cache of the specified URL.

Proxy_cache and fastcgi_cache constitute the Nginx cache. proxy_cache is mainly used for reverse proxy and caching the backend content source server. fastcgi_cache is mainly used for caching FastCGI dynamic programs. The two functions are basically the same.

  • Proxy_cache is used to cache the content of backend servers. It may be any content, including static and dynamic content.
  • The proxy_cache cache reduces the number of communications between nginx and the backend, saving the transmission time and backend bandwidth.
  • Fastcgi_cache is used to cache the content generated by fastcgi. In many cases, it is the dynamic content generated by php.
  • Fastcgi_cache cache reduces the number of communications between nginx and php.
Http://labs.frickle.com/nginx_ngx_cache_purge/github:https://github.com/FRiCKLE/ngx_cache_purge/

1. Download The ngx_cache_purge-2.2, decompress it, and get the path

wget -c http://labs.frickle.com/files/ngx_cache_purge-2.2.tar.gztar zxvf ngx_cache_purge-2.2.tar.gzcd ngx_cache_purge-2.2pwd/usr/local/src/ngx_cache_purge-2.2

2. Check the nginx compilation parameters that have been installed (find the binary file nginx-V)

[root@localhost ~]# /usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.7.8built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) TLS SNI support enabledconfigure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module

3. Go to the installation package directory decompressed by the original nginx and run the preceding compilation parameters. Then, use the path of-add-module = module to add the module.

cd /usr/local/src/nginx-1.7.8./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/ngx_cache_purge-2.2make && make install

4. Verify that the third-party module ngx_cache_purge just added is successfully installed.

[root@localhost nginx-1.7.8]# /usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.7.8built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) TLS SNI support enabledconfigure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/root/ngx_cache_purge-2.2

5. basic configuration: Clear the cache of the reverse proxy.

Http {proxy_cache_path/tmp/cache keys_zone = tmpcache: 10 m; server {location/{# reverse proxy for a backend server proxy_pass http: // 127.0.0.1: 8000; # Set the name of a cache region. The same region can be used in different places. Proxy_cache tmpcache; # The command specifies the cache keywords contained in the cache. Proxy_cache_key $ uri $ is_args $ args;} # only allow the local network to clear the cache location ~ /Purge (/. *) {allow 127.0.0.1; deny all; proxy_cache_purge tmpcache $1 $ is_args $ args ;}}}

6. proxy_cache cache and clear configuration in the production environment

User www; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;/usr/local/nginx/logs/nginx. pid; worker_rlimit_nofile 65535; events {use epoll; worker_connections 65535;} http {include mime. types; default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; client_header_buffer_size 32 k; large_client_header_buf Fers 4 32 k; Listen 300 m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; Limit 512 k; proxy_connect_timeout 5; proxy_read_timeout 60; limit 5; Limit 16 k; proxy_buffers 4 64 k; Limit 128 k; proxy_temp_file_write_size 128 k; gzip on; gzip_min_length 1 k; gzip_buffers 4 16 k; gzip_http_version 1.0; gzip_comp_level 2; gzip_vary on; gzip_types text/plain text/css image/png image/gif image/jpeg application/x-javascript text/xml application/xml + rss text/javascript image/x-icon; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6] \. "; log_format main '$ remote_addr-$ remote_user [$ time_local]" $ request "'' $ status $ body_bytes_sent "$ http_referer" ''" $ http_user_agent "" $ h Paths "'' "$ upstream_cache_status" '; # The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition because they are hard-linked. proxy_temp_path/var/cache/nginx/proxy_temp_dir; # Set the name of the Web cache area to cache_one. The size of the memory cache space is 200 MB. The content not accessed within one day is automatically cleared. The size of the hard disk cache space is 30 GB. Proxy_cache_path/var/cache/nginx/proxy_cache_dir levels = keys_zone = cache_one: 200 m inactive = 1d max_size = 30g; # backend request server upstream backend_server {server http://back01.test.com Weight = 1 max_fails = 2 fail_timeout = 30 s; server http://back02.test.com Weight = 1 max_fails = 2 fail_timeout = 30 s;} server {listen 80; server_name www.test.com; index index.html index.htm; root/usr/share/wwwroot /; # Set proxy_cache cache location/{# If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover. Proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_redirect off; # cache cache_one proxy_cache cache_one; # normal state cache time 12 hours proxy_cache_valid 200 304 12 h; # combine domain names, Uris, and parameters into the Web cache Key values. Nginx stores the cached content to the proxy_cache_key $ host $ URI $ is_args $ args in the second-level cache directory based on the hash of the Key values; proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ remote_addr; proxy_pass http://backend_server ; Add_header X-Cache '$ upstream_cache_status from $ host'; # expires 10 days by default;} # used to clear the Cache. Assume that a URL is http://www.test.com/test.txt By accessing http://www.test.com/purge/test.txt You can clear the cache of the URL. Location ~ /Purge (/. *) {# Set the account and password for the permission to access the purge path. Auth_basic "TDT Center CACHE Center"; auth_basic_user_file/tmp/htpasswd; # You can clear the url cache only by specifying IP addresses or IP segments. Allow 127.0.0.1; allow 192.168.5.0/24; deny all; proxy_cache_purge cache_one $ host $1 $ is_args $ args;} # extension. php ,. jsp ,. dynamic applications ending with cgi are not cached. Location ~. * \. (Php | jsp | cgi )? $ {Proxy_set_header Host $ host; proxy_set_header X-Forwarded-For $ remote_addr; proxy_pass http://backend_server ; Log_not_found off;} access_log/usr/local/nginx/logs/access. log main ;}}

Access an image in the website directory to view the header information:

For more information about the Nginx proxy cache module, see the following official documentation:

http://nginx.org/cn/docs/http/ngx_http_proxy_module.html

6. Use fastcgi_cache for caching

# Add in the http layer #15 m for memory usage 1g for hard disk space usage fastcgi_cache_path/home/cache/fcgi levels = keys_zone = fcgi: 15 m inactive = 1d max_size = 1g; add location ~ to the location Layer ~ [^/] \. Php (/| $) {fastcgi_pass 127.0.0.1: 9000; fastcgi_index index. php; include fastcgi_params; fastcgi_cache fcgi; # indicates enabling the FastCGI cache and specifying a name for it. Fastcgi_cache_valid 200 302 301 1 h; # Cache http status 200 302 301 1 hour fastcgi_cache_valid any 1 m; # other response code is cached for 1 minute. Fastcgi_cache_min_uses 1; # Set the link request to be cached several times. Fastcgi_cache_use_stale error timeout invalid_header http_500; # define the expiration cache fastcgi_cache_key $ request_method: // $ host $ request_uri; # add $ request_method as the cache key, otherwise, if the first request of the HEAD type causes the subsequent GET request to return NULL} location ~ /Purge (/. *) {# Set the account and password for the permission to access the purge path. Auth_basic "TDT Center CACHE Center"; auth_basic_user_file/tmp/htpasswd; # You can clear the url cache only by specifying IP addresses or IP segments. Allow 127.0.0.1; allow 192.168.5.0/24; deny all; proxy_cache_purge fcgi $ host $1 $ is_args $ args ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.