一、靜態(tài)資源WEB服務(wù)
非服務(wù)器動(dòng)態(tài)運(yùn)行生成的文件
配置語法-文件讀取
Synatax: sendfile on| off
Default: sendfile off;
Context: http,server,location,if in location
配置語法-tcp_noposh(sendfile開啟時(shí),提高網(wǎng)絡(luò)傳輸效率)
Syntax: tcp_nopush on|off;
Default: tcp_nopush off;
Context: http,server,location
簡(jiǎn)單點(diǎn)說吝岭,就是批量收集再push
配置語法-tcp_nodelay
Syntax: tcp_nodelay on|off;
Default: tcp_nodelay off;
Context: http,server,location
及時(shí)push枕扫,實(shí)時(shí)性傳輸
配置語法-壓縮
Syntax: gzip on|off;
Default: gzip off;
Context: http,server,location,if in location
壓縮傳輸,服務(wù)器端進(jìn)行壓縮幌蚊,瀏覽器上自動(dòng)解壓
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http,server,location
調(diào)試壓縮比例熙掺。級(jí)別
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
版本號(hào)
擴(kuò)展Nginx壓縮模塊
- http_gzip_static_module -預(yù)讀gzip功能
Syntax: gzip_static on | off | always;
Default:
gzip_static off;
Context: http, server, location
先會(huì)去磁盤查找是否有已經(jīng)壓縮過的文件未斑,再確定要不要進(jìn)行壓縮操作。如果磁盤有币绩,直接就發(fā)送了壓縮文件給客戶端蜡秽。
- http_gunzip_module -應(yīng)用支持gunzip的壓縮方式(不多用)
實(shí)戰(zhàn):
配置文件(注釋gzip功能)
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ .*\.(jpg|gif|png) {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types image/jpeg image/gif image/png;
root /opt/app/code/images;
}
location ~ .*\.(txt|xml) {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types image/jpeg image/gif image/png text/javascript text/plain;
root /opt/app/code/doc;
}
location ~ ^/download {
gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
然后我們開啟gzip,并重啟nginx服務(wù)
emmmm我給虛擬機(jī)配置了,請(qǐng)求頭里有g(shù)zip信息缆镣,但是size沒有壓縮芽突。我再找找原因,這里先不放成功后的截圖。
配置語法 -expires(靜態(tài)資源過期設(shè)置)
添加Cache-Control费就、Expires頭,
優(yōu)點(diǎn):可以跟服務(wù)器做實(shí)時(shí)交互
缺點(diǎn):每次都會(huì)訪問服務(wù)器看是否有更新
Syntax: expires [modified] time;
expires epoch | max| off;
Default: expires off;
Context: http,server,location,if in location
跨域訪問
允許跨域訪問
Syntax: add_header Access-Control-Allow-Origin *; #星號(hào)代表所有ip都能跨域
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; #可以跨域的方法
Default: --
Context: http,server,location,in in location
防盜鏈--防止資源被盜用-http_refer
首先诉瓦,最主要的是區(qū)別那些請(qǐng)求是非正常的用戶請(qǐng)求
valid_referers none blocked server_names
*.example.com example.* www.example.org/galleries/
~\.google\.;
if ($invalid_referer) {
return 403;
}
http://nginx.org/en/docs/http/ngx_http_referer_module.html
二、代理服務(wù)
正向代理
- 對(duì)象是客戶端
反向代理
- 對(duì)象是服務(wù)端
location / {
proxy_pass http://localhost:8000; #將localhost的8080端口代理到監(jiān)聽的server
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
其他配置語法-緩沖區(qū)
Syntax: proxy_buffering on | off;
Default: proxy_buffering on;
Context: http, server, location
其他配置語法-跳轉(zhuǎn)重定向
Syntax: proxy_redirect default;proxy_redirect off;proxy_redirect redirect replacement;
Default: proxy_redirect default;
Context: http, server, location
其他配置語法-頭信息
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host;proxy_set_header Connection close;
Context: http, server, location
其他配置語法-超時(shí)
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
實(shí)戰(zhàn):
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real_IP $remote_addr;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 32k;
proxy_buffering on;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
三力细、負(fù)載均衡調(diào)度器SLB
upstream dynamic {
zone upstream_dynamic 64k;
hash $request_uri; #采用hash策略,獲取uri固额,確保以下操作在同一服務(wù)器
server backend1.example.com weight=5;
server backend2.example.com:8080 fail_timeout=5s slow_start=30s; #max_fail失敗后服務(wù)暫停的時(shí)間
server 192.0.2.1 max_fails=3; #允許請(qǐng)求失敗次數(shù)
server backend3.example.com resolve;
server backend4.example.com service=http resolve;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup; #備用服務(wù)器
}
server {
location / {
proxy_pass http://dynamic;
health_check;
}
}
策略 | 介紹 |
---|---|
輪詢 | 按時(shí)間順序逐一分配到不通的后端服務(wù)器 |
加權(quán)輪詢 | weight值越大眠蚂,分配到的訪問幾率越高 |
ip_hash | 每個(gè)請(qǐng)求按訪問IP的hash結(jié)果分配,這樣癩子同一個(gè)IP的固定訪問一個(gè)溝段服務(wù)器 |
least_conn | 最少連接數(shù)斗躏,哪個(gè)機(jī)器連接數(shù)少就分發(fā) |
url_hash | 按照訪問的URL的hash結(jié)果來分配氫氣逝慧,是每個(gè)URL丁香到同一個(gè)后端服務(wù)器 |
hash關(guān)鍵數(shù)值 | hash自定義的key |
只有最后一個(gè)hash關(guān)鍵數(shù)值策略能保證用戶訪問后,一直停留在該服務(wù)器。
四笛臣、動(dòng)態(tài)緩存
客戶端緩存
- 瀏覽器緩存
代理緩存nginx
配置語法-proxy_cache
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
配置語法-緩存路徑
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
配置語法-部分頁面不緩存
Syntax: proxy_no_cache string ...; #string 是
Default: —
Context: http, server, location
實(shí)戰(zhàn):
upstream fantj{
server 192.168.0.1:8081;
server 192.168.0.1:8082;
server 192.168.0.1:8083;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=fantj_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost fantj.com;
......
location / {
proxy_cache fantj_cache;
proxy_pass http://fantj;
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;
include my_proxy_params; #讀取自定義的proxy配置文件
}
}
如何清理指定緩存
- rm -rf 緩存目錄
- 第三方模塊
ngx_cache_purge