nginx 高级配置

1 网页的状态页

基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态

只需要添加    location    
location /nginx_status {
   stub_status;
}
 

nginx 高级配置_第1张图片

 nginx 高级配置_第2张图片

2 Nginx 第三方模块

2.1 ehco 模块

解压并编译安装 echo-nginx

nginx 高级配置_第3张图片

[root@localhost nginx-1.18.0]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/opt/echo-nginx-module-master

make && make install

编写配置文件

nginx 高级配置_第4张图片

 用另一台机器测试

nginx 高级配置_第5张图片

3 变量

官方文档   http://nginx.org/en/docs/varindex.html
 

3.1 内置

常用内置变量

$remote_addr; 
#存放了客户端的地址,注意是客户端的公网IP




$proxy_add_x_forwarded_for
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addrthe “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.
客户机    代理1     代理2     nginx服务器
$proxy_add_x_forwarded_for: 在代理1 上存的是  客户机的ip
$proxy_add_x_forwarded_for: 在代理2 上存的是  客户机的ip,代理1的ip            用逗号隔开
$proxy_add_x_forwarded_for: nginx  上存的是  客户机的ip,代理1的ip,代理2的ip



$args; 
#变量中存放了URL中的参数,例如:http://www.kgc.org/main/index.do?id=20190221&partner=search
#返回结果为: id=20190221&partner=search    存放的就是这个


$document_root; 
#保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html。



$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令,比
如:http://www.kgc.org/main/index.do?id=20190221&partner=search会被定义为/main/index.do 
#返回结果为:/main/index.do


$host; 
#存放了请求的host名称


limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0


$remote_port; 
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口

$remote_user; 
#已经经过Auth Basic Module验证的用户名

$request_body_file; 
#做反向代理时发给后端服务器的本地资源的名称

$request_method; 
#请求资源的方式,GET/PUT/DELETE等

$request_filename; 
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,如:/apps/nginx/html/main/index.html

$request_uri; 
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search 

$scheme; 
#请求的协议,例如:http,https,ftp等

$server_protocol; 
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等

$server_addr; 
#保存了服务器的IP地址

$server_name; 
#请求的服务器的主机名

$server_port; 
#请求的服务器的端口号

$http_
#name为任意请求报文首部字段,表示记录请求报文的首部字段
arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores 
#用下划线代替横线
#示例: echo $http_User_Agent;  

$http_user_agent; 
#客户端浏览器的详细信息

$http_cookie; 
#客户端的cookie信息


$cookie_
#name为任意请求报文首部字部cookie的key名

$http_
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线
arbitrary request header field; the last part of a variable name is the field 
name converted to lower case with dashes replaced by underscores #用下划线代替横线
#示例: 
echo $http_user_agent; 
echo $http_host;


$sent_http_
#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有问题
echo $sent_http_server;



$arg_
#此变量存放了URL中的指定参数,name为请求url中指定的参数
#对比 变量  $arg  是全部, 如果 要id   如下
echo $arg_id;

编写nginx 高级配置_第6张图片

重启nginx后用另一台机器进行测试

nginx 高级配置_第7张图片

-b  加上cookie

nginx 高级配置_第8张图片

3.2 定义变量

假如需要自定义变量名称和值,使用指令set $variable value;

语法格式:

Syntax: set $variable value;
Default: —
Context: server, location, if

例如:

location /test {
        set $name  kgc;
        echo $name;
        set $my_port $server_port;
        echo $my_port;
        }

nginx 高级配置_第9张图片

重启nginx后用另一台机器进行测试

nginx 高级配置_第10张图片

3.3Nginx压缩功能

支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module

官方文档: Module ngx_http_gzip_module

配置指令如下:

你可能感兴趣的:(运维,nginx,linux,系统安全,服务器)