欢迎访问分类目录网
快审联系QQ:158925126当前位置:分类目录网 » 站长资讯 » 技术教程 » 文章详细 订阅RssFeed

设置nginx的proxy_cache 网站缓存 详细步骤

来源:网络 浏览:868次 时间:2015-05-31
随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力。与此同时某些网站的页面内容并不是经常变化,因此我们可以分两层架构来组织网站。前端web缓存+后端web服务器,可以参看这里配置nginx反向代理配置

前端web缓存有多重方式实现,原理就是队请求结果页面静态化并设置一个超时期限,缓存页面过期后,新请求到达时重新到后端web服务器获取内容更 新;没有nginx前比较流行的方法是squid,但squid不能充分利用处理器的多核特性,越来越多的网站选用nginx来做前端的web缓存。

要想使用nginx的缓存功能要保证nginx添加了proxy模块。我们可以使用-V选项(大写的V,小写的v是看版本号的)来查看nginx的编译参数。我使用的是默认的参数编译的,如下所示:

root@SNDA-172-17-12-117:/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的所有模块必须在编译的时候添加,不能再运行的时候动态加载,默认的编译选项下包含的模块,如果你不是显示的用参数关闭它。

nginx默认安装的模块如下

模块名称 描述 版本 如何禁用
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 are 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模块中常用的指令时proxy_pass和proxy_cache.

nginx的web缓存功能的主要是由proxy_cache、fastcgi_cache指令集和相关指令集完成,proxy_cache指令负 责反向代理缓存后端服务器的静态内容,fastcgi_cache主要用来处理FastCGI动态进程缓存(这里我不是很清楚这两个指令的区别,好像功能 上都差不多,尤其后面这句话的意思,是我翻译过来的)。

确认proxy模块安装好后,下面对nginx的配置文件进行设置,重点部分如标红字体所示。

这是我的nginx.conf配置文件。

user www-data;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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_timeout 0;
keepalive_timeout 65;

#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip

#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end

## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}

## Start www.quancha.cn ##
server {
listen 80;
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 200;

#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_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_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##

}

配置文件中以proxy_开头的指令我们大都可以字面意思得到理解。请务必注意一点proxy_cache_path和proxy_temp_path设置的目录需要在同一分区,因为它们之间是硬链接的关系。

最后执行重新加载nginx配置文件启动nginx :/usr/local/nginx/sbin/nginx -s reload,来迎接着激动人心的时刻吧。

公司企业大全

推荐站点

  • 纳米AI搜索 纳米AI搜索

    纳米AI搜索开创全新问答方式,没有套路,直接给答案,让搜索变得简单直观!拍照问、语音搜、听

    www.n.cn
  • 一号屋手赚库 一号屋手赚库

    一号屋手赚库致力于分享最新且免费的手机赚钱软件,在这里,你可以找到各种类型的赚钱app,获

    www.yihaowu.com
  • 事业编招聘网 事业编招聘网

    事业编招聘网(sybzp.cn)事业单位招聘信息基考试资料原创内容网站,主打快速、全面、优

    www.sybzp.cn
  • 4K高清电影下载 4K高清电影下载

    4Kfilm视界是专业的4K电影下载站,本网站页面简洁,提供4K盘HDR杜比视界电影、美剧

    www.4kfilm.cn
  • 百评客 百评客

    百评客深度评测各种赚钱游戏、手机赚钱app,提供客观且真实的评测分析,在这里,你可以更了解

    m.baike5.com
  • 优质设计素材 优质设计素材

    颜格视觉专注海外创意广告设计图片素材下载的网站!提供包括样机素材,平面素材,UI设计,ic

    www.youngem.com
  • 国家智慧教育平台 国家智慧教育平台

    国家智慧教育平台,全称国家智慧教育公共服务平台,是由中华人民共和国教育部指导,教育部教育技

    www.smartedu.cn
  • 鸟说游戏 鸟说游戏

    鸟说是一个聚焦电竞产业的网站,旨在分享不一样的电竞乐趣。我们致力于提供最新的电竞资讯、热门

    www.niaoshuo.com
  • 游软盟 游软盟

    游软盟是一个免费的应用下载网站,为用户提供好玩的手机游戏、实用的手机软件下载,我们也会及时

    app.ufolm.com