{"action":"opened","issue":{"url":"https://api.github.com/repos/wall-wxk/blog/issues/3","repository_url":"https://api.github.com/repos/wall-wxk/blog","labels_url":"https://api.github.com/repos/wall-wxk/blog/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/wall-wxk/blog/issues/3/comments","events_url":"https://api.github.com/repos/wall-wxk/blog/issues/3/events","html_url":"https://github.com/wall-wxk/blog/issues/3","id":321766226,"number":3,"title":"使用nginx缓存服务器上的静态文件","user":{"login":"wall-wxk","id":25104791,"avatar_url":"https://avatars3.githubusercontent.com/u/25104791?v=4","gravatar_id":"","url":"https://api.github.com/users/wall-wxk","html_url":"https://github.com/wall-wxk","followers_url":"https://api.github.com/users/wall-wxk/followers","following_url":"https://api.github.com/users/wall-wxk/following{/other_user}","gists_url":"https://api.github.com/users/wall-wxk/gists{/gist_id}","starred_url":"https://api.github.com/users/wall-wxk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wall-wxk/subscriptions","organizations_url":"https://api.github.com/users/wall-wxk/orgs","repos_url":"https://api.github.com/users/wall-wxk/repos","events_url":"https://api.github.com/users/wall-wxk/events{/privacy}","received_events_url":"https://api.github.com/users/wall-wxk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-10T00:06:00Z","updated_at":"2018-05-10T00:06:00Z","closed_at":null,"author_association":"OWNER","body":"## 一、nginx缓存的优点\r\n\r\n![nginx缓存图示](http://wangxiaokai.vip/images/2018-05-09-nginx-cache/1.png)\r\n\r\n如图所示，nginx缓存，可以在一定程度上，减少源服务器的处理请求压力。\r\n因为静态文件（比如css，js， 图片）中，很多都是不经常更新的。nginx使用proxy_cache将用户的请求缓存到本地一个目录。下一个相同请求可以直接调取缓存文件，就不用去请求服务器了。\r\n毕竟，IO密集型服务的处理是nginx的强项。\r\n\r\n## 二、如何进行设置\r\n\r\n先上个栗子：\r\n```\r\nhttp{\r\n    proxy_connect_timeout 10;\r\n    proxy_read_timeout 180;\r\n    proxy_send_timeout 5;\r\n    proxy_buffer_size 16k;\r\n    proxy_buffers 4 32k;\r\n    proxy_busy_buffers_size 96k;\r\n    proxy_temp_file_write_size 96k;\r\n    proxy_temp_path /tmp/temp_dir;\r\n    proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;\r\n\r\n\r\n    server {\r\n        listen       80 default_server;\r\n        server_name  localhost;\r\n        root /mnt/blog/;\r\n\r\n        location / {\r\n\r\n        }\r\n\r\n        #要缓存文件的后缀，可以在以下设置。\r\n        location ~ .*\\.(gif|jpg|png|css|js)(.*) {\r\n                proxy_pass http://ip地址:90;\r\n                proxy_redirect off;\r\n                proxy_set_header Host $host;\r\n                proxy_cache cache_one;\r\n                proxy_cache_valid 200 302 24h;\r\n                proxy_cache_valid 301 30d;\r\n                proxy_cache_valid any 5m;\r\n                expires 90d;\r\n                add_header wall  \"hey!guys!give me a star.\";\r\n        }\r\n    }\r\n\r\n    # 无nginx缓存的blog端口\r\n    server {\r\n        listen  90;\r\n        server_name localhost;\r\n        root /mnt/blog/;\r\n\r\n        location / {\r\n\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n因为我\b是在一台\b\b\b\b服务器上做试验，\b所以用了两个端口`80`和`90`进行模拟两台服务器之间的交互。\r\n> `80`端口对接的是普通的域名（`http://wangxiaokai.vip`）访问。   \r\n  `90`端口负责处理`80`\b端口代理过来的资源访问。   \r\n  相当于`90`端口是源服务器，`80`端口是nginx\b反向缓存代理服务器。\r\n\r\n---\r\n\r\n接下来讲一下配置项：\r\n\r\n### 2.1 http层设置\r\n\r\n```\r\n    proxy_connect_timeout 10;\r\n    proxy_read_timeout 180;\r\n    proxy_send_timeout 5;\r\n    proxy_buffer_size 16k;\r\n    proxy_buffers 4 32k;\r\n    proxy_busy_buffers_size 96k;\r\n    proxy_temp_file_write_size 96k;\r\n    proxy_temp_path /tmp/temp_dir;\r\n    proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=10g;\r\n```\r\n\r\n- proxy_connect_timeout `服务器连接的超时时间`\r\n- proxy_read_timeout `连接成功后,等候后端服务器响应时间`\r\n- proxy_send_timeout `后端服务器数据回传时间`\r\n- proxy_buffer_size `缓冲区的大小`\r\n- proxy_buffers `每个连接设置缓冲区的数量为number，每块缓冲区的大小为size`\r\n- proxy_busy_buffers_size `开启缓冲响应的功能以后，在没有读到全部响应的情况下，写缓冲到达一定大小时，nginx一定会向客户端发送响应，直到缓冲小于此值。`\r\n- proxy_temp_file_write_size `设置nginx每次写数据到临时文件的size(大小)限制`\r\n- proxy_temp_path `从后端服务器接收的临时文件的存放路径`\r\n- proxy_cache_path `设置缓存的路径和其他参数。被缓存的数据如果在inactive参数（当前\b为1天）指定的时间内未被访问，就会被从缓存中移除`\r\n\r\n### 2.2 \bserver层设置\r\n\r\n#### 2.2.1 反向\b缓存代理服务器\r\n\r\n```\r\n    server {\r\n        listen       80 default_server;\r\n        server_name  localhost;\r\n        root /mnt/blog/;\r\n\r\n        location / {\r\n\r\n        }\r\n\r\n        #要缓存文件的后缀，可以在以下设置。\r\n        location ~ .*\\.(gif|jpg|png|css|js)(.*) {\r\n                proxy_pass http://ip地址:90;\r\n                proxy_redirect off;\r\n                proxy_set_header Host $host;\r\n                proxy_cache cache_one;\r\n                proxy_cache_valid 200 302 24h;\r\n                proxy_cache_valid 301 30d;\r\n                proxy_cache_valid any 5m;\r\n                expires 90d;\r\n                add_header wall  \"hey!guys!give me a star.\";\r\n        }\r\n    }\r\n```\r\n\r\n- proxy_pass `\bnginx缓存里拿不到资源，向该地址转发请求，拿到新的资源，并进行缓存`\r\n- proxy_redirect `设置后端服务器“Location”响应头和“Refresh”响应头的替换文本`\r\n- proxy_set_header `允许重新定义或者添加发往后端服务器的请求头`\r\n- proxy_cache `指定用于页面缓存的共享内存，对应http层设置的keys_zone`\r\n- proxy_cache_valid `为不同的响应状态码设置不同的缓存时间`\r\n- expires `缓存时间`\r\n\r\n---\r\n\r\n这里我设置了`图片`、`css`、`js`静态资源进行缓存。\r\n当用户输入`http://wangxiaokai.vip`域名时，解析得到`ip:port`的访问地址。`port`默认为80\b。所以页面请求会被当前server截取到，进行请求处理。\r\n当解析到上述文件名结尾到静态资源，会到缓存区获取静态资源。\r\n如果获取到对应资源，则直接返回数据。\r\n如果获取不到，则将请求转发给`proxy_pass`指向到地址进行处理。\r\n\r\n#### 2.2.2 源服务器\r\n\r\n```\r\n    server {\r\n        listen  90;\r\n        server_name localhost;\r\n        root /mnt/blog/;\r\n\r\n        location / {\r\n\r\n        }\r\n    }\r\n```\r\n\r\n这里直接处理`90`端口接受到到请求，到服务器本地目录`/mnt/blog`下抓取资源\b进行响应。\r\n\r\n## 三、如何验证缓存是否\b有效\r\n\r\n\b细心到读者应该发现，我在第二段\b栗子里，留了个彩蛋 `add_header wall  \"hey!guys!give me a star.\"`。\r\n`add_header`是用于在报头设置自定义的信息。\r\n所以，如果\b缓存有效的话，那么静态资源返回的报头，一定会带上这个信息。\r\n\r\n访问`http://wangxiaokai.vip`结果如下：\r\n![nginx缓存结果\b](http://wangxiaokai.vip/images/2018-05-09-nginx-cache/2.png)\r\n\r\n## 四、参考\r\n\r\n[1] [nginx\b文档](https://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_proxy_module.html)\r\n[2] [nginx反向缓存代理详解](http://blog.51cto.com/xsboke/1918681)\r\n[3] [Nginx缓存服务器静态文件](http://www.qttc.net/20120681.html)\r\n\r\n## 五、后记\r\n\r\n[\b我的GitHub仓库](https://github.com/wall-wxk/blog)\r\nGive me a star,if it's work out for you.Thank you.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"wall-wxk"}}