nginx cache test.md

Nginx Cache 简要配置

# 使用 CentOS 7 作为基础镜像
FROM centos:7

# 安装依赖
RUN yum -y update && \
    yum -y install epel-release && \
    yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel

# 下载 Nginx 和 ngx_cache_purge 模块
RUN curl -O http://nginx.org/download/nginx-1.18.0.tar.gz && \
    tar -zxvf nginx-1.18.0.tar.gz && \
    curl -L https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz | tar zx

RUN groupadd -r nginx && \
    useradd -r -g nginx -d /var/cache/nginx -s /sbin/nologin nginx


# 编译 Nginx 与 ngx_cache_purge 模块
RUN cd nginx-1.18.0 && \
    ./configure --with-http_ssl_module  		--user=nginx \
                                        		--group=nginx \
     --add-module=../ngx_cache_purge-2.3 && \
    make && make install


# 设置环境变量,使 nginx 命令可用
ENV PATH="/usr/local/nginx/sbin:$PATH"

# 清理安装包和缓存
RUN yum clean all && \
    rm -rf /nginx-1.18.0.tar.gz /nginx-1.18.0 /ngx_cache_purge-2.3.tar.gz /ngx_cache_purge-2.3

# 暴露 80 端口
EXPOSE 80

RUN mkdir -p /data/nginx/cache

COPY DockerfileNcnginx.conf /usr/local/nginx/conf/nginx.conf

# 使用 Nginx 前台运行
CMD ["nginx", "-g", "daemon off;"]

DockerfileNcnginx.conf

user nginx nginx;
# worker_processes  auto;
worker_processes  1;

# error_log  /var/log/nginx/error.log warn;
pid        /var/run/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

	upstream my_backend {
                server www.test.com;
	}
    #gzip  on;

    proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen       80;
        server_name  localhost;
      location ~/purge_cache(/.*) {
    #             allow 127.0.0.1;
    #             deny all;
                proxy_cache_purge my_cache $1;

                add_header X-Cache-xxxxxxxxxxxxx  $1 always;
        }
        location /bbs/ {
            proxy_pass http://my_backend;
            proxy_set_header Host "www.test.com";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_key $uri;
            proxy_cache my_cache;
            proxy_cache_revalidate on;
            proxy_cache_min_uses 1;
            proxy_cache_use_stale error timeout updating;
            proxy_cache_lock on;

# 忽略后端的 Cache-Control 和 Expires 头部
            proxy_ignore_headers Cache-Control Expires;

            # 无论后端说什么,都缓存所有200状态码的响应60分钟
            proxy_cache_valid 200 60m;
            # 添加此行来显示缓存状态
            add_header X-Cache-Status11 $upstream_cache_status always;
            add_header X-Cache-DDDDD  $uri always;
        }



        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}


posted @ 2024-04-29 13:24  张保维  阅读(3)  评论(0编辑  收藏  举报