Nginx系列(十四. nginx分离静态文件)

来源:互联网 发布:随机数预测算法 编辑:程序博客网 时间:2024/06/09 15:51

一.配置文件

server {        listen       8080;        server_name  www.test01.com;        access_log  logs/host.access.log  main;        root   /web/www/longddcms;        location / {            root   /web/www/longddcms;            index  index.php;            if (!-e $request_filename){                rewrite ^/(.*)$ /index.php/$1 last;            }        }        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {             if (!-f $request_filename) {                  rewrite ^ /404.jpg last;              }             access_log off;            expires 15d;         }        location ~ .*\.(js|css)?$ {             expires 1h;             access_log off;        }        location ~ [^/]\.php(/|$) {            root /web/www/longddcms;            fastcgi_index index.php;            fastcgi_pass 127.0.0.1:9000;            include      fastcgi_params;            fastcgi_split_path_info ^(.+?\.php)(/.*)$;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;            fastcgi_param SCRIPT_NAME $fastcgi_script_name;            fastcgi_param PATH_INFO $fastcgi_path_info;            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;        }           location = /404.jpg {              root   html;          }         error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }

特别注意:server要配置默认root的路径,否则将会在nginx下的html目录下寻找,一直报404错误

二.相关配置项

expires 指令可以控制 http 应答中的“ expires ”和“ cache-control ”的头标(起到控制页面缓存的作用)
语法:expires [time|epoch|max|pff]

默认值:off
expires指令控制http应答中的“expires”和“cache-control”header头部信息,启动控制页面缓存的作用
time:可以使用正数或负数。“expires”头标的值将通过当前系统时间加上设定time值来设定。
time值还控制”cache-control”的值:
负数表示no-cache
正数或零表示max-age=time

epoch:指定“expires”的值为 1 january,1970,00:00:01 gmt
max:指定“expires”的值为31 december2037 23:59:59gmt,”cache-control”的值为10年。
-1:指定“expires”的值为当前服务器时间-1s,即永远过期。
off:不修改“expires”和”cache-control”的值

expires使用了特定的时间,并且要求服务器和客户端的是中严格同步。

而cache-control是用max-age指令指定组件被缓存多久。
对于不支持http1.1的浏览器,还是需要expires来控制。所以最好能指定两个响应头。但http规范规定max-age指令将重写expires头。


cache-control策略cache-control与expires的作用一致,都是指明当前资源的有效期,控制浏览器是否直接从浏览器缓存取数据还是重新发请求到服务器取数据。只不过cache-control的选择更多,设置更细致,如果同时设置的话,其优先级高于expires。

http协议头cache-control :值可以是public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age各个消息中的指令含义如下:

public指示响应可被任何缓存区缓存。

private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。 Linux学习,http:// linux.it.net.cn

no-cache指示请求或响应消息不能缓存

no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。 Linux学习,http:// linux.it.net.cn

max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。

min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。 Linux学习,http:// linux.it.net.cn

max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。

last-modified/if-modified-since

last-modified/if-modified-since要配合cache-control使用。 IT网,http://www.it.net.cn

last-modified:标示这个响应资源的最后修改时间。web服务器在响应请求时,告诉浏览器资源的最后修改时间。

if-modified-since:当资源过期时(使用cache-control标识的max-age),发现资源具有last-modified声明,则再次向web服务器请求时带上头 if-modified-since,表示请求时间。web服务器收到请求后发现有头if-modified-since 则与被请求资源的最后修改时间进行比对。若最后修改时间较新,说明资源又被改动过,则响应整片资源内容(写在响应消息包体内),http 200;若最后修改时间较旧,说明资源无新修改,则响应http 304 (无需包体,节省浏览),告知浏览器继续使用所保存的cache。

参考资料:

http://nginx.org/en/docs/http/ngx_http_headers_module.html

http://www.ahlinux.com/nginx/4921.html

http://baike.baidu.com/item/Cache-control

https://serversforhackers.com/nginx-caching/

0 0
原创粉丝点击