系统是CENTOS5.9
wget -c http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm
rpm -ivh nginx-release-centos-5-0.el5.ngx.noarch.rpm
ulimit -HSn 65536
yum -y install nginx
cd /home #进入目录
mkdir -p /home/proxy_temp_dir #proxy_temp_dir与proxy_cache_dir这两个文件夹必须在同一个分区
mkdir -p /home/proxy_cache_dir #proxy_cache_dir与proxy_temp_dir这两个文件夹必须在同一个分区
chown nginx.nginx -R proxy_cache_dir proxy_temp_dir #设置目录所有者
chmod -R 777 proxy_cache_dir proxy_temp_dir #设置目录权限
备份原来的配置文件
cd /etc/nginx
mv nginx.conf nginx.conf.bak
mv conf.d/default.conf conf.d/default.conf.bak
下面是我配置的配置文件 nginx.conf
user nginx nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /home/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天清理一次缓存,硬盘缓存空间大小为10GB。
proxy_cache_path /home/proxy_cache_dir levels=1:2 keys_zone=NAME:64m inactive=1d max_size=10g;
include /etc/nginx/conf.d/*.conf;
}
默认配置文件 conf.d/default.conf
这个文件是禁止未绑定的域名或IP访问
server {
listen 80 default;
server_name _;
return 500;
access_log off;
}
下面是网站反向代理配置文件 conf.d/blog.conf
server {
listen 80;
server_name blog.com; #网站域名
location / {
proxy_pass http://www.google.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache NAME;#使用NAME这个keys_zone
proxy_cache_valid 200 302 1h;#200和302状态码保存1小时
proxy_cache_valid 301 1d;#301状态码保存一天
proxy_cache_valid any 1m;#其它的保存一分钟
}
}
多个网站可以复制多个conf.d/blog.conf 修改相应网址就行 配置完所有.重启NGINX
server nginx restart
然后观察缓存目录
tree /home/proxy_cache_dir
有文件生成表示缓存反向代理服务器成功!!
(责任编辑:IT) |