1.nginx升级
1.下载echo-nginx-module-master.zip模块
[root@localhost ~]# ls
anaconda-ks.cfg echo-nginx-module-master.zip nginx-1.16.1 pass remi-release-7.rpm
[root@localhost ~]#
[root@localhost ~]# unzip echo-nginx-module-master.zip
2.将原来的nginx的主程序备份一下
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# cp nginx{,-orgin}
[root@localhost sbin]# ls
nginx nginx-orgin
3.看之前的nginx安装的编译参数,然后再重新编译加上现在的编译参数
[root@localhost 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
[root@localhost 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=../echo-nginx-module-master
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
4.查看编译后的objs文件,查看objs下面的版本发生了变化
[root@localhost 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@localhost nginx-1.16.1]# cd objs/
[root@localhost objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
[root@localhost objs]# ./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=../echo-nginx-module-master
[root@localhost objs]# 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
5.停止nginx,然后将objs下面的nginx覆盖之前的主程序之后再重启就可以了
[root@localhost objs]# nginx -s stop
[root@localhost objs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
[root@localhost objs]# cp nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y
[root@localhost objs]# nginx
[root@localhost objs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
6.测试echo模块是否成功
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /opt/haha {
echo lala;
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.90.128/opt/haha
lala
2.location区段
没有修饰符表示必须以指定模式开始,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /abc {
echo haha;
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.90.128/abc
haha
[root@localhost ~]# curl http://192.168.90.128/abc/
haha
[root@localhost ~]# curl http://192.168.90.128/abc?p1=11$p2=22
haha
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
http://www.idfsoft.com/abc/
=:表示必须与指定的模式精确匹配,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location = /abc {
echo haha;
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.90.128/abc?p1=11$p2=22
haha
[root@localhost ~]# curl http://192.168.90.128/abc/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/abc/abcde
~:表示指定的正则表达式要区分大小写,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ ^/abc$ {
echo haha;
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.90.128/abc
haha
[root@localhost ~]# curl http://192.168.90.128/ABC
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@localhost ~]# curl http://192.168.90.128/abc/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/ABC
http://www.idfsoft.com/abcde
~*:表示指定的正则表达式不区分大小写,如:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~ ^/abc$ {
echo haha;
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.90.128/ABC
haha
[root@localhost ~]# curl http://192.168.90.128/abc
haha
那么如下内容就可正确匹配:
http://www.idfsoft.com/abc
http://www.idfsoft.com/abc?p1=11&p2=22
http://www.idfsoft.com/ABC
如下内容则无法匹配:
http://www.idfsoft.com/abc/
http://www.idfsoft.com/abcde
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为
带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有或*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配
3.rewrite
用rewrite模块执行URL的重定向
1.修改配置文件,在官网上搜索ima时会重定向到images里去访问1.jpg
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /ima {
rewrite ^/ima/(.*\.jpg)$ /images/$1 break;
}
location /images {
root html;
}
[root@localhost ~]# nginx -s reload
2.在html下面创建images目录,将1.jpg存放到下面
[root@localhost ~]# ls /usr/local/nginx/html/
50x.html images index.html index.php zabbix
[root@localhost ~]# ls /usr/local/nginx/html/images/
1.jpg
3.测试,输入192.168.90.128/ima/1.jpg会重定向到images下面的1.jpg
4.修改配置文件让他重定向到百度
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /ima {
rewrite ^/ima/(.*\.jpg)$ http://www.baidu.com break;
}
[root@localhost ~]# nginx -s reload
5.测试,访问192.168.90.128/ima/1.jpg时会跳转到百度
6.在配置文件写上 try_files,当你输入ip访问时,会在html自动的去找第一个index1.html,如果第一个没有去找a.html,然后去找@heelo,
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
try_files $uri/index1.html a.html @hello;
}
location @hello{
echo 'haha';
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ls /usr/local/nginx/html/
50x.html images/ index.html index.php zabbix/