Linux学习-Nignx学习(三)

开启stub_status功能
#先判断是否在编译时添加了--with-http_stub_status_module模块
[root@nginx01 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client

# stub_status {on|off}
#在server标签中配置以下几行:
location /status {
          stub_status on;
          allow 192.168.88.101;
          deny all;
 }
[root@nginx01 ~]# curl http://192.168.88.101/status
Active connections: 1                 #当前处于打开状态连接数
server accepts handled requests
 10 10 6 
 #1.已接受的连接数(10)
 #2.已处理的连接数(10)
 #3.已处理的请求数,在“保持连接”模式下,请求数量可能会多于连接数量(6)
Reading: 0 Writing: 1 Waiting: 0 
#Reading:正处于接收请求状态的连接数
#Writing:请求接收完成,正处于处理请求或发送响应的过程中的连接数
#Waiting:保持连接模式,且处理活动状态的连接数
rewrite regex replacement flag
#如:rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
http://www.tye.com/images/a/b/c/1.jpg------------>http://www.tye.com/imgs/a/b/c/1.jpg

flag:{last|break|redirect|permanent}
 last:一旦此rewrite规则重写完成后,不再被后面其它的Rewrite规则进行处理,而由User Agentt重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
 break:一旦此Rewrite规则重写完成后,由User Agent对新的URL重新发起请求,不会再被当前location内的任何rewrite规则所检查
 redirect:以302响应码(临时重定向)返回新的URL
 permanent:以301响应码(永久重定向)返回新的URL
#/etc/nginx/nginx.conf配置
#实验1,使用break
location / {
            root html;
            rewrite ^/bbs/(.*)$ /forum/$1 break;
        }
[root@nginx01 html]# curl http://www.tye.com/bbs/index.html
forum/index.html
#实验2,使用redirect
location / {
            root html;
            rewrite ^/bbs/(.*)$  https://www.tye.com/web1/ redirect;
            #deny 127.0.0.1;
           # auth_basic "Only For VIP";
           # auth_basic_user_file /etc/nginx/users/.htpasswd;
        }


注:当使用浏览器访问http://192.168.88.101/bbs/时,会跳转至https://www.tye.com/web1/页面,并返回302状态码
在这里插入图片描述

if
#语法 if(condition) {...}
#应用环境:server,location
condition:
  (1)变量名:变量值为空串,或以“0”开始,则为false,其它均为true
  (2)以变量为操作数构成的比较表达式:可使用=,!=类似的比较操作符进行测试
  (3)正则表达式的模式匹配操作:【~】区分大小写,【~*】不区分大小写,【!~】或【!~*】对上面取反
  (4)测试路径为文件可能性:-f,!-f
  (5)测试指定路径为目录的可能性:-d,!-d
  (6)测试文件的存在性:-e,!-e
  (7)检查文件是否有执行权限:-x,!-x
如:if($http_user_agent ~* MSIE) {
      rewrite ^(.*)$ /msie/$1 break;
     }
防盗链
location ~*\.(jpg|jpeg|gif|png)$ {
  #定义合法引用
  valid_referer none blocked www.tye.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.tye.com/403.html;
  }
}
定制访问日志格式
#变量值参见-->https://nginx.org/en/docs/http/ngx_http_core_module.html
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
#$remote_addr:表示远程主机IP
#$remote_user:表示基本认证的用户名称
#$time_local:本地时间
#$request:请求行
#$status:响应状态码
#$body_bytes_sent:向客户端发送了多少字节,不统计响应头
#$http_referer:当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器该网页是从哪个页面链接过来,服务器因此可以获得信息用于处理
#$http_user_agent:检查浏览页面的访问者在用什么操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好的代码
#$http_x_forwarded_for:用来表示 HTTP 请求端真实客户端 IP
网络连接相关配置
#keepalive_timeout #:长连接的超时时长,默认75
#keepalive_requests #:长连接上所能够允许请求的最大资源数
#keepalive_disable [msie6|safari|none]:为指定类型的User Agent禁用长连接
#tcp_nodelay on|off:是否对长连接使用TCP_NODELAY选项
#client_header_timeout #:读取http请求报文首部的超时时长
#client_body_timeout #:读取http请求报文body部分的超时时长
#send_timeout #:发送响应报文的超时时长	
fastcgi的相关配置
#LNMP:php启用fpm
[root@nginx01 html]# ls /etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        /etc/nginx/fastcgi_params;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值