目录
1、nginx location的详细使用
匹配优先级从高到低:
~* = ~ > 不带符号 > ^~ > =
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写,
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
(1)精确匹配
vim /usr/local/nginx/conf.d/vhosts.conf
server{
listen 80;
server_name www.zx.org;
root/data/web/html;
index index.html;
location = /test {
root /data/web2;
}
}
(2)区分大小写
如果访问uri中包含大写字母的ZX,则以下location匹配zx条件不成功,因为 ~ 区分大小写,当用户的请求被执行匹配时发现location中定义的是小写的zx, 本次访问的uri匹配失败,后续要么继续往下匹配其他的location(如果有),要么报错给客户端
vim /usr/local/nginx/conf.d/vhosts.conf
server{
listen 80;
server_name www.zx.org;
root/data/web/html;
index index.html;
location / {
root /data/nginx/zx.org/html;
}
location ~ /ZX {
root /data/nginx/zx.org/zx/html;
}
}
(3)不区分大小写
vim /usr/local/nginx/conf.d/vhosts.conf
server{
listen 80;
server_name www.zx.org;
root/data/web/html;
index index.html;
location / {
root /data/nginx/zx.org/html;
}
location ~* /ZX {
root /data/nginx/zx.org/zx/html;
}
}
(4)匹配文件名后缀
#mkdir -p /webdate/nginx/zx/images
#上传一个图片到/webdate/nginx/zx/images
#vim /usr/local/nginx/conf.d/vhosts.conf
server{
listen 80;
server_name www.zx.org;
location / {
root /webdate/nginx/zx/html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
root /webdate/nginx/zx/images;
index index.html
}
}
2、nginx下的用户认证
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
[root@nginx ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$EN0NlJGM$fMDitN/3j045DlVuT3lXT1
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd zx
New password:
Re-type new password:
Adding password for user zx
[root@nginx ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$EN0NlJGM$fMDitN/3j045DlVuT3lXT1
zx:$apr1$kgGvOH0.$.cnDFi0XkRbE9t9wr/mPA1
[root@nginx ~]# mkdir -p /usr/local/nginx/html/zx/
[root@nginx ~]# echo zx > /usr/local/nginx/html/zx/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.zx.org;
root /usr/local/nginx/html;
index index.html;
location /zx {
root /usr/local/nginx;
auth_basic "login password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
[root@nginx ~]# nginx -s reload
[root@nginx ~]# nginx -t
3、nginx自定义错误页面
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.zx.org;
root /usr/local/nginx/html;
index index.html;
error_page 404 /40x.html; # 添加
location /zx {
root /usr/local/nginx;
auth_basic "login password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html { # 添加
root /usr/local/nginx/html/errorpage;
}
}
[root@nginx ~]# mkdir -p /usr/local/nginx/html
[root@nginx ~]# echo error page > /usr/local/nginx/html/40x.html
4、自定义错误日志
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# mkdir /var/log/zx.org
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.zx.org;
root /usr/local/nginx/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/zx.org/error.log; #错误日志
access_log /var/log/zx.org/access.log; #访问日志
location /zx {
root /usr/local/nginx;
auth_basic "login password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /usr/local/nginx/html/errorpage;
}
}
5、nginx中的文件检测
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内 部500错误。
[root@nginx ~]# mkdir -p /usr/local/nginx/html/
[root@nginx ~]# echo error default > /usr/local/nginx/html/default.html
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.zx.org;
root /usr/local/nginx/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/zx.org/error.log;
access_log /var/log/zx.org/access.log;
# 如果不存在页面, 就转到default.html页面
try_files $uri $uri.html $uri/index.html /html/default.html;
location = /40x.html {
root /data/web/errorpage;
}
}
6、长链接管理
[root@nginx ~]# dnf install telnet -y # 测试工具
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# telnet www.zx.org 80
Trying 15.197.204.56...
Connected to www.zx.org.
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.zx.org
7、下载服务器的设定及优化
ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务 配置使用
[root@nginx ~]# mkdir -p /usr/local/nginx/html/download
[root@nginx ~]# dd if=/dev/zero of=/data/web/download/zxfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.326466 s, 321 MB/s
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.zx.org;
root /usr/local/nginx/html;
index index.html;
error_log /var/log/zx.org/error.log;
access_log /var/log/zx.org/access.log;
try_files $uri $uri.html $uri/index.html /html/default.html;
location /download {
root /usr/local/nginx;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
limit_rate 0;
}
}
8、nginx的状态页面
[root@nginx ~]# nginx -V # 查看配置
nginx version: nginx/1.24.0
built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module
[root@nginx ~]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vim status.conf
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# vim /etc/hosts
[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module
[root@nginx ~]#
[root@nginx ~]#
[root@nginx ~]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vim status.conf
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# vim /etc/hosts # 添加域名解析
[root@nginx ~]# cat /usr/local/nginx/conf.d/status.conf
server {
listen 80;
server_name status.zx.org;
root /usr/local/nginx/html;
index index.html;
location /status {
stub_status;
#auth_basic "login"
#auth_basic_user_file "usr/local/nginx/.htpasswd"
allow 172.25.254.1;
deny all;
}
}
[root@nginx ~]# vim /etc/hosts
[root@nginx ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 nginx.zx.org www.zx.org status.zx.org
10、nginx的数据压缩功能
Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。
Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_vary on;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/gif image/png;
[root@nginx ~]# echo hello zx > /data/web/html/small.html
[root@nginx ~]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html
[root@nginx ~]# curl --head --compressed 172.25.254.100/small.html
[root@nginx ~]# curl --head --compressed 172.25.254.100/big.html