当前位置: 首页 > news >正文

nginx核心配置示例

目录

1、nginx location的详细使用

(1)精确匹配

(2)区分大小写

(3)不区分大小写

(4)匹配文件名后缀

2、nginx下的用户认证

3、nginx自定义错误页面

4、自定义错误日志

5、nginx中的文件检测

6、长链接管理

7、下载服务器的设定及优化

8、nginx的状态页面

10、nginx的数据压缩功能


1、nginx location的详细使用

匹配优先级从高到低:
 ~*    =    ~    >     不带符号    >        ^~        >        =

#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
=       #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~      #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查,不区分字符大小写
~       #用于标准uri前,表示包含正则表达式,并且区分大小写
~*      #用于标准uri前,表示包含正则表达式,并且不区分大写,
不带符号 #匹配起始于此uri的所有的uri
\       #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号

(1)精确匹配

vim /usr/local/nginx/conf.d/vhosts.confserver{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.confserver{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.confserver{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.confserver{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.confgzip  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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 基于LangChain手工测试用例转接口自动化测试生成工具!
  • 第七章数据安全10分
  • Excel求和方法之
  • Pytorch如何判断两个模型的权重是否相同(比较权重差异/参数字典)
  • 推荐适合七夕的SVG模版(第II期)
  • FreeBSD启动后进入单用户模式,但是发现zfs系统是只读的,应该怎样挂载成可读可写呢?
  • SpringCloudAlibaba基础七-2 seata的使用
  • Docker Swarm
  • ArcGIS Pro SDK (十二)布局 6 地图框和环绕要素
  • 年薪30万+,TOP大厂月薪10万+....网络安全工程师凭什么?
  • npm install
  • 如何修改计算机ip地址?几招教你轻松改
  • Java Web|day5.MyBatis
  • 快排/堆排/归并/冒泡/
  • 分布式知识总结(一致性Hash算法)
  • 【React系列】如何构建React应用程序
  • laravel 用artisan创建自己的模板
  • MySQL QA
  • NSTimer学习笔记
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • React的组件模式
  • session共享问题解决方案
  • Shadow DOM 内部构造及如何构建独立组件
  • 程序员该如何有效的找工作?
  • 好的网址,关于.net 4.0 ,vs 2010
  • 简单基于spring的redis配置(单机和集群模式)
  • 使用 @font-face
  • 树莓派 - 使用须知
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​水经微图Web1.5.0版即将上线
  • # 计算机视觉入门
  • $(selector).each()和$.each()的区别
  • (10)STL算法之搜索(二) 二分查找
  • (3)医疗图像处理:MRI磁共振成像-快速采集--(杨正汉)
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (转)socket Aio demo
  • (转)菜鸟学数据库(三)——存储过程
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .FileZilla的使用和主动模式被动模式介绍
  • .mysql secret在哪_MySQL如何使用索引
  • .Net Core中的内存缓存实现——Redis及MemoryCache(2个可选)方案的实现
  • .net(C#)中String.Format如何使用
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .net6 webapi log4net完整配置使用流程
  • .net连接oracle数据库
  • //TODO 注释的作用
  • /proc/vmstat 详解
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • [ 隧道技术 ] cpolar 工具详解之将内网端口映射到公网
  • []指针
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-高中低端机性能适配策略
  • [AIGC] SpringBoot的自动配置解析
  • [BUAA软工]第一次博客作业---阅读《构建之法》