nginx 缓存配置安装

2021-04-28  本文已影响0人  WilburLi

安装 ngx_cache_purge-1.3. tar

wget http: //labs .frickle.com /files/ngx_cache_purge-2 .0. tar .gz
tar  -zxvf ngx_cache_purge-2.0. tar .gz
cd  /home/data/install/lnmp/nginx-1 .0.15/
查看版本
nginx -V
[root@ceshi nginx-1.0.15] # /home/data/websrv/nginx/sbin/nginx -V
nginx version: nginx /1 .0.15
configure arguments: --prefix= /home/data/websrv/nginx  --with-http_stub_status_module
. /configure  --user=nginx --group=nginx --add-module= /home/data/install/ngx_cache_purge-2 .0 \
  --prefix= /home/data/websrv/nginx  --with-http_stub_status_module --with-http_ssl_module
加模块
onfiguration summary
   + using system PCRE library
   + using system OpenSSL library
   + md5: using OpenSSL library
   + sha1: using OpenSSL library
   + using system zlib library
   nginx path prefix:  "/home/data/websrv/nginx"
   nginx binary  file :  "/home/data/websrv/nginx/sbin/nginx"
   nginx configuration prefix:  "/home/data/websrv/nginx/conf"
   nginx configuration  file :  "/home/data/websrv/nginx/conf/nginx.conf"
   nginx pid  file :  "/home/data/websrv/nginx/logs/nginx.pid"
   nginx error log  file :  "/home/data/websrv/nginx/logs/error.log"
   nginx http access log  file :  "/home/data/websrv/nginx/logs/access.log"
   nginx http client request body temporary files:  "client_body_temp"
   nginx http proxy temporary files:  "proxy_temp"
   nginx http fastcgi temporary files:  "fastcgi_temp"
   nginx http uwsgi temporary files:  "uwsgi_temp"
   nginx http scgi temporary files:  "scgi_temp"

接下来是关键 一定只能make 不可以 make install 否则会覆盖

然后替换 nginx 的二进制文件即可

# cp ./objs/nginx /home/data/websrv/nginx/sbin/
# service nginx start

接下来 可以配置 内核参数

vi  /etc/sysctl .conf

然后执行 /sbin/sysctl -p 让参数生效。

配置事例如下:

user coovanftp coovanftp;
worker_processes 8;
error_log  logs /error .log  crit;
worker_rlimit_nofile 65535;
events
{
   use epoll;
   worker_connections 65535;
}
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"' ;
     server_tokens off;
     access_log  logs /access .log  main;
     sendfile        on;
     tcp_nopush  on;
     tcp_nodelay on;
     keepalive_timeout  60;
     gzip   on;
     gzip_types       text /plain  application /x-javascript  text /css  application /xml ;
     server_names_hash_bucket_size 2048;
     client_header_buffer_size 2048k;
     large_client_header_buffers 4 2048k;
     client_max_body_size 500m;
     client_body_buffer_size 4048k;
     fastcgi_buffer_size         2048k;
     fastcgi_buffers             6 2048k;
     fastcgi_busy_buffers_size   2048k;
     fastcgi_temp_file_write_size        2048k;
     fastcgi_intercept_errors    on;
     proxy_connect_timeout 60;
     proxy_read_timeout 60;
     proxy_send_timeout 60;
     proxy_buffer_size 2048k;
     proxy_buffers 4 2048k;
     proxy_busy_buffers_size 2048k;
     #proxy_cache_valid 200 304 302 3s;
     proxy_temp_file_write_size 4096k;
     proxy_temp_path  /home/data/websrv/nginx/temp_dir ;
     proxy_cache_path  /home/data/websrv/nginx/cache  levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
#  upstream指令用于设置一组可以在proxy_pass和fastcgi_pass指令中使用的代理服务器,默认的负载均衡方式为轮询.upstream模块中的server指令用于指定后端服务器的名称和参数,服务器的名称可以是一个域名,一个ip地址,端口号或者UNIX Socket.
        #而在server{..}虚拟主机内,可以通过proxy_pass和fastcgi_pass指令设置进行反向代理的upstream服务器集群
       # proxy_set_header指令用于在向反向代理的后端WEB服务器发起请求时添加指定的header头信息
        #当后端WEB服务器上有多个基于域名的虚拟主机时,要通过添加header头信息Host,用于指定请求的域名,这样后端服务器才能识别该反向代理访问请求是由那一个虚拟主机来处理
        #使用反向代理之后,后端web服务器就不能直接$_SERVER['REMOTE_ADDR']变量来获取用户的真实ip了,通过$_SERVER['REMOTE_ADDR']获得的将是负载均衡器的ip.这时,就要通过Nginx反向代理时添加Header头信息X-Forwarded-For,让后端web服务器能够通过$_SERVER['HTTP_X_FORWARDED_FOR']获取到用户的真实ip
                                    
#nginx的proxy_cache相关指令集
     # 1.proxy_cache:该指令用于设置那个缓存区将被使用
     # 2.proxy_cache_path:该指令用于设置缓存文件的存放路径
    #示例:proxy_cache_path /web/server1 levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g
   # 该指令只能在http标签内配置,levels指定该缓存空间有两层hash目录,第一层为1个字母,第二层为2个字母;
   # keys_zone参数用来为这个缓存区起名,500m指内存缓存空间大小为500MB;inactive的1d指如果缓存数据在1天内没有被访问,将被删除;max_size的30g是指硬盘缓存空间为30GB
      #3.proxy_cache_methods:该指令用于设置缓存那些http方法,默认缓存get和head,不缓存post
      #4.proxy_cache_min_uses:该指令用于设置缓存的最小使用次数,默认为1
      #5.proxy_cache_valid:该指令用于对不同返回状态码的url设置不同的缓存时间
     upstream iis{
       server  192.168.0.21;
       }
server {
     listen       80;
     server_name  192.168.0.21;
     location / {
         index  index.php index.html;
         proxy_pass http: //iis ;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         proxy_cache_valid 200 5m;
         proxy_cache_valid 304 5m;
         proxy_cache_valid 301 302 1h;
         proxy_cache_valid any 5m;
         proxy_cache_key http: // $host$uri$is_args$args;
     }
     error_page   500 502 503 504   /50x .html;
     location =  /50x .html {
         root    /usr/share/nginx/html ;
     }
}
}
上一篇下一篇

猜你喜欢

热点阅读