3.12 nginx安装及使用

本文详细介绍了Nginx的安装、日志格式定义、location的使用,包括无修饰符、=、~、~*等匹配方式,以及访问控制、用户认证、状态界面开启、Zabbix监控、Rewrite规则、If条件判断、反向代理和负载均衡等核心功能的配置与应用。
摘要由CSDN通过智能技术生成

1. nginx升级、添加模块

//查看当前版本
[root@130 nginx-1.16.1]# nginx -V					
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

//备份当前版本
[root@130 nginx-1.16.1]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.12
[root@130 nginx-1.16.1]# ls /usr/local/nginx/sbin/
nginx  nginx-1.12

//升级版本
[root@130 ~]# cd nginx-1.16.1
[root@130 nginx-1.16.1]# ls ..
anaconda-ks.cfg  nginx-1.16.1.tar.gz            nginx-http-echo-module-master.zip
nginx-1.16.1     nginx-http-echo-module-master  pass
[root@130 nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@130 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../nginx-http-echo-module-master
[root@130 nginx-1.16.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@130 nginx-1.16.1]# make

//停止现有nginx服务
[root@130 nginx-1.16.1]# nginx -s stop
[root@130 nginx-1.16.1]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      128     127.0.0.1:9000                        *:*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      80             :::3306                       :::*

//升级nginx
[root@130 nginx-1.16.1]# ls objs/
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src
[root@130 nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/.
cp:是否覆盖"/usr/local/nginx/sbin/./nginx"? y
[root@130 nginx-1.16.1]# nginx
[root@130 nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../nginx-http-echo-module-master

2. nginx日志log定义格式

log_format 定义日志格式

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  logs/access.log  main;

//注意:此处可用变量为nginx各模块内建变量

3.location使用

location区段,通过指定模式来与客户端请求的URI相匹配

//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能

//语法:location [ 修饰符 ] pattern {
   ......}

常用修饰符说明:

修饰符 功能
= 精确匹配
~ 正则表达式模式匹配,区分大小写
~* 正则表达式模式匹配,不区分大小写
^~ 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@ 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

3.1 无修饰符

没有修饰符表示必须以指定模式开始,如:

server {
   
  server_name www.idfsoft.com;
  location /abc {
   
    ......
  }
}

那么如下内容就可正确匹配:

  • http://www.idfsoft.com/abc
  • http://www.idfsoft.com/abc?p1=11&p2=22
  • http://www.idfsoft.com/abc/
    示例:
[root@130 conf]# vim nginx.conf
    	location /abc {
   
            root html;
            echo '
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值