nginx升级以及location区段

平滑升级
1、nginx -V
2、重新编译加上新功能
3、编译
4、先备份现有的程序
5、在objs目录下将nginx程序拷贝到现有的程序目录

下载nginx echo模块
[root@localhost ~]# ls
!  anaconda-ks.cfg  nginx-1.18.0  nginx-1.18.0.tar.gz  v0.61.tar.gz
[root@localhost ~]# tar xf v0.61.tar.gz 
[root@localhost ~]# ls
!  anaconda-ks.cfg  echo-nginx-module-0.61  nginx-1.18.0  nginx-1.18.0.tar.gz  v0.61.tar.gz

升级nginx
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.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@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ./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=../echo-nginx-module-0.61/    //要先进入模块的目录

[root@localhost nginx-1.18.0]# make   //直接make就好了

备份编译文件以防发生错误
[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/

用编译好的文件把原来的覆盖
cp: 无法创建普通文件"/usr/local/nginx/sbin/nginx": 文本文件忙
[root@localhost ~]# nginx -s stop;cp /root/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y

查看
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.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 --add-module=../echo-nginx-module-0.61/

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

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

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

常用修饰符说明

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写
~*正则表达式模式匹配,不区分大小写
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等
 [root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# mkdir zabbix
[root@localhost html]# ls
50x.html  index.html  zabbix
[root@localhost html]# cd zabbix/
[root@localhost zabbix]# echo 'zabbix' >index.html
[root@localhost zabbix]# ls
index.html
[root@localhost conf]# vim nginx.conf
找到location,加入zabbix
 location / {
            root   html/zabbix;        //在HTML后面加zabbix,让它直接访问到zabbix
            index  index.html index.htm;
此时会报错
[root@localhost conf]# nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
解决方法
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload

页面访问
在这里插入图片描述

[root@localhost html]# mkdir /opt/myweb
[root@localhost html]# cd /opt/myweb/
[root@localhost myweb]# ls
[root@localhost myweb]# echo 'yangcan' >index.html
[root@localhost myweb]# ls
index.html
[root@localhost myweb]# mkdir zabbix
[root@localhost myweb]# mv index.html zabbix/
[root@localhost myweb]# pwd
/opt/myweb
[root@localhost myweb]# cd zabbix/
[root@localhost zabbix]# pwd
/opt/myweb/zabbix
[root@localhost zabbix]# ls
index.html
[root@localhost zabbix]# cat index.html
yangcan
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload
        #access_log  logs/host.access.log  main;
        
        location /zabbix {           //添加内容
            root /opt/myweb;          //添加内容  
            index index.html;             //添加内容
        }              //添加内容
        
        location / {
            root   html/zabbix;
            index  index.html index.htm;
        }

页面访问(用root是找 /opt/myweb/下的zabbix下的index.html)
在这里插入图片描述
用alias path指定地址

[root@localhost myweb]# ls
zabbix
[root@localhost myweb]# echo 'ycan' >index.html
[root@localhost myweb]# ls
index.html  zabbix
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
        location /zabbix {                 //添加内容
            alias /opt/myweb;           //root修改为alias
            index index.html;          //添加内容
        }                                      //添加内容

        location / {
            root   html/zabbix;
            index  index.html index.htm;
        }

页面访问(使用alias是直接去找/opt/myweb/下面的index.html)
在这里插入图片描述
颠倒位置

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload

location / {
            root   html/zabbix;
            index  index.html index.htm;
        }

        location /zabbix {
            alias /opt/myweb;
            index index.html;
        }

页面访问
在这里插入图片描述
没有修饰符表示必须以指定模式开始,如:

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
     #access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
        }
        location /abc {          //添加内容
            echo "abc test";                        //添加内容
        }                      //添加内容

本机测试

可以匹配到
C:\Users\Administrator>curl http://192.168.175.100/abc
abc test
C:\Users\Administrator>curl http://192.168.175.100/abc/
abc test
C:\Users\Administrator>curl http://192.168.175.100/abc/jjyy
abc test

继续修改

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
         location / {
            root   html;
            index  index.html index.htm;
        }
        location ^/abc$ {     //修改内容
            echo "abc test";
        }

本机测试

匹配不到
C:\Users\Administrator>curl http://192.168.175.100/abc/jjyy
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

C:\Users\Administrator>curl http://192.168.175.100/abc/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

加等于号

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
        #access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
        }
        location /abc/ {
            echo "abc test";
        }

        location = /abc {     //加上等于号
            echo "abc =test";    //加上等于号
        }

本机查看测试结果

可以匹配到
C:\Users\Administrator>curl http://192.168.175.100/abc
abc =test
以下匹配不到
C:\Users\Administrator>curl http://192.168.175.100/abc/
abc test
C:\Users\Administrator>curl http://192.168.175.100/abc/jjyy
abc test

~:表示指定的正则表达式要区分大小写,如:

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
 #access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
        }
        location /abc/ {
            echo "abc test";
        }

        location = /abc {
            echo "abc =test";
        }
        location  ~ ^/abc$ {                      //加~号
            echo "区分大小写的正则表达式";
        }

本机测试(=号比~优先级高

可以匹配
C:\Users\Administrator>curl http://192.168.175.100/abc
abc =test
匹配不到
C:\Users\Administrator>curl http://192.168.175.100/abc/
abc test

交换位置测试

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
#access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
        }
        location /abc/ {
            echo "abc test";
        }
        location ~ ^/abc$ {
            echo "区分大小写的正则表达式";
        }
        location = /abc {
            echo "abc =test";
        }

本机测试(确实证明=号比~优先级高)

C:\Users\Administrator>curl http://192.168.175.100/abc
abc =test

去掉=号继续测试

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# nginx -s reload
         location / {
            root   html;
            index  index.html index.htm;
        }
        location /abc/ {
            echo "abc test";
        }
        location ~ ^/abc$ {
            echo "区分大小写的正则表达式";
        }
#        location = /abc {                           用#好注释
#            echo "abc =test";
#        }

本机测试
可以匹配
C:\Users\Administrator>curl http://192.168.175.100/abc
区分大小写的正则表达式
匹配不到
C:\Users\Administrator>curl http://192.168.175.100/abc/
abc test
C:\Users\Administrator>curl http://192.168.175.100/abcde
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式

查找顺序和优先级:由高到底依次为

带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配
优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
### Nginx在Windows上的安装与配置 对于希望在 Windows 上部署 Nginx 的用户而言,官方并不直接提供针对此操作系统的二进制分发版[^1]。然而,社区版本可以用于测试目的。下载完成后解压至指定位置即可完成基本安装。 #### 创建并编辑Nginx主配置文件`nginx.conf` 通常情况下,在解压缩后的 Nginx 文件夹内会自带一个示例性的 `nginx.conf` 文件位于 conf 目录下。该文件包含了多个上下文区块定义,其中最重要的部分是全局设置区段,它决定了整个 Web 服务的行为模式[^3]: ```plaintext user nobody; worker_processes auto; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } ``` 上述片段展示了如何指明错误日志的位置以及进程 ID (PID) 文件的保存路径等重要参数。 #### 使用conf.d目录管理额外站点配置 为了便于管理和维护不同应用的服务设定,推荐采用独立于核心配置之外的方式处理特定网站或者应用程序的相关选项。这可以通过包含其他配置文件来实现,具体做法是在主配置中加入如下语句指向子配置文件所在目录下的所有`.conf`结尾文档: ```nginx http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 加入这一行以加载conf.d中的配置文件 include conf.d/*.conf; } ``` 当涉及到具体的虚拟主机或是项目级别的调整时,则可以在 `conf.d/` 下创建相应的 `.conf` 文件来进行针对性定制化工作。例如建立一个新的名为 example.com.conf 的文件,并在里面编写对应域名或 IP 地址访问规则、静态资源定位等相关条目。 #### 修改或新增conf.d内的配置实例 假设现在有一个简单的 PHP 应用程序需要通过 FastCGI 协议传递请求给后端解释器执行脚本逻辑,那么可以在 `conf.d/example-app.conf` 中添加类似下面的内容: ```nginx server { listen 80; server_name localhost; location / { root html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; # 或者 unix:/tmp/php-cgi.sock 对应PHP-FPM监听地址 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` 这段代码设置了 HTTP 监听端口、默认首页文件顺序、自定义错误页面显示方式,并特别为带有 .php 扩展名的 URL 请求制定了转发策略以便能够正确解析动态网页内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值