一、浏览器缓存html
server {
listen 8083;
server_name 127.0.0.1;
sendfile on;
access_log /var/log/nginx/static_server_access.log;
error_log /var/log/nginx/static_server_error.log;
location ~ .*\.(html|htm) {
expires 24h;(缓存过时时间)
root /Data/work/picture;
}
}
二、跨站访问前端
server {
listen 8083;
server_name 127.0.0.1;
sendfile on;
access_log /var/log/nginx/static_server_access.log;
error_log /var/log/nginx/static_server_error.log;
location ~ .*\.(html|htm) {
add_header Access-Control-Allow-Origin http://www.jesonc.com;(容许某个站点进行跨站访问) add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;(容许进行跨站访问的http请求方法)
root /Data/work/picture;
}
}
(1)概念nginx
防止资源被盗用ajax
(2)防盗链设置思路redis
首要方式:区别哪些请求是非正常的用户请求(阻止非正经常使用户常常访问,保证正经常使用户正常访问)后端
(3)基于http_refer防盗链配置模块跨域
Syntax:valid_referers none|blocked|server_names|string...;浏览器
Default:--缓存
Context:server,loation安全
valid_referers:容许哪些referer信息访问
none:容许没有带referer信息的访问
blocked:容许非http://domain样式的请求访问
server_names:只容许ip的方式访问
server {
listen 8083;
server_name 127.0.0.1;
sendfile on;
access_log /var/log/nginx/static_server_access.log;
error_log /var/log/nginx/static_server_error.log;
location ~ .*\.(jpg|gif|png)$ { valid_referers none blocked 192.168.126.137; if ($invalid_referer) { return 403; }
root /Data/work/picture;
}
}
(4)测试防盗链
不予许访问的地址,如百度,返回403错误码
curl -e "http://www.baidu.com" -I http://ip:8083/1.jpg
容许访问的地址,返回200成功码
curl -e "http://192.168.126.137" -I http://ip:8083/1.jpg
四、代理服务
(1)原理
代理--代为办理(代理理财,代理收获等等)
(2)代理分类
按应用场景模式总结
<1>正向代理(客户端经过代理服务器访问网站,如访问谷歌,客户端请求代理服务器由代理服务器去访问谷歌,客户端不须要访问谷歌)
<2>反向代理(服务端用来均衡流量等做用)
(3)代理区别
区别在于形式上服务的对象不同
正向代理代理的对象是客户端,为客户端服务
反向代理代理的对象是服务端,为服务端服务
(4)配置语法
Syntax:proxy_pass URL;
Default:--
Context:location,if in location, limit_except
<1>反向代理
server {
listen 8083;
server_name 127.0.0.1;
sendfile on;
access_log /var/log/nginx/static_server_access.log;
error_log /var/log/nginx/static_server_error.log;
location ~ /test_proxy.html$ { proxy_pass http://192.168.126.137:8082; }
}
(5)代理补充配置和规范
server {
listen 8083;
server_name 127.0.0.1;
sendfile on;
access_log /var/log/nginx/static_server_access.log;
error_log /var/log/nginx/static_server_error.log;
location / {
proxy_pass http://192.168.126.137:8090; proxy_redirect default;(重定向) proxy_set_header Host $http_host;(nginx代理日后端server发送信息的时候所添加的头信息,经常会添加的为Host头信息) proxy_set_header X-Real-IP $remote_addr;(获取前端的真实ip地址) proxy_connect_timeout 30s;(链接请求的超时时间) proxy_send_timeout 60s;(发送数据超时时间) proxy_read_timeout 60s;(读取数据超时时间) proxy_buffer_size 32k;(nginx默认缓冲区的内存大小) proxy_buffering on;(尽量的读取缓冲区中后端响应信息,一次传递全部信息给前端,减小IO损耗) proxy_buffers 4 128k; proxy_busy_buffers_size 256k; proxy_max_temp_file_size 256k;(当缓存区已满时,将数据存到临时文件中,设置临时文件的大小)
}
}
五、nginx做为缓存服务
(1)缓存类型
<1>若是缓存放到服务端,称为服务端缓存(redis,memcahce)
<2>若是缓存放到代理或者中间件上,称为代理缓存(从服务端获取到数据后,先缓存到本地一份,再返回给客户端使用)
<3>若是缓存放到客户端,称为客户端缓存(缓存到浏览器上)
(2)proxy_cache配置语法
Syntax:proxy_cache_path path[levels=levels] [use_temp_path=on|off] keys_zone=name:size[inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
Default:--
Context:http
定义好path后
Syntax:proxy_cache zone|off;
Default:proxy_cache off;
Context:http,server,location
缓存过时周期
Syntax:proxy_cache_valid [code...] time;
Default:--
Context:http,server,location
缓存的维度
Syntax:proxy_cache_key string;
Default:proxy_cache_key $scheme$proxy_host$request_uri
Context:http,server,location
upstream tcache {
server 192.168.126.137:8090;
server 192.168.126.137:8081;
server 192.168.126.137:8082;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=test_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 8084;
server_name 127.0.0.1;
access_log /var/log/nginx/test_proxy_access.log main;
location / {
proxy_cache test_cache;
proxy_pass http://tcache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache $upstream_cache_status;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;(若是代理的服务器出现500,502,503,504错误时就默认跳过,访问下一台服务器)
}
}
(3)补充-如何清理指定缓存
方式一:rm -rf 缓存目录内容
方式二:第三方扩展模块ngx_cache_purge
(4)补充-如何让部分页面不缓存
Syntax:proxy_no_cache string ...;
Default:--
Context:http,server,location
upstream tcache {
server 192.168.126.129:8081;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=test_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 8084;
server_name 127.0.0.1;
access_log /var/log/nginx/test_proxy_access.log main;
if ($request_uri ~ ^/(url|login|register|password\/reset))
{ set $cookie_nocache 1;
} (判断请求是否以不能缓存的uri路径开头,若是是则将cookie_nocache设置为1)
location ~ .*\.(jpg|png|gif) {
proxy_cache test_cache;
proxy_pass http://tcache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
proxy_no_cache $cookie_nocache $arg_noache $arg_comment;(cookie_nocache不为0或者空,这不能缓存)
proxy_no_cache $http_pragma $http_authorization;
add_header Nginx-Cache $upstream_cache_status;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location ~ .*\.(html|htm){
root /Data/work/picture; index index.html index.htm;
}
}
六、缓存命中分析
(1)方式一:经过设置response的头信息Nginx-Cache
add_header Nginx-Cache "$upstream_cache_status";
$upstream_cache_status
状态 | 意义 |
MISS | 未命中,请求被传送到后台处理 |
HIT | 缓存命中 |
EXPIRED | 缓存已通过期,请求被传送到后台处理 |
UPDAING | 正在更新缓存,将使用旧的应答 |
STALE | 后端获得过时的应答 |
(2)方式二:经过设置log_format,打印日志分析
缓存命中率 = HIT次数/总请求数
实现方式:分析Nginx里的Access日志
awk命令使用(分析命中率)
awk '{if($NF=="\"HIT\""){hit++}}END{printf "%.2f",hit/NR}' /var/log/nginx/test_proxy_access.log(红色为可变量,蓝色为自定义量)
七、负载均衡
(1)配置语法
Syntax:upstream name {...}
Default:--
Context:http
upstream test { server 192.168.126.137:8081; server 192.168.126.137:8090; server 192.168.126.137:8091; }
server {
listen 8086;
server_name 127.0.0.1;
charset UTF-8;
access_log /var/log/nginx/test_proxy_access.log main;
error_log /var/log/nginx/test_proxy_error.log;
location / {
proxy_pass http://test;
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 30s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}