平滑升级
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 路径 )