nginx常用场景

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 ~ .*\.(html|htm) {
       expires    24h;(缓存过期时间)
       root    /Data/work/picture;
   }    
}

2、跨站访问

(1)概念
   浏览器访问同一个服务端,一个页面中当请求http://www.a.com时,同时会用到某种方式(ajax等)去请求http://www.b.com,这样就出现一个页面请求服务端用到两个域名,这种方式对于浏览器来说一般是默认禁止这么做的,主要是出于安全的考虑
(2)为什么浏览器禁止跨域访问
    不安全,容易出现CSRF攻击
(3)nginx怎么做
    Syntax: add_header name value [always];
    Default: --
    Context:http,server,location,if in location
    name:Access-Control-Allow-Origin
    value:允许进行跨站访问的站点
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;
   }    
}
3、防盗链

(1)概念

    防止资源被盗用

(2)防盗链设置思路

    首要方式:区别哪些请求是非正常的用户请求(阻止非正常用户经常访问,保证正常用户正常访问)

(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

 HTTP/1.1 403 Forbidden
 Server: nginx/1.14.1
 Date: Sat, 24 Nov 2018 14:50:35 GMT
 Content-Type: text/html
 Content-Length: 169
 Connection: keep-alive

允许访问的地址,返回200成功码

curl -e "http://192.168.126.137" -I http://ip:8083/1.jpg

 HTTP/1.1 200 OK
 Server: nginx/1.14.1
 Date: Sat, 24 Nov 2018 14:51:40 GMT  
 Content-Type: image/jpeg
 Content-Length: 35675
 Last-Modified: Sat, 24 Nov 2018 14:42:10 GMT  
 Connection: keep-alive
 ETag: "5bf96342-8b5b"
 Accept-Ranges: bytes

4、代理服务

(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;(当缓存区已满时,将数据存到临时文件中,设置临时文件的大小)
   }    
}

5、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;
  }
}

6、缓存命中分析

(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(红色为可变量,蓝色为自定义量)

7、负载均衡

(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;
    }    
}
 
 

猜你喜欢

转载自www.cnblogs.com/jindp/p/10739658.html