在总结反向代理,页面缓存,虚拟主机等配置后,为了在以后对nginx做出快速配置,现概括如下:
1.反向代理配置
server
{
listen 80;
server_name test.com;
location / {
proxy_pass http://upstream.com/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2.页面缓存配置
#新建2个目录,放置缓存文件:
mkdir /data/cache/path -p
mkdir /data/cache/temp -p
#修改 /usr/local/nginx/conf/nginx.conf 新增以下代码,主要是缓存相关设置,请放置于 http{ ##这里 } 中,一般加在 log_format 上面或下面均可:
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;
proxy_temp_path /data/cache/temp;
proxy_cache_path /data/cache/path levels=1:2 keys_zone=cache_one:500m inactive=7d max_size=30g;
#500m是内存占用,7d是7天无访问删除,30g是缓存占具硬盘空间
3.虚拟主机配置
server
{
listen 80;
server_name test.com; #主机名
location / {
proxy_cache cache_one;
proxy_cache_valid 200 304 3d; #正常状态缓存时间3天
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://upstream.com/; #反代的网站
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
expires 10d; #默认10天缓存
access_log /data/wwwlogs/test.log access; #日志文件
}
}