最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Nginx配置缓存 多域名时分域名配置缓存
时间:2022-06-30 18:41:46 编辑:袖梨 来源:一聚教程网
相信很多人有已经从apache之类的web服务器转到Nginx了,然而Nginx除了单纯的web服务器用途外,可以当做反向代理服务器使用,配合proxy_cache功能基本可以代替squid了。
1. 编译安装Nginx
./configure --user=www --group=www --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
2. nginx.conf配置文件如下:
user www www;
worker_processes 8;
error_log /opt/nginx/logs/nginx_error.log crit;
pid /opt/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /data/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name *.yourdomain.com;
index index.html index.htm;
root /data/htdocs/www;
location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log off;
}
}
3. 接下来启动nginx吧
/opt/nginx/sbin/nginx
4. 能否将缓存按照域名分文件夹存放?
由于我们的运营的是一个网站集群,所以子站是通过域名泛解析解析到本机的,所以希望能否按照域名存放,便于整站清理缓存。比如:a.youdomain.com/a/b3/…. ,b.youdomain.com/b/ae/….,这样的目录结构。但是研究了很久nginx配置方式,并没有发现相关的设置。于是想到自己来改写nginx源代码,涉及到的文件是src/http/ngx_http_file_cache.c文件。
以下函数的改写是基于 nginx0.8.55的。
ngx_http_file_cache_name函数改写如下:
static ngx_int_t
ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
{
u_char *p;
ngx_http_cache_t *c;
size_t pathlen;
c = r->cache;
//added by Lingter
pathlen = path->name.len;
ngx_http_file_cache_domain(r,path);
c->file.name.len = path->name.len + 1 + path->len
+ 2 * NGX_HTTP_CACHE_KEY_LEN;
c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
if (c->file.name.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
p = c->file.name.data + path->name.len + 1 + path->len;
p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
*p = '\0';
ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"cache file: \"%s\"", c->file.name.data);
path->name.len = pathlen;
return NGX_OK;
}
上一段代码中加入了一个新的函数ngx_http_file_cache_domain的调用。
先申明一下函数定义:
static ngx_int_t ngx_http_file_cache_domain(ngx_http_request_t *r,
ngx_path_t *path);
具体函数内容:
//added by Lingter
static ngx_int_t ngx_http_file_cache_domain(ngx_http_request_t *r,ngx_path_t *path)
{
ngx_http_cache_t *c;
u_char *pathname,*pos;
size_t i;
ngx_str_t *key,domain;
c = r->cache;
key = c->keys.elts;
domain.len = key[0].len;
domain.data = ngx_pstrdup(r->pool,& key[0]);
pos = (u_char *) ngx_strchr(domain.data,'/');
if(pos){
*pos = '\0';
domain.len = ngx_strlen(domain.data);
pathname = path->name.data;
i = path->name.len+1;
pathname[i-1] = '/';
ngx_memcpy(&pathname[i],domain.data,domain.len);
path->name.len = i + domain.len;
}
return NGX_OK;
}
实际上上面的代码仅仅是找出key中的第一个出现字符’/'的地方,并且将/前的字符串作为域名拼接到缓存路径里去。
由于本人不会C语言,所以写得比较粗糙,但是基本实现了这一需求。
看看最终的成果吧:
最后补充全部代码
/*
* Copyright (C) Igor Sysoev
*/
#include
#include
#include
#include
static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
ngx_http_cache_t *c);
static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r,
ngx_http_cache_t *c);
#if (NGX_HAVE_FILE_AIO)
static void ngx_http_cache_aio_event_handler(ngx_event_t *ev);
#endif
static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache,
ngx_http_cache_t *c);
static ngx_int_t ngx_http_file_cache_domain(ngx_http_request_t *r,
ngx_path_t *path);
static ngx_int_t ngx_http_file_cache_name(ngx_http_request_t *r,
ngx_path_t *path);
static ngx_http_file_cache_node_t *
ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
static void ngx_http_file_cache_cleanup(void *data);
static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
ngx_queue_t *q, u_char *name);
static ngx_int_t
ngx_http_file_cache_manager_sleep(ngx_http_file_cache_t *cache);
static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
ngx_str_t *path);
static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
ngx_str_t *path);
static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
ngx_str_t *path);
static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
ngx_http_cache_t *c);
static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
ngx_str_t *path);
ngx_str_t ngx_http_cache_status[] = {
ngx_string("MISS"),
ngx_string("BYPASS"),
ngx_string("EXPIRED"),
ngx_string("STALE"),
ngx_string("UPDATING"),
ngx_string("HIT")
};
static u_char ngx_http_file_cache_key[] = { LF, 'K', 'E', 'Y', ':', ' ' };
static ngx_int_t
ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data)
{
ngx_http_file_cache_t *ocache = data;
size_t len;
ngx_uint_t n;
ngx_http_file_cache_t *cache;
cache = shm_zone->data;
if (ocache) {
if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) {
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
"cache "%V" uses the "%V" cache path "
"while previously it used the "%V" cache path",
&shm_zone->shm.name, &cache->path->name,
&ocache->path->name);
return NGX_ERROR;
}
for (n = 0; n < 3; n++) {
if (cache->path->level[n] != ocache->path->level[n]) {
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
"cache "%V" had previously different levels",
&shm_zone->shm.name);
return NGX_ERROR;
}
}
cache->sh = ocache->sh;
cache->shpool = ocache->shpool;
cache->bsize = ocache->bsize;
cache->max_size /= cache->bsize;
if (!cache->sh->cold || cache->sh->loading) {
cache->path->loader = NULL;
}
return NGX_OK;
}
cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
if (shm_zone->shm.exists) {
cache->sh = cache->shpool->data;
cache->bsize = ngx_fs_bsize(cache->path->name.data);
return NGX_OK;
}
cache->sh = ngx_slab_alloc(cache->shpool, sizeof(ngx_http_file_cache_sh_t));
if (cache->sh == NULL) {
return NGX_ERROR;
}
cache->shpool->data = cache->sh;
ngx_rbtree_init(&cache->sh->rbtree, &cache->sh->sentinel,
ngx_http_file_cache_rbtree_insert_value);
ngx_queue_init(&cache->sh->queue);
cache->sh->cold = 1;
cache->sh->loading = 0;
cache->sh->size = 0;
cache->bsize = ngx_fs_bsize(cache->path->name.data);
cache->max_size /= cache->bsize;
len = sizeof(" in cache keys zone """) + shm_zone->shm.name.len;
cache->shpool->log_ctx = ngx_slab_alloc(cache->shpool, len);
if (cache->shpool->log_ctx == NULL) {
return NGX_ERROR;
}
ngx_sprintf(cache->shpool->log_ctx, " in cache keys zone "%V"%Z",
&shm_zone->shm.name);
return NGX_OK;
}
ngx_int_t
ngx_http_file_cache_new(ngx_http_request_t *r)
{
ngx_http_cache_t *c;
c = ngx_pcalloc(r->pool, sizeof(ngx_http_cache_t));
if (c == NULL) {
return NGX_ERROR;
}
if (ngx_array_init(&c->keys, r->pool, 4, sizeof(ngx_str_t)) != NGX_OK) {
return NGX_ERROR;
}
r->cache = c;
c->file.log = r->connection->log;
c->file.fd = NGX_INVALID_FILE;
return NGX_OK;
}
ngx_int_t
ngx_http_file_cache_create(ngx_http_request_t *r)
{
ngx_http_cache_t *c;
ngx_pool_cleanup_t *cln;
ngx_http_file_cache_t *cache;
ngx_http_file_cache_create_key(r);
c = r->cache;
cache = c->file_cache;
cln = ngx_pool_cleanup_add(r->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
}
if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
return NGX_ERROR;
}
cln->handler = ngx_http_file_cache_cleanup;
cln->data = c;
if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
void
ngx_http_file_cache_create_key(ngx_http_request_t *r)
{
size_t len;
ngx_str_t *key;
ngx_uint_t i;
ngx_md5_t md5;
ngx_http_cache_t *c;
c = r->cache;
len = 0;
ngx_crc32_init(c->crc32);
ngx_md5_init(&md5);
key = c->keys.elts;
for (i = 0; i < c->keys.nelts; i++) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http cache key: "%V"", &key[i]);
len += key[i].len;
ngx_crc32_update(&c->crc32, key[i].data, key[i].len);
ngx_md5_update(&md5, key[i].data, key[i].len);
}
c->header_start = sizeof(ngx_http_file_cache_header_t)
+ sizeof(ngx_http_file_cache_key) + len + 1;
ngx_crc32_final(c->crc32);
ngx_md5_final(c->key, &md5);
}
ngx_int_t
ngx_http_file_cache_open(ngx_http_request_t *r)
{
ngx_int_t rc, rv;
ngx_uint_t cold, test;
ngx_http_cache_t *c;
ngx_pool_cleanup_t *cln;
ngx_open_file_info_t of;
ngx_http_file_cache_t *cache;
ngx_http_core_loc_conf_t *clcf;
c = r->cache;
if (c->buf) {
return ngx_http_file_cache_read(r, c);
}
cache = c->file_cache;
cln = ngx_pool_cleanup_add(r->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
}
rc = ngx_http_file_cache_exists(cache, c);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache exists: %i e:%d", rc, c->exists);
if (rc == NGX_ERROR) {
return rc;
}
cln->handler = ngx_http_file_cache_cleanup;
cln->data = c;
if (rc == NGX_AGAIN) {
return NGX_HTTP_CACHE_SCARCE;
}
cold = cache->sh->cold;
if (rc == NGX_OK) {
if (c->error) {
return c->error;
}
c->temp_file = 1;
test = c->exists ? 1 : 0;
rv = NGX_DECLINED;
} else { /* rc == NGX_DECLINED */
if (c->min_uses > 1) {
if (!cold) {
return NGX_HTTP_CACHE_SCARCE;
}
test = 1;
rv = NGX_HTTP_CACHE_SCARCE;
} else {
c->temp_file = 1;
test = cold ? 1 : 0;
rv = NGX_DECLINED;
}
}
if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
return NGX_ERROR;
}
if (!test) {
return NGX_DECLINED;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_memzero(&of, sizeof(ngx_open_file_info_t));
of.uniq = c->uniq;
of.valid = clcf->open_file_cache_valid;
of.min_uses = clcf->open_file_cache_min_uses;
of.events = clcf->open_file_cache_events;
of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
of.read_ahead = clcf->read_ahead;
if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
!= NGX_OK)
{
switch (of.err) {
case 0:
return NGX_ERROR;
case NGX_ENOENT:
case NGX_ENOTDIR:
return rv;
default:
ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
ngx_open_file_n " "%s" failed", c->file.name.data);
return NGX_ERROR;
}
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache fd: %d", of.fd);
c->file.fd = of.fd;
c->file.log = r->connection->log;
c->uniq = of.uniq;
c->length = of.size;
c->buf = ngx_create_temp_buf(r->pool, c->body_start);
if (c->buf == NULL) {
return NGX_ERROR;
}
return ngx_http_file_cache_read(r, c);
}
static ngx_int_t
ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
{
time_t now;
ssize_t n;
ngx_int_t rc;
ngx_http_file_cache_t *cache;
ngx_http_file_cache_header_t *h;
n = ngx_http_file_cache_aio_read(r, c);
if (n < 0) {
return n;
}
if ((size_t) n < c->header_start) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
"cache file "%s" is too small", c->file.name.data);
return NGX_ERROR;
}
h = (ngx_http_file_cache_header_t *) c->buf->pos;
if (h->crc32 != c->crc32) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
"cache file "%s" has md5 collision", c->file.name.data);
return NGX_DECLINED;
}
c->buf->last += n;
c->valid_sec = h->valid_sec;
c->last_modified = h->last_modified;
c->date = h->date;
c->valid_msec = h->valid_msec;
c->header_start = h->header_start;
c->body_start = h->body_start;
r->cached = 1;
cache = c->file_cache;
if (cache->sh->cold) {
ngx_shmtx_lock(&cache->shpool->mutex);
if (!c->node->exists) {
c->node->uses = 1;
c->node->body_start = c->body_start;
c->node->exists = 1;
c->node->uniq = c->uniq;
cache->sh->size += (c->length + cache->bsize - 1) / cache->bsize;
}
ngx_shmtx_unlock(&cache->shpool->mutex);
}
now = ngx_time();
if (c->valid_sec < now) {
ngx_shmtx_lock(&cache->shpool->mutex);
if (c->node->updating) {
rc = NGX_HTTP_CACHE_UPDATING;
} else {
c->node->updating = 1;
c->updating = 1;
rc = NGX_HTTP_CACHE_STALE;
}
ngx_shmtx_unlock(&cache->shpool->mutex);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache expired: %i %T %T",
rc, c->valid_sec, now);
return rc;
}
return NGX_OK;
}
static ssize_t
ngx_http_file_cache_aio_read(ngx_http_request_t *r, ngx_http_cache_t *c)
{
#if (NGX_HAVE_FILE_AIO)
ssize_t n;
ngx_http_core_loc_conf_t *clcf;
if (!ngx_file_aio) {
goto noaio;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (!clcf->aio) {
goto noaio;
}
n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
if (n != NGX_AGAIN) {
return n;
}
c->file.aio->data = r;
c->file.aio->handler = ngx_http_cache_aio_event_handler;
r->main->blocked++;
r->aio = 1;
return NGX_AGAIN;
noaio:
#endif
return ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
}
#if (NGX_HAVE_FILE_AIO)
static void
ngx_http_cache_aio_event_handler(ngx_event_t *ev)
{
ngx_event_aio_t *aio;
ngx_http_request_t *r;
aio = ev->data;
r = aio->data;
r->main->blocked--;
r->aio = 0;
r->connection->write->handler(r->connection->write);
}
#endif
static ngx_int_t
ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
{
ngx_int_t rc;
ngx_http_file_cache_node_t *fcn;
ngx_shmtx_lock(&cache->shpool->mutex);
fcn = ngx_http_file_cache_lookup(cache, c->key);
if (fcn) {
ngx_queue_remove(&fcn->queue);
fcn->uses++;
fcn->count++;
if (fcn->error) {
if (fcn->valid_sec < ngx_time()) {
goto renew;
}
rc = NGX_OK;
goto done;
}
if (fcn->exists) {
c->exists = fcn->exists;
c->body_start = fcn->body_start;
rc = NGX_OK;
goto done;
}
if (fcn->uses >= c->min_uses) {
c->exists = fcn->exists;
c->body_start = fcn->body_start;
rc = NGX_OK;
} else {
rc = NGX_AGAIN;
}
goto done;
}
fcn = ngx_slab_alloc_locked(cache->shpool,
sizeof(ngx_http_file_cache_node_t));
if (fcn == NULL) {
ngx_shmtx_unlock(&cache->shpool->mutex);
(void) ngx_http_file_cache_forced_expire(cache);
ngx_shmtx_lock(&cache->shpool->mutex);
fcn = ngx_slab_alloc_locked(cache->shpool,
sizeof(ngx_http_file_cache_node_t));
if (fcn == NULL) {
rc = NGX_ERROR;
goto failed;
}
}
ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
fcn->uses = 1;
fcn->count = 1;
fcn->updating = 0;
fcn->deleting = 0;
renew:
rc = NGX_DECLINED;
fcn->valid_msec = 0;
fcn->error = 0;
fcn->exists = 0;
fcn->valid_sec = 0;
fcn->uniq = 0;
fcn->body_start = 0;
fcn->length = 0;
done:
fcn->expire = ngx_time() + cache->inactive;
ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
c->uniq = fcn->uniq;
c->error = fcn->error;
c->node = fcn;
failed:
ngx_shmtx_unlock(&cache->shpool->mutex);
return rc;
}
static ngx_int_t ngx_http_file_cache_domain(ngx_http_request_t *r,ngx_path_t *path)
{
ngx_http_cache_t *c;
u_char *pathname,*pos;
size_t i;
ngx_str_t *key,domain;
c = r->cache;
key = c->keys.elts;
domain.len = key[0].len;
domain.data = ngx_pstrdup(r->pool,& key[0]);
pos = (u_char *) ngx_strchr(domain.data,'/');
if(pos){
*pos = '';
domain.len = ngx_strlen(domain.data);
pathname = path->name.data;
i = path->name.len+1;
pathname[i-1] = '/';
ngx_memcpy(&pathname[i],domain.data,domain.len);
path->name.len = i + domain.len;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
{
u_char *p;
ngx_http_cache_t *c;
size_t pathlen;
c = r->cache;
pathlen = path->name.len;
ngx_http_file_cache_domain(r,path);
c->file.name.len = path->name.len + 1 + path->len
+ 2 * NGX_HTTP_CACHE_KEY_LEN;
c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
if (c->file.name.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
p = c->file.name.data + path->name.len + 1 + path->len;
p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
*p = '';
ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"cache file: "%s"", c->file.name.data);
path->name.len = pathlen;
return NGX_OK;
}
static ngx_http_file_cache_node_t *
ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
{
ngx_int_t rc;
ngx_rbtree_key_t node_key;
ngx_rbtree_node_t *node, *sentinel;
ngx_http_file_cache_node_t *fcn;
ngx_memcpy((u_char *) &node_key, key, sizeof(ngx_rbtree_key_t));
node = cache->sh->rbtree.root;
sentinel = cache->sh->rbtree.sentinel;
while (node != sentinel) {
if (node_key < node->key) {
node = node->left;
continue;
}
if (node_key > node->key) {
node = node->right;
continue;
}
/* node_key == node->key */
do {
fcn = (ngx_http_file_cache_node_t *) node;
rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
if (rc == 0) {
return fcn;
}
node = (rc < 0) ? node->left : node->right;
} while (node != sentinel && node_key == node->key);
break;
}
/* not found */
return NULL;
}
static void
ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
{
ngx_rbtree_node_t **p;
ngx_http_file_cache_node_t *cn, *cnt;
for ( ;; ) {
if (node->key < temp->key) {
p = &temp->left;
} else if (node->key > temp->key) {
p = &temp->right;
} else { /* node->key == temp->key */
cn = (ngx_http_file_cache_node_t *) node;
cnt = (ngx_http_file_cache_node_t *) temp;
p = (ngx_memcmp(cn->key, cnt->key,
NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t))
< 0)
? &temp->left : &temp->right;
}
if (*p == sentinel) {
break;
}
temp = *p;
}
*p = node;
node->parent = temp;
node->left = sentinel;
node->right = sentinel;
ngx_rbt_red(node);
}
void
ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
{
ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf;
u_char *p;
ngx_str_t *key;
ngx_uint_t i;
ngx_http_cache_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache set header");
c = r->cache;
h->valid_sec = c->valid_sec;
h->last_modified = c->last_modified;
h->date = c->date;
h->crc32 = c->crc32;
h->valid_msec = (u_short) c->valid_msec;
h->header_start = (u_short) c->header_start;
h->body_start = (u_short) c->body_start;
p = buf + sizeof(ngx_http_file_cache_header_t);
p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
key = c->keys.elts;
for (i = 0; i < c->keys.nelts; i++) {
p = ngx_copy(p, key[i].data, key[i].len);
}
*p = LF;
}
void
ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
{
off_t size, length;
ngx_int_t rc;
ngx_file_uniq_t uniq;
ngx_file_info_t fi;
ngx_http_cache_t *c;
ngx_ext_rename_file_t ext;
ngx_http_file_cache_t *cache;
c = r->cache;
if (c->updated) {
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache update");
c->updated = 1;
c->updating = 0;
cache = c->file_cache;
uniq = 0;
length = 0;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache rename: "%s" to "%s"",
tf->file.name.data, c->file.name.data);
ext.access = NGX_FILE_OWNER_ACCESS;
ext.path_access = NGX_FILE_OWNER_ACCESS;
ext.time = -1;
ext.create_path = 1;
ext.delete_file = 1;
ext.log = r->connection->log;
rc = ngx_ext_rename_file(&tf->file.name, &c->file.name, &ext);
if (rc == NGX_OK) {
if (ngx_fd_info(tf->file.fd, &fi) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
ngx_fd_info_n " "%s" failed", tf->file.name.data);
rc = NGX_ERROR;
} else {
uniq = ngx_file_uniq(&fi);
length = ngx_file_size(&fi);
}
}
size = (length + cache->bsize - 1) / cache->bsize;
ngx_shmtx_lock(&cache->shpool->mutex);
c->node->count--;
c->node->uniq = uniq;
c->node->body_start = c->body_start;
size = size - (c->node->length + cache->bsize - 1) / cache->bsize;
c->node->length = length;
cache->sh->size += size;
if (rc == NGX_OK) {
c->node->exists = 1;
}
c->node->updating = 0;
ngx_shmtx_unlock(&cache->shpool->mutex);
}
ngx_int_t
ngx_http_cache_send(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_cache_t *c;
c = r->cache;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http file cache send: %s", c->file.name.data);
/* we need to allocate all before the header would be sent */
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
if (b->file == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
r->header_only = (c->length - c->body_start) == 0;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
b->file_pos = c->body_start;
b->file_last = c->length;
b->in_file = 1;
b->last_buf = (r == r->main) ? 1: 0;
b->last_in_chain = 1;
b->file->fd = c->file.fd;
b->file->name = c->file.name;
b->file->log = r->connection->log;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
void
ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf)
{
ngx_http_file_cache_t *cache;
ngx_http_file_cache_node_t *fcn;
if (c->updated) {
return;
}
cache = c->file_cache;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
"http file cache free, fd: %d", c->file.fd);
ngx_shmtx_lock(&cache->shpool->mutex);
fcn = c->node;
fcn->count--;
if (c->updating) {
fcn->updating = 0;
}
if (c->error) {
fcn->error = c->error;
if (c->valid_sec) {
fcn->valid_sec = c->valid_sec;
fcn->valid_msec = c->valid_msec;
}
} else if (!fcn->exists && fcn->count == 0 && c->min_uses == 1) {
ngx_queue_remove(&fcn->queue);
ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
ngx_slab_free_locked(cache->shpool, fcn);
c->node = NULL;
}
ngx_shmtx_unlock(&cache->shpool->mutex);
c->updated = 1;
c->updating = 0;
if (c->temp_file) {
if (tf && tf->file.fd != NGX_INVALID_FILE) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
"http file cache incomplete: "%s"",
tf->file.name.data);
if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, c->file.log, ngx_errno,
ngx_delete_file_n " "%s" failed",
tf->file.name.data);
}
}
}
}
static void
ngx_http_file_cache_cleanup(void *data)
{
ngx_http_cache_t *c = data;
if (c->updated) {
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
"http file cache cleanup");
if (c->updating) {
ngx_log_error(NGX_LOG_ALERT, c->file.log, 0,
"stalled cache updating, error:%ui", c->error);
}
ngx_http_file_cache_free(c, NULL);
}
static time_t
ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)
{
u_char *name;
size_t len;
time_t wait;
ngx_uint_t tries;
ngx_path_t *path;
ngx_queue_t *q;
ngx_http_file_cache_node_t *fcn;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"http file cache forced expire");
path = cache->path;
len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
name = ngx_alloc(len + 1, ngx_cycle->log);
if (name == NULL) {
return 10;
}
ngx_memcpy(name, path->name.data, path->name.len);
wait = 10;
tries = 20;
ngx_shmtx_lock(&cache->shpool->mutex);
for (q = ngx_queue_last(&cache->sh->queue);
q != ngx_queue_sentinel(&cache->sh->queue);
q = ngx_queue_prev(q))
{
fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",
fcn->count, fcn->exists,
fcn->key[0], fcn->key[1], fcn->key[2], f
相关文章
- 《绝区零》伊芙琳培养材料汇总 01-24
- 《无限暖暖》1.2春节兑换码一览 01-24
- 《网上国网》查询阶梯档位方法 01-24
- 《蛋仔派对》神游贺岁盲盒获取方法 01-24
- 《炉石传说》星际联动盗贼卡组玩法介绍 01-24
- 皮革珊瑚属于珊瑚中的 01-24