nginx經常使用場景

一、瀏覽器緩存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;
   }    
}

二、跨站訪問前端

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

 

三、防盜鏈

(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

 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

四、代理服務

(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;
    }    
}
相關文章
相關標籤/搜索