Nginx在接收代理服务器的数据后,会根据cache的配置将这些数据缓存到本地硬盘。当客户端下次访问相同的数据,Nginx服务器将直接从硬盘检索到响应的数据返回给用户,从而减少与被代理服务器交互的时间。
附上自己的 nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8388;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#指定缓存位置、缓存名称、内存中缓存内容元数据信息大小限制、缓存总大小限制。缓存位置是一个目录应该先创建好,nginx并不会帮我们创建这个缓存目录
#loader_threshold - 迭代的持续时间,以毫秒为单位(默认为200)
#loader_files - 在一次迭代期间加载的最大项目数(默认为100)
#loader_sleeps - 迭代之间的延迟(以毫秒为单位)(默认为50)
#max_size-设置缓存数据上限
#purger=on -该参数表示永久的遍历所有缓存条目,并删除与通配符相匹配的条目
#inactive=5m inactive的时间表示一个文件在指定时间内没有被访问过,就从存储系统中移除,不管你proxy_cache_valid里设置的时间是多少
proxy_cache_path D://windownginx/nginx-1.17.9/conf/cache keys_zone=one:10m inactive=5m max_size=10g loader_threshold=300 loader_files=200;
#设定负载均衡服务器列表
upstream proxy.server{
#后端服务器访问规则
#ip_hash;
#weight参数表示权重值,权值越高被分配到的几率越大
#server 10.11.12.116:80 weight=5;
#PC_Local
#server 10.11.12.116:80;
#PC_Server
#server 10.11.12.112:80;
#Notebook
#server 10.11.12.106:80;
server 127.0.0.1:9097;
}
server {
listen 9999 ssl;
#拦截域名以abc开头的 比如 https://abc.com:9999/mysql/getUserData
server_name abc.*;
# server_name LJ.com;
#缓存的名称 和proxy_cache_path 中one一样
proxy_cache one;
ssl_certificate test.crt; #这个是证书的crt文件所在目录
ssl_certificate_key test.key; #这个是证书key文件所在目录
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
#root html;
#index index.html index.htm;
#与upstream关联,定义转发后端负载服务器组
proxy_pass http://proxy.server;
#固定写法-------------
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_set_header X-Forwarded-Port $server_port;
#缓存自定义key
proxy_cache_key “hosthosthostrequest_uri$cookie_user”;
#指定请求至少被发送了多少次以上时才缓存,可以防止低频请求被缓存
proxy_cache_min_uses 5;
#指定哪些方法的请求被缓存
proxy_cache_methods GET HEAD POST;
#响应状态码为200 302时,缓存2分钟有效,两分钟内请求不管请求变没变数据任然是之前的
#proxy_cache_valid any 5m;如果想为所有状态码定义相同缓存时间,就可以使用any作为第一个参数
proxy_cache_valid 200 302 2m;
}
}
}