1. 程式人生 > >Nginx 實現本地靜態檔案記憶體快取

Nginx 實現本地靜態檔案記憶體快取

1、需求背景

搭建靜態檔案伺服器,要求在本地部署Nginx,訪問靜態檔案時使用Nginx做記憶體快取。

2、Nginx架構


3、nginx.conf

http {
    include       /etc/nginx/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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    server_tokens off;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay on;

    proxy_temp_path /dev/shm/temp_dir/;
    proxy_cache_path /dev/shm/cache/ levels=1:2 keys_zone=cache_one:10m;

upstream  local_img {
         server localhost:81;
    }
 server{
     listen       81;
     server_name 127.0.0.1;
     location / {
             root /home/static/data/;
             client_max_body_size   10m;
             access_log off;
             autoindex off;
             }
 }
server {
     listen       80;
     server_name img.test.com;
     proxy_cache cache_one;
     location / {
             proxy_redirect off;
             proxy_cache_valid 200 304 12h;
             proxy_cache_valid 301 302 1m;
			 proxy_cache_valid any 1m;
             proxy_cache_key $host$uri$is_args$args;
             add_header X-Cache $upstream_cache_status;
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_pass http://local_img;
             access_log off;
      }
 }

}
4、配置說明

proxy_cache快取路徑在/dev/shm下面,該目錄為實體記憶體地址