Nginx配置模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx配置模块相关的知识,希望对你有一定的参考价值。

参考技术A nginx: [emerg] the ``"ssl" parameter requires ngx_http_ssl_module ``in /usr/local/nginx/conf/nginx.conf:37

切换到源码包:

cd /usr/local/nginx-1.11.3

查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V

在configure arguments:后面显示的原有的configure参数如下:
--prefix=/usr/local/nginx --with-http_stub_status_module

那么我们的新配置信息就应该这样写:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
运行上面的命令即可,等配置完

配置完成后,运行命令

make

这里不要进行make install,否则就是覆盖安装
备份原有已安装好的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态

cp ./objs/nginx /usr/local/nginx/sbin/
然后启动nginx,仍可以通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V

配置http请求转成https

Nginx进阶学习模块配置记录

本章目录

  • 0x00 编译参数一览

  • 0x01 多配置文件

  • 0x02 模块使用

  • 0x03 进阶配置

    • 1.虚拟主机配置

    • 2.缓存配置

0x00 编译参数一览

描述:Nginx编译参数不是功能加的越多越好,应该尽可能少编译模块不用的最好不要加入

安装Nginx查看编译参数

$nginx -V
nginx version: nginx/1.17.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx/1.17.9 --user=nginx --group=nginx --with-http_stub_status_module

Nginx编译参数(./configure)

$ ./configure --help  #查看编译参数

#常用选项
--prefix= #指向安装目录
--user= #指定程序运行时的非特权用户
--group= #指定程序运行时的非特权用户组
--with-perl= #设定perl库文件路径启用pcre库
--with-perl_modules_path= #设定perl模块路径
--with-pcre-opt= 在编译时为pcre库设置附加参数

--with-zlib= #指向zlib库目录
--with-zlib-opt= #在编译时为zlib设置附加参数
--with-zlib-asm= #为指定的CPU使用zlib汇编源进行优化,CPU类型为pentium, pentiumpro

--with-openssl= #指向openssl安装目录
--with-openssl-opt #在编译时为openssl设置附加参数
--with-http_ssl_module #启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)

--with-cc-opt= #设置C编译器参数将被添加到CFLAGS变量(PCRE库,需要指定–with-cc-opt=”-I /usr/local/include”,如果使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc- opt=”-D FD_SETSIZE=2048”指定。)
--with-ld-opt= #设置连接文件参数链接系统库。(PCRE库,需要指定–with-ld-opt=”-L /usr/local/lib”。)
--with-cpu-opt= #指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64


#下面按需配置
--conf-path= #指向配置文件(nginx.conf)
--error-log-path= #指向错误日志目录
--pid-path= #指向pid文件(nginx.pid)
--sbin-path= #指向(执行)程序文件(nginx)
--lock-path= #指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--builddir= #指向编译目录

#模块添加
--with-rtsig_module #启用rtsig模块支持(实时信号)
--with-select_module #启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:–without-select_module
--with-poll_module #启用poll模块支持(功能与select相同,与select特性相同,为一种轮询模式,不推荐在高载环境下使用)
--with-file-aio #启用file aio支持(一种APL文件传输格式)
--with-ipv6 #启用ipv6支持
--with-http_realip_module #启用ngx_http_realip_module支持(这个模块允许从请求标头更改客户端的IP地址值,默认为关)
--with-http_addition_module #启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_xslt_module #启用ngx_http_xslt_module支持(过滤转换XML请求)
--with-http_image_filter_module #启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用、gd库要用到)
--with-http_geoip_module #启用ngx_http_geoip_module支持(该模块创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量)
--with-http_sub_module #启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_dav_module #启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
--with-http_flv_module #启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_gzip_static_module #启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--with-http_random_index_module #启用ngx_http_random_index_module支持(从目录中随机挑选一个目录索引)
--with-http_secure_link_module #启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接网址)
--with-http_degradation_module #启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码)
--with-http_stub_status_module #启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)

--without-http_charset_module #禁用ngx_http_charset_module支持(重新编码web页面,但只能是一个方向–服务器端到客户端,并且只有一个字节的编码可以被重新编码)
--without-http_gzip_module #禁用ngx_http_gzip_module支持(该模块同-with-http_gzip_static_module功能一样)

--without-http_ssi_module #禁用ngx_http_ssi_module支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持SSI命令的列表是不完整的)

--without-http_userid_module #禁用ngx_http_userid_module支持(该模块用来处理用来确定客户端后续请求的cookies)

--without-http_access_module #禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于ip地址)

--without-http_auth_basic_module #禁用ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于http基本认证方法来保护你的站点或其部分内容)

--without-http_autoindex_module #禁用disable ngx_http_autoindex_module支持(该模块用于自动生成目录列表,只在ngx_http_index_module模块未找到索引文件时发出请求。)

--without-http_geo_module #禁用ngx_http_geo_module支持(创建一些变量,其值依赖于客户端的IP地址)

--without-http_map_module #禁用ngx_http_map_module支持(使用任意的键/值对设置配置变量)

--without-http_split_clients_module #禁用ngx_http_split_clients_module支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等)

--without-http_referer_module #禁用disable ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中Referer值不正确的请求)

--without-http_rewrite_module #禁用ngx_http_rewrite_module支持(该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级 别设置该选项,那么他们将在 location之前生效。如果在location还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为 location部分的规则造成的,那么 location部分会再次被执行作为新的URI。这个循环会执行10次,然后Nginx会返回一个500错误。)

--without-http_proxy_module #禁用ngx_http_proxy_module支持(有关代理服务器)

--without-http_fastcgi_module #禁用ngx_http_fastcgi_module支持(该模块允许Nginx 与FastCGI 进程交互,并通过传递参数来控制FastCGI 进程工作。)FastCGI一个常驻型的公共网关接口。

--without-http_uwsgi_module #禁用ngx_http_uwsgi_module支持(该模块用来医用uwsgi协议,uWSGI服务器相关)

--without-http_scgi_module #禁用ngx_http_scgi_module支持(该模块用来启用SCGI协议支持,SCGI协议是CGI协议的替代。它是一种应用程序与HTTP服务接口标准。它有些像FastCGI但他的设计 更容易实现。)

--without-http_memcached_module #禁用ngx_http_memcached_module支持(该模块用来提供简单的缓存,以提高系统效率)
-without-http_limit_zone_module #禁用ngx_http_limit_zone_module支持(该模块可以针对条件,进行会话的并发连接数控制)
--without-http_limit_req_module #禁用ngx_http_limit_req_module支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件)
--without-http_empty_gif_module #禁用ngx_http_empty_gif_module支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用)
--without-http_browser_module #禁用ngx_http_browser_module支持(该模块用来创建依赖于请求报头的值。如果浏览器为modern ,则$modern_browser等于modern_browser_value指令分配的值;如 果浏览器为old,则$ancient_browser等于 ancient_browser_value指令分配的值;如果浏览器为 MSIE中的任意版本,则 $msie等于1)
--without-http_upstream_ip_hash_module #禁用ngx_http_upstream_ip_hash_module支持(该模块用于简单的负载均衡)
--with-http_perl_module #启用ngx_http_perl_module支持(该模块使nginx可以直接使用perl或通过ssi调用perl)


--http-log-path= 设定access log路径
--http-client-body-temp-path= 设定http客户端请求临时文件路径
--http-proxy-temp-path= 设定http代理临时文件路径
--http-fastcgi-temp-path= 设定http fastcgi临时文件路径
--http-uwsgi-temp-path= 设定http uwsgi临时文件路径
--http-scgi-temp-path= 设定http scgi临时文件路径

-without-http #禁用http server功能
--without-http-cache #禁用http cache功能

--with-mail #启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module 启用ngx_mail_ssl_module支持
--without-mail_pop3_module #禁用pop3协议(POP3即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。是因特网电子邮件的第一个离 线协议标 准,POP3协议允许用户从服务器上把邮件存储到本地主机上,同时根据客户端的操作删除或保存在邮件服务器上的邮件。POP3协议是TCP/IP协议族中 的一员,主要用于 支持使用客户端远程管理在服务器上的电子邮件)
--without-mail_imap_module #禁用imap协议(一种邮件获取协议。它的主要作用是邮件客户端可以通过这种协议从邮件服务器上获取邮件的信息,下载邮件等。IMAP协议运行在 TCP/IP协议之上, 使用的端口是143。它与POP3协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。)
--without-mail_smtp_module #禁用smtp协议(SMTP即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于 TCP/IP协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。)
--with-google_perftools_module #启用ngx_google_perftools_module支持(调试用,剖析程序性能瓶颈)
--with-cpp_test_module #启用ngx_cpp_test_module支持

--add-module= #启用外部模块支持

--with-cc= 指向C编译器路径
--with-cpp= 指向C预处理路径

--without-pcre #禁用pcre库

--with-md5= 指向md5库文件目录(消息摘要算法第五版,用以提供消息的完整性保护)
--with-md5-opt= 在编译时为md5库设置附加参数
--with-md5-asm 使用md5汇编源

--with-sha1= 指向sha1库目录(数字签名算法,主要用于数字签名)
--with-sha1-opt= 在编译时为sha1库设置附加参数
--with-sha1-asm 使用sha1汇编源

--with-libatomic 为原子内存的更新操作的实现提供一个架构
--with-libatomic= 指向libatomic_ops安装目录

--with-debug 启用debug日志

0x01 多配置文件

描述:Nginx配置文件中支持包含多个配置文件,比如下面的虚拟主机的配置将使用,在程序加载运行时候首先会读取nginx.conf的配置文件,然后在读取/etc/nginx/conf.d/下面的配置文件;

需要在/etc/nginx/nginx.conf进行设置


0x02 模块使用

官方模块使用手册:http://nginx.org/en/docs/

ngx_http_log_module
nginx配置文件中记录访问日志和错误日志:

Syntax: 	access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except


Syntax: log_format name [escape=default|json|none] string ...;
Default:
log_format combined "...";
Context: http

#日志记录等级
* main
* info
* warn
* 自定义等级

#基础示例;
error_log /var/log/nginx/error.log warn;
access_log /var/log/nginx/access.log main;

#默认情况下,Nginx 的 access log 是不会显示请求时间(request time)的,这需要我们给它的配置文件自定义一下 log format。(可以用来分析 Web Server 的性能)
log_format timed_combined '$remote_addr - $remote_user [$time_local]' ' "$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" $request_time $upstream_response_time';;


$time_local:通用日志格式的本地时间
$request_time:代表 Nginx 从收到客户端请求、到发送完响应的整个时间
$upstream_response_time:在 Nginx 接受完 client 的请求后,再和 upstream server 请求的过程的时间
$bytes_sent:发送给客户端的字节数

access_log /path/to/access.log timed_combined buffer=32k gzip flush=5m; #gizp compression需要zlib 库的支持
187.217.160.191 - - [29/Mar/2020:14:35:49 +0800]"GET / HTTP/1.1" 200 2046 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0" 0.000 0.001


ngx_http_stub_status_module
描述:即在编译时候加入预编译参数--with-http_stub_status_module,它是一个实时统计Nginx Client状态的;

基础语法:

Context: server,location;  #使用的上下文范围
Syntax: stu_status;

使用案例:

server {
...
location /project_status {
stub_status;
}
...
}

使用效果:
Nginx进阶学习模块配置记录


ngx_http_random_index_module
描述:编译选项--with-help_random_index_module在目录中随机选择一个主页;

基础语法:

Context: location;  #使用的上下文范围
Syntax: random_index on|off; #默认off

使用案例:

server {
...
location / {
root /opt/app;
random_index on;
#index index.html index.htm
}
...
}



0x03 进阶配置

1.虚拟主机配置

描述:常规网站编译安装后运行只是单个网站访问,我们如何可以做到多网站同时运行到该Nginx服务器上;

常规有三种方式:


    1. 多端口


    1. 多IP


    1. 多域名:虚拟主机(VirtualHost)

环境准备:

cat >> /etc/hosts<<END
127.0.0.1 v1.weiyigeek.top v2.weiyigeek.top
END

流程步骤:

  • 1.修改移除Nginx.conf配置文件中的Server {...}包含的参数添加include domains/*;;

user  nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
include domains/*;
}
  • 2.在Nginx的Conf目录中建立一个domains目录(注意这里不要建立到上级目录之中,否则会出现nginx master进程已经启动但是无监听端口),进入该目录中建立虚拟主机v1.weiyigeek.top.conf文件

server {
listen 80;
#关键点
server_name v1.weiyigeek.top;
location / {
#关键点
root html/v1.weiyigeek.top;
index index.html index.htm;
}
#Nginx 监控模块启用
location /nginxStatus {
stub_status;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
  • 3.在nginx安装目录中的html中建立一个与虚拟主机同名的文件夹(当然名字您可以任意取,但是必须和v1.weiyigeek.top.conf中指定路与的root一致)

[root@WeiyiGeek html]# pwd
/usr/local/nginx/1.16.1/html
[root@WeiyiGeek html]# ls
50x.html index.html v1.weiyigeek.top v2.weiyigeek.top

Shell脚本一键部署虚拟主机(附上关键性脚本):
完整代码:https://github.com/weiyigeek/SecOpsDev/Application/Web/Nginx/nginxVirtualHost-v1.sh

#!/bin/bash
#@Desc:Nginx多实例部署虚拟主机
#@Author:WeiyiGeek
#@CreatTime:2020年3月8日 12点06分
#@Site:WeiyiGeek.top
#@Test_Linux: Linux weiyigeek 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
#@release:CentOS Linux release 7.4.1708 (Core)

## Define nginx variable
NGX_VER=1.16.1
NGX_URI="http://nginx.org/download/nginx-${NGX_VER}.tar.gz"
NGX_SRC="nginx-${NGX_VER}.tar.gz"
NGX_NAME=${NGX_SRC%.tar.gz}
NGX_DIR="/usr/local/nginx/${NGX_VER}"
NGX_ARGS="--prefix=${NGX_DIR} --user=nginx --group=nginx --with-http_stub_status_module"
NGX_SRCCODE="${NGX_NAME}/src/core/nginx.h"
NGX_VHDIR="${NGX_DIR}/conf/domains"

## Define 防火墙开放端口
FIREWALL_PORT=(80 8080)


## [Nginx INSTALL]
function nginx_install(){
echo -e "e[32m1.核查安装依赖....... e[0m"
CHECK_SOFT=$(rpm -qa | grep -cE "^gcc|^pcre|^zlib")
if [ $CHECK_SOFT -lt 2 ];then yum install -y gcc gcc-c++ pcre pcre-devel zlib-devel;fi

echo -e "e[32m2.检查nginx源码包是否存在....... e[0m"
if [ ! -f $NGX_SRC ];then wget -c $NGX_URI;fi
if [ ! -d $NGX_NAME ];then tar -zxf $NGX_SRC;fi

echo -e "e[32m3.nginx安装陆军是否存在....... e[0m"
if [ ! -f $NGX_DIR/sbin/nginx ];then mkdir -vp $NGX_DIR;fi

echo -e "e[32m3.验证nginx用户是否存在不存在则建立低权限用户....... e[0m"
CHECK_USER=$(getent passwd | grep -wc nginx)
if [ $CHECK_USER -eq 0 ];then useradd -s /sbin/nologin nginx -M; fi

echo -e "安全设置:Nginx版本隐藏......"
sed -i "s/$NGX_VER//g" $NGX_SRCCODE
sed -i 's/nginx//JWS/g' $NGX_SRCCODE
sed -i 's/"NGINX"/"JWS"/g' $NGX_SRCCODE

echo -e "e[32m4.进行nginx预编译及其编译安装....... e[0m"
cd $NGX_NAME && ./configure $NGX_ARGS
if [ $? -eq 0 ];then
#进行2个线程并行编译(可以根据你处理器个数选择)
make -j2 && make -j2 install
else
echo -e "e[31m#Error: 预编译失败!终止安装,请检查软件依赖! e[0m"
exit
fi
if [ $? -ne 0 ];then echo -e "e[31m#Error: 编译安装失败!终止安装 e[0m";exit;fi
echo -e "e[32m Nginx 成功安装....... 安装目录:${NGX_DIR} 正在启动Nginx....e[0m"
$NGX_DIR/sbin/nginx
}

## [Nginx CONFIG]
function nginx_vhost(){
NGX_VHOSTS=$1
firewall_config
cd ${NGX_DIR}
NGX_CNF="${NGX_DIR}/conf/nginx.conf"
if [ ! -f $NGX_CONF ];then echo -e "Nginx-配置文件不存在请仔细检查!";exit;fi
#判断是否已经存在domains配置文件是则不同重新建立;
grep "domains" ${NGX_CNF} >>/dev/null 2>&1
if [ $? -ne 0 ];then
#备份NGX配置文件
cp ${NGX_CNF}{,_$(date +%F_%H%M%S).bak}
mkdir -vp ${NGX_VHDIR}
sed -i "s/#user nobody/user nginx/g" ${NGX_CNF}
sed -i "s/#gzip/gzip/g" ${NGX_CNF}
#去除空行以及注释
grep -vE "#|^$" ${NGX_CNF} > ${NGX_CNF}.swp
#重点删除server字符到文件末尾
sed -i '/server/,$d' ${NGX_CNF}.swp
cp ${NGX_CNF}.swp ${NGX_CNF}
echo -e " include domains/*; }" >> ${NGX_CNF}
fi

cat>${NGX_VHDIR}/$NGX_VHOSTS.conf<<EOF
server {
listen 80;
server_name $NGX_VHOSTS;

location / {
root html/$NGX_VHOSTS;
index index.html index.htm;
}
#Nginx 监控模块启用
location /nginxStatus {
stub_status;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF



echo -e "e[32m$NGX_VHOSTS 网站目录建立之中.....e[0m"
if [ ! -d $NGX_DIR/html/$NGX_VHOSTS/ ];then
mkdir -vp $NGX_DIR/html/$NGX_VHOSTS/
cat>$NGX_DIR/html/$NGX_VHOSTS/index.html<<EOF
<h1>$NGX_VHOSTS Test Pages. </h1>
<p>By WeiyiGeek.top </p>
<hr color=red>
EOF

fi
echo -e "e[32mNginx配置文件验证中.....e[0m"
$NGX_DIR/sbin/nginx -t
if [ $? -ne 0 ];then
echo -e "e[31mNginx配置文件有误,请处理错误后重启Nginx服务器: ${NGX_DIR}/sbin/nginx -s reload"
fi
cat ${NGX_VHDIR}/$NGX_VHOSTS.conf

echo -e "e[32mNginx重启之中.....e[0m"
$NGX_DIR/sbin/nginx -s reload
CHECK_STATUS=$(netstat -tlnp | grep -wc "nginx")
if [ $CHECK_STATUS -ne 0 ];then
echo -e "e[32m#Nginx 启动成功.... e[0m"
else
echo -e "e[31m#Nginx 启动失败.... e[0m"
fi
}

运行效果:

测试效果:


2.缓存配置

首先我们需要了解一下三种缓存类型:

  • 客户端缓存

  • 代理缓存(Proxy Cache)

  • 服务端缓存

这里主要讲解Nginx作为代理服务器进行代理缓存的配置采用的参数以及示例:

  • 1.proxy_cache:定义用于缓存的共享内存区域。同一个区域可以用于多个地方

Syntax: 	proxy_cache zone | off;
Default: proxy_cache off;
Context: http, server, location
  • 2.proxy_cache_path:设置缓存的路径和其他参数缓存数据存储在文件中,缓存中的文件名是将MD5函数应用于缓存键的结果

Context: http
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];

#例如,在下面的配置中level参数定义缓存的层次结构级别:从1到3,每个级别接受值1或2。
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m; #10M = 10 * 8000 个密钥
#/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
  • 3.proxy_cache_valid:设置缓存过期时间

Syntax: 	proxy_cache_valid [code ...] time;
Context: http, server, location

#例如,为不同的响应代码设置缓存时间。
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;

#If only caching time is specified (指定),也可以可以指定any参数来缓存任何响应:
proxy_cache_valid 5m;
proxy_cache_valid any 1m;
  • 4.proxy_cache_key:设置缓存维度

Syntax: 	proxy_cache_key string;
Default: proxy_cache_key $scheme$proxy_host$request_uri;
Context: http, server, location

#例如,定义用于缓存的键,
proxy_cache_key "$host$request_uri $cookie_user";
proxy_cache_key $scheme$proxy_host$uri$is_args$args; #默认情况下指令也是字符串不用加上""
  • 5.proxy_cache_purge:定义将在何种条件下将请求视为缓存清除请求。如果字符串参数中至少有一个值不为空且不等于“0”,则删除具有相应缓存键的缓存项。通过返回204 (No Content)响应来指示操作成功的结果。

Syntax: 	proxy_cache_purge string ...;
Context: http, server, location

# Example configuration:
map $request_method $purge_method {
PURGE 1;
default 0;
}
  • 6.proxy_no_cache:指定不缓存的部分页面,可以与proxy_cache_bypass指令一起使用。

Syntax: 	proxy_no_cache string ...;
Context: http, server, location

#如果字符串参数中至少有一个值不为空且不等于“0”,则不会保存响应:
proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;
proxy_no_cache $http_pragma $http_authorization;

基础示例:

http {
..
proxy_cache_path /opt/app levels:1:2 keys_zone=weiyigeek_cache:10m max_size=10g inactive=60m use_temp_path=off;
map $request_method $purge_method {
PURGE 1;
default 0;
}
...
server {
...
location / {
proxy_cache weiyigeek_cache;
proxy_pass http://weiyigeek;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_purge $purge_method;
add_header Nginx-Cache "$upstream_cache_status";
proxy_net_upstream error timeout invalid_header http_500 http_502;
include proxy_params;
}
...
}
}

问:如何清理指定缓存?

1.rm -rf 删除缓存目录内容
2.第三方扩展模块ngx_cache_purge


以上是关于Nginx配置模块的主要内容,如果未能解决你的问题,请参考以下文章

如何查看nginx的版本及配置选项?nginx都配置了哪些的模块?

Nginx核心配置文件介绍

Nginx进阶学习模块配置记录

FastDFS配置Nginx模块

10.Nginx配置文件与日志切割

10.Nginx配置文件与日志切割