Why do you want to do Web cache, I think everyone is the most important to solve the traffic pressure. With the increase of website traffic, if only a single machine processing static files, and processing dynamic scripts, it is obvious that efficiency is difficult to rise, unable to handle the rising flow pressure. At the same time, the page content of some websites is not constantly changing, so we can organize the website in two tiers of architecture. Front-end Web cache + Backend Web server, see here to configure Nginx reverse proxy configuration
Front-end Web caching is implemented in multiple ways, the principle is that the team request the result page is static and set a time-out period, after the cache page expires, the new request arrives back to the backend Web server to obtain content updates; The popular method without nginx is squid, But squid can not take full advantage of the multi-core features of the processor, more and more websites choose Nginx to do the front-end Web cache.
To use the Nginx cache function to ensure that Nginx added the proxy module. We can use the-V option (the upper case V, the lowercase v is the version number) to see the Nginx compilation parameters. I am using the default parameters compiled as follows:
[Email protected]:/usr/local/nginx#./nginx-v
Nginx version:nginx/1.2.3
Built by GCC 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI Support Enabled
Configure arguments:--sbin-path=/usr/local/nginx/nginx--conf-path=/usr/local/nginx/nginx.conf--pid-path=/usr/ Local/nginx/nginx.pid--with-http_ssl_module--with-pcre=/usr/local/src/pcre-8.21--with-zlib=/usr/local/src/ zlib-1.2.7
Nginx All modules must be added at compile time, can no longer run when the dynamic load, the default compilation option under the module contained, if you are not displayed with the parameters to close it.
The default installation of the Nginx module is as follows
| Module Name |
Description |
version |
How to disable |
| Core |
Control Ports, locations, error pages, aliases, and other essentials. |
|
--without-http |
| Access |
Allow/deny based on IP address. |
|
--without-http_access_module |
| Auth Basic |
Basic HTTP authentication. |
|
--without-http_auth_basic_module |
| Auto Index |
Generates automatic directory listings. |
|
--without-http_autoindex_module |
| Browser |
Interpret "User-agent" string. |
0.4.3 |
--without-http_browser_module |
| Charset |
Recode Web pages. |
|
--without-http_charset_module |
| Empty GIF |
Serve a 1x1 image from memory. |
0.3.10 |
--without-http_empty_gif_module |
| FastCGI |
FastCGI support. |
|
--without-http_fastcgi_module |
| Geo |
Set config variables using key/value pairs of IP addresses. |
0.1.17 |
--without-http_geo_module |
| Gzip |
Gzip responses. |
|
--without-http_gzip_module |
| Headers |
Set arbitrary HTTP response headers. |
|
| Index |
Controls which files is to be used as index. |
|
| Limit Requests |
Limit frequency of connections from a client. |
0.7.20 |
--without-http_limit_req_module |
| Limit Zone |
Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. |
0.5.6 |
--without-http_limit_zone_module |
| Limit Conn |
Limit concurrent connections based on a variable. |
|
--without-http_limit_conn_module |
| Log |
Customize access logs. |
|
| Map |
Set config variables using arbitrary key/value pairs. |
0.3.16 |
--without-http_map_module |
| Memcached |
Memcached support. |
|
--without-http_memcached_module |
| Proxy |
Proxy to upstream servers. |
|
--without-http_proxy_module |
| Referer |
Filter requests based on Referer header. |
|
--without-http_referer_module |
| Rewrite |
Request rewriting using regular expressions. |
|
--without-http_rewrite_module |
| scgi |
SCGI protocol support. |
0.8.42 |
--without-http_scgi_module |
| Split clients |
Splits clients based on some conditions |
0.8.37 |
--without-http_split_clients_module |
| Ssi |
Server-side includes. |
|
--without-http_ssi_module |
| Upstream |
For load-balancing. |
|
--without-http_upstream_ip_hash_module (ip_hash directive only) |
| User ID |
Issue identifying cookies. |
|
--without-http_userid_module |
| Uwsgi |
UWSGI protocol support. |
0.8.40 |
--without-http_uwsgi_module |
| X-accel |
X-sendfile-like module. |
|
Proxy_pass and Proxy_cache are commonly used in the proxy module.
Nginx Web caching function is mainly done by Proxy_cache, Fastcgi_cache instruction set and related instruction set, Proxy_cache instruction is responsible for reverse proxy cache back-end server static content, Fastcgi_ The cache is primarily used to handle fastcgi dynamic process caching (here I am not very clear about the difference between the two commands, as if the function is similar, especially after the meaning of this sentence, I translated it).
After confirming that the proxy module is installed, the Nginx configuration file is set up below, and the key part is shown in the red font.
This is my nginx.conf configuration file.
User www-data;worker_processes1; #error_log logs/Error.log, #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {worker_connections1024x768;} HTTP {include Mime.types;default_type application/octet-Stream;log_format Main'$remote _addr-$remote _user [$time _local] "$request"''$status $body _bytes_sent "$http _referer"''"$http _user_agent" "$http _x_forwarded_for" "$host"'; #access_log logs/Access.log main;sendfile on, #tcp_nopush on; #keepalive_timeout0; Keepalive_timeout $; #Compression settingsgzip on;gzip_http_version1.0; Gzip_comp_level2; gzip_proxied any;gzip_min_length1100; Gzip_buffers -8k;gzip_types Text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;# Some version of IE6Don'T handle compression well on some mime-types,# so just disable forthemgzip_disable"MSIE [1-6]. (?!. *SV1)"; # Set a vary header so downstream proxies Don't send cached gzipped# content to ie6gzip_vary in; #end Gzip#cache beginproxy_buffering on;proxy_cache_valid any 10m;proxy_cache_path /data/cache levels=1:2keys_zone=my-cache:8m max_size=1000m inactive=600m;proxy_temp_path/data/temp;proxy_buffer_size 4k;proxy_buffers -8k, #cache end## Basic Reverse proxy Server # # Apache (vm02) backend forwww.example.com # #upstream apachephp {server www.quancha.cn:8080; #Apache1}## Start www.quancha.cn # #server {Listen the; server_name*. Quancha.cn;access_log logs/quancha.access.log Main;error_log Logs/quancha.error.log;root html;index index.html index.htm index.php;## send request back to Apache1 # #location/{proxy_pass http://apachephp;Proxy_cache my-Cache;proxy_cache_valid $; #Proxy settingsproxy_redirect off;proxy_set_header Host $host;p roxy_set_header X-real-IP $remote _addr;proxy_set_header X-forwarded-For $proxy _add_x_forwarded_for;proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_ 504;proxy_max_temp_file_size0;p roxy_connect_timeout -;p roxy_send_timeout -;p roxy_read_timeout -;p roxy_buffer_size 4k;proxy_buffers432k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;# #End proxy Settings}} # # End www.quancha.cn # #}
Most of the instructions in the configuration file that begin with Proxy_ can be understood literally. It is important to note that the Proxy_cache_path and Proxy_temp_path settings of the directory need to be in the same partition because they are hard-linked relationships.
Finally start the nginx, to meet the exciting moment. I can't wait to have it. If the article where there is a problem or you encounter any trouble, you can leave a message let me know.
Use Nginx Proxy_cache to do website cache