企业高性能web服务器之nginx篇


文章相关连接如下:

Nginx核心配置

Nginx的配置文件的组成部分:

  • 主配置文件:nginx.conf
  • 子配置文件: include conf.d/*.conf
  • fastcgi, uwsgi,scgi 等协议相关的配置文件
  • mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动
  • 使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

location 使用详情

  • 在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;
  • ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,
  • 而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri
  • uri是用户请求的字符串,即域名后面的web文件路径
  • 然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。

语法规则:

location [ = | ~ | ~* | ^~ ] uri { ... }

= 		#用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求

^~   	#用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
 		#对uri的最左边部分做匹配检查,不区分字符大小写
 
~   	#用于标准uri前,表示包含正则表达式,并且区分大小写
~*      #用于标准uri前,表示包含正则表达式,并且不区分大写不带符号 #匹配起始于此uri的所有的uri

\   	#用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号

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

location之精确匹配

在server部分使用location配置一个web界面,例如:当访问nginx 服务器的/logo.jpg的时候要显示指定html文件的内容,精确匹配一般用于匹配组织的logo等相对固定的URL,匹配优先级最高

[root@Nginx test]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx test]# cat /data/web/test/index.html 
test
[root@Nginx test]# mkdir /data/web/test2 -p
[root@Nginx test]# echo test2 > /data/web/test2/index.html
[root@Nginx test]# cat /data/web/test2/index.html
test2

[root@Nginx test]# nginx -s reload

在这里插入图片描述

在这里插入图片描述
验证:在这里插入图片描述

必须精确才能匹配到

在这里插入图片描述

location之区分大小写

实现区分大小写的模糊匹配. 以下范例中,

  • 如果访问uri中包含大写字母的logo.PNG,则以下location匹配logo.png条件不成功,因为 ~ 区分大小写,当用户的请求被执行匹配时发现location中定义的是小写的png,
  • 本次访问的uri匹配失败,后续要么继续往下匹配其他的location(如果有),要么报错给客户端
[root@Nginx test]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx test]# nginx -s reload

在这里插入图片描述
验证:

在这里插入图片描述
在这里插入图片描述

location之不区分大小写

用来对用户请求的uri做模糊匹配,uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作,此方式使用较多

注意: 此方式中,对于Linux文件系统上的文件仍然是区分大小写的,如果磁盘文件不存在,仍会提示404

[root@Nginx test2]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx test2]# nginx -s reload

在这里插入图片描述

location之文件名后缀

[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@Nginx ~]# mkdir /data/web1
[root@Nginx ~]# echo test1111 > /data/web1/index.html
[root@Nginx ~]# nginx -s reload

在这里插入图片描述

在这里插入图片描述

Nginx账户认证功能

由 ngx_http_auth_basic_module 模块提供此功能

[root@Nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd lee
New password: 
Re-type new password: 
Adding password for user lee
[root@Nginx ~]# cat /usr/local/nginx/.htpasswd 
lee:$apr1$XkdmqB6a$bWiKP9BKtlC/KFOLhmyiX1
[root@Nginx ~]# mkdir /data/web/lee
[root@Nginx ~]# echo lee > /data/web/lee/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx ~]# nginx -s reload

在这里插入图片描述

验证:
在这里插入图片描述
在这里插入图片描述

自定义错误页面

自 定义错误页,同时也可以用指定的响应状态码进行响应, 可用位置:http, server, location, if in location

[root@Nginx ~]# mkdir /data/web/errorpage
[root@Nginx ~]# echo error page > /data/web/errorpage/40x.html
[root@Nginx ~]# nginx -s reload

在这里插入图片描述

验证:输入一个不存在的目录,失败界面显示自定义的页面

在这里插入图片描述

自定义错误日志

[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx ~]# mkdir /var/log/timinglee.org -p
[root@Nginx ~]# nginx -s reload
[root@Nginx timinglee.org]# curl www.timinglee.org
test1111
[root@Nginx timinglee.org]# cat  /var/log/timinglee.org/access.log
[root@Nginx timinglee.org]# curl www.timinglee.org/aaaaa
error page
[root@Nginx timinglee.org]# cat  /var/log/timinglee.org/error.log

在这里插入图片描述
验证:
在这里插入图片描述

检测文件是否存在


[root@Nginx timinglee.org]# mkdir /data/web/html/error -p
[root@Nginx timinglee.org]# vim /usr/local/nginx/conf.d/vhost.conf
[root@Nginx timinglee.org]# rm -rf /date/web/html/index.html 
[root@Nginx timinglee.org]# curl www.timinglee.org
error page
[root@Nginx timinglee.org]# cat /data/web/html/error/default.html
error default

在这里插入图片描述
在这里插入图片描述

长连接

keepalive_timeout timeout [header_timeout];     #设定保持连接超时时长,0表示禁止长连接,默认为75s
 												#通常配置在http字段作为站点全局配置
 
keepalive_requests 数字;   #在一次长连接上所允许请求的资源的最大数量
 						  #默认为100次,建议适当调大,比如:500

在这里插入图片描述

下载服务器

ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求,并生成目录列表,可以做为下载服务配置使用

建立下载目录:

[root@Nginx ~]# mkdir /data/web/download
[root@Nginx ~]# dd if=/dev/zero of=/data/web/download/leefile bs=1M count=100
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB,100 MiB)已复制,0.0975128 s,1.1 GB/s
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx ~]# nginx -s reload

在这里插入图片描述
做一个大小100M的文件

在这里插入图片描述
此时浏览器并不能看到和下载
在这里插入图片描述
设置参数,使其成为下载服务器

[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@Nginx ~]# nginx -s reload

在这里插入图片描述
再去查看就有下载列表:时间显示的是伦敦时间
在这里插入图片描述

将伦敦时间设置为本地时间
在这里插入图片描述
更改后时间
在这里插入图片描述
默认下载速度过快,我们需要进行限速
在这里插入图片描述
设置限速:
在这里插入图片描述
成功:
在这里插入图片描述

Nginx高级配置

Nginx的状态页

  • 基于nginx 模块ngx_http_stub_status_module 实现,
  • 在编译安装nginx的时候需要添加编译参数 -with-http_stub_status_module
  • 否则配置完成之后监测会是提示法错误

注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态

做windows和linux的本地解析

在这里插入图片描述

在这里插入图片描述
编写配置文件

[root@Nginx conf.d]# vim /usr/local/nginx/conf.d/status.conf
[root@Nginx conf.d]# nginx -s reload

在这里插入图片描述
此时可以看见状态页的相关信息:状态页用于输出nginx的基本状态信息

Active connections: 291
server accepts handled requests
 16630948 16630948 31070465
 上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数
 #包括连接等待空闲连接数=reading+writing+waiting
 
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
 #通常等于accepts,除非有因worker_connections限制等被拒绝的
连接
 
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
 #数值越大,说明排队现象严重,性能不足
 
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明
访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
 开启 keep-alive的情况下,这个值等于active – 
(reading+writing)

在这里插入图片描述
为此我们做一些限定,规定谁能看,谁不能看,windows下就是只允许172.25.254.1的主机访问
在这里插入图片描述

Nginx 压缩功能

Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。

Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_module,默认是内置模块

root@Nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf
[root@Nginx conf.d]# echo hell > /data/web/html/small.html
[root@Nginx conf.d]# du -sh /usr/local/nginx/logs/

在这里插入图片描述
创建一个小于1k的文件

echo hell > /data/web/html/small.html

创建一个大于1k的文件

# 查找一个大于1k的文件
[root@Nginx logs]# du -sh access.log 
32K	access.log
[root@Nginx logs]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html

在这里插入图片描述

验证:
在这里插入图片描述

Nginx 变量使用

  • nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用
  • 变量可以分为内置变量和自定义变量
  • 内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值。

Nginx与php-fpm在同一服务器

重新准备nginx环境

# 下载nginx软件包并解压进入
[root@Nginx ~]# cd nginx-1.26.1/
[root@Nginx nginx-1.26.1]#

# 编译环境
./configure --prefix=/usr/local/nginx --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33 --user=nginx --group=nginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-pcre
# 下载
make && make install

源码编译php

# 下载php依赖
yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel
# 进入php启动文件
[root@Nginx ~]# cd php-8.3.9/
# 开始编译环境

# 编译

php配置参数

[root@Nginx php-8.3.9]# cd /usr/local/php/etc/
[root@Nginx etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@Nginx etc]# vim php-fpm.conf	

在这里插入图片描述

[root@Nginx etc]# cd php-fpm.d/
[root@Nginx php-fpm.d]# ls
www.conf.default
[root@Nginx php-fpm.d]# cp www.conf.default www.con
cd /root/php-8.3.9/
[root@Nginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@Nginx ~]# vim /usr/local/php/etc/php.ini


在这里插入图片描述
查看时区:

[root@Nginx ~]# timedatectl list-timezones | grep Asia/Shang
Asia/Shanghai

在这里插入图片描述

[root@Nginx php-8.3.9]# cd sapi/fpm/
[root@Nginx fpm]# cp php-fpm.service /lib/systemd/system/
# 注释掉重新挂载为只读
[root@Nginx fpm]# vim /lib/systemd/system/php-fpm.service

在这里插入图片描述

[root@Nginx fpm]# systemctl daemon-reload 
[root@Nginx fpm]# systemctl start php-fpm.service 
[root@Nginx fpm]# netstat -antlupe | grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      0          124115     143173/php-fpm: mas 

添加环境变量

[root@Nginx fpm]# vim ~/.bash_profile 
[root@Nginx fpm]# source ~/.bash_profile

在这里插入图片描述

php测试页面

[root@Nginx ~]# mkdir -p /data/web/php
[root@Nginx ~]# cd /data/web/php/
[root@Nginx php]# ls
[root@Nginx php]# vim index.php

在这里插入图片描述

[root@Nginx php]# cd /usr/local/nginx/conf/
[root@Nginx conf]# vim fastcgi.conf
[root@Nginx conf]# cd ..
[root@Nginx nginx]# ls
conf  html  logs  sbin
[root@Nginx nginx]# mkdir conf.d
[root@Nginx nginx]# vim conf/nginx.conf

在这里插入图片描述

[root@Nginx nginx]# ls
conf  conf.d  html  logs  sbin
[root@Nginx nginx]# cd conf.d/
[root@Nginx conf.d]# ls
[root@Nginx conf.d]# vim vhosts.conf

在这里插入图片描述

[root@Nginx nginx]# 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@Nginx nginx]# nginx -s reload

浏览器验证:
在这里插入图片描述

php性能差,怎么解决

php的动态扩展模块

在这里插入图片描述

需要安装memcache模块

[root@Nginx ~]# tar zxf memcache-8.2.tgz
[root@Nginx ~]# cd memcache-8.2/
[root@Nginx memcache-8.2]# yum install autoconf
[root@Nginx memcache-8.2]# phpize
[root@Nginx memcache-8.2]# ./configure && make && make install

配置php加载memcache模块

[root@Nginx memcache-8.2]# vim /usr/local/php/etc/php.ini
[root@Nginx memcache-8.2]# systemctl reload php-fpm.service
[root@Nginx memcache-8.2]# php -m | grep mem

在这里插入图片描述
在这里插入图片描述
部署memcached

[root@Nginx ~]# yum install memcached -y
[root@Nginx ~]# systemctl enable --now memcached.service
[root@Nginx ~]# netstat -antlupe | grep memcache

在这里插入图片描述

[root@Nginx memcache-8.2]# cp example.php memcache.php /data/web/php/
[root@Nginx memcache-8.2]# cd /data/web/php/
[root@Nginx php]# ls
example.php  index.php  memcache.php
[root@Nginx php]# vim memcache.php

在这里插入图片描述
测试:

在这里插入图片描述
在这里插入图片描述

不断刷新:模拟数据的读写

在这里插入图片描述
查看命中效果
在这里插入图片描述

性能对比
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
mysql 数据
nginx web服务数据请求
php处理的是mysql读写数据
memcache缓存的是php冲数据库中取的数据

php 大多数时间做网页和数据库的交互

php告诉缓存

实现nginx直接取memcache调用数据
在这里插入图片描述

需要有srcache-nginx-module-0.33,memc-nginx-module-0.20两个模块,此我们前面编译nginx的时候已经加入

[root@Nginx conf.d]# vim vhosts.conf 
[root@Nginx conf.d]# nginx -s reload

在这里插入图片描述

测试:

在这里插入图片描述

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值