一、nginx简介
Nginx:engine X ,2002年开发,分为社区版和商业版(nginx plus ) 2019年3月11日 F5 Networks 6.7亿美元的价格收购
Nginx是免费的、开源的、高性能的HTTP和反向代理服务器、邮件代理服务器、以及TCP/UDP代理服务器
解决C10K问题(10K Connections)
Nginx官网:http://nginx.org nginx
其它的二次发行版: Tengine:由淘宝网发起的Web服务器项目。
它在Nginx的基础上,针对大访问量网站的需求,添加 了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了 很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。从2011年12月开始, Tengine成为一个开源项目官网:http://tengine.taobao.org/
功能
- 静态的web资源服务器html,图片,js,css,txt等静态资源
- http/https协议的反向代理
- 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
- tcp/udp协议的请求转发(反向代理)
- imap4/pop3协议的反向代理
进程结构
web 请求处理机制
- 多进程方式:服务器每接收到一个客户端请求就有服务器的主进程生成一个子进程响应客户端,直 到用户关闭连接,这样的优势是处理速度快,子进程之间相互独立,但是如果访问过大会导致服务 器资源耗尽而无法提供请求
- 多线程方式:与多进程方式类似,但是每收到一个客户端请求会有服务进程派生出一个线程和此客 户端进行交互,一个线程的开销远远小于一个进程,因此多线程方式在很大程度减轻了web服务器 对系统资源的要求,但是多线程也有自己的缺点,即当多个线程位于同一个进程内工作的时候,可 以相互访问同样的内存地址空间,所以他们相互影响,一旦主进程挂掉则所有子线程都不能工作 了,IIS 服务器使用了多线程的方式,需要间隔一段时间就重启一次才能稳定。
- Nginx是多进程组织模型,而且是一个由 Master 主进程和 Worker 工作进程组成。
Nginx 模块介绍
nginx 有多种模块
- 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件 驱动机制 、进程管理等核心功能
- 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应 头设置 等等
- 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如: Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
- 邮件服务模块:主要用于支持 Nginx 的邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持
- Stream服务模块: 实现反向代理功能,包括TCP协议代理
- 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支 持等
二、Nginx 安装与配置
环境准备
从RHEL9.4母盘中克隆出一台新的虚拟机,网络默认NAT模式
Nginx eth0 172.25.254.100 nginx-node1.exmaple.com
关闭selinux,关闭防火墙
[root@nginx-node1 ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz
--2024-08-20 10:12:07-- https://nginx.org/download/nginx-1.26.2.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2601::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1244789 (1.2M) [application/octet-stream]
Saving to: ‘nginx-1.26.2.tar.gz’
nginx-1.26.2.tar.gz 100%[======================>] 1.19M 563KB/s in 2.2s
2024-08-20 10:12:10 (563 KB/s) - ‘nginx-1.26.2.tar.gz’ saved [1244789/1244789]
[root@nginx-node1 ~]# ll
total 2308
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos
# 解压源码包
[root@nginx-node1 ~]# tar zxf nginx-1.24.0.tar.gz
[root@nginx-node1 ~]# ll
total 2308
-rw-------. 1 root root 992 Aug 4 00:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Desktop
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Documents
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Downloads
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Music
drwxr-xr-x 8 1001 1001 158 Apr 11 2023 nginx-1.24.0
-rw-r--r-- 1 root root 1112471 Aug 16 09:17 nginx-1.24.0.tar.gz
-rw-r--r-- 1 root root 1244789 Aug 13 00:39 nginx-1.26.2.tar.gz
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Pictures
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Public
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Templates
drwxr-xr-x. 2 root root 6 Aug 4 00:54 Videos
[root@nginx-node1 nginx-1.24.0]# ll
total 812
drwxr-xr-x 6 1001 1001 326 Aug 16 10:16 auto
-rw-r--r-- 1 1001 1001 323312 Apr 11 2023 CHANGES
-rw-r--r-- 1 1001 1001 494234 Apr 11 2023 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Aug 16 10:16 conf
-rwxr-xr-x 1 1001 1001 2611 Apr 11 2023 configure #检测环境
drwxr-xr-x 4 1001 1001 72 Aug 16 10:16 contrib
drwxr-xr-x 2 1001 1001 40 Aug 16 10:16 html
-rw-r--r-- 1 1001 1001 1397 Apr 11 2023 LICENSE
drwxr-xr-x 2 1001 1001 21 Aug 16 10:16 man
-rw-r--r-- 1 1001 1001 49 Apr 11 2023 README
drwxr-xr-x 9 1001 1001 91 Aug 16 10:16 src
# 检测当前的环境是否适合当前的选择
[root@nginx-node1 nginx-1.24.0]# ./configure --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
checking for OS
+ Linux 5.14.0-427.13.1.el9_4.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
# 此时会报错,因为没有安装C语言的编译器,所以我们需要下载gcc
[root@nginx-node1 nginx-1.24.0]# dnf install gcc -y
# 安装完之后再执行检测环境
[root@nginx-node1 nginx-1.24.0]# ./configure --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
Nginx的平滑升级及版本回滚
- 平滑升级:让客户感受不到任何的使用影响
- 将旧Nginx二进制文件换成新Nginx程序文件(注意先备份)
- 向master进程发送USR2信号
- master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
- master进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx主
- 进程共同提供Web服务,当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进程的PID存放至新生成的pid文件nginx.pid
- 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止
- 向旧master进程发送QUIT信号,关闭老master,并删除Nginx.pid.oldbin文件
- 如果发现升级有问题,可以回滚∶向老master发送HUP,向新master发送QUIT
导入安装包编译,参考上面24版安装,步骤相似
[root@Nginx nginx]# tar zxf nginx-1.26.1.tar.gz
[root@Nginx nginx]# cd nginx-1.26.1/ #开始编译新版本
[root@Nginx nginx-1.26.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module -- with-stream_realip_module
#只要make
#查看两个版本
[root@Nginx nginx-1.26.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
#把之前的旧版的nginx命令备份
[root@Nginx ~]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp nginx nginx.bak
#把新版本的nginx命令复制过去
[root@Nginx sbin]# \cp -f /root/nginx/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin
#检测一下有没有问题
[root@Nginx sbin]# nginx -t
[root@Nginx sbin]# kill -USR2 48732 #nginx worker ID
#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的
nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进
程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
#回滚
#如果升级的版本发现问题需要回滚,可以重新拉起旧版本的worker
[root@nginx-node1 sbin]# kill -HUP 55464
[root@nginx-node1 sbin]#
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2564 ? Ss 14:48 0:00 nginx: master process nginx
root 55490 0.0 0.1 9764 6656 ? S 14:50 0:00 nginx: master process nginx
nginx 55491 0.0 0.1 14228 5388 ? S 14:50 0:00 nginx: worker process
nginx 55497 0.0 0.1 14200 5124 ? S 14:58 0:00 nginx: worker process
root 55499 0.0 0.0 221664 2304 pts/0 S+ 14:58 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# kill -WINCH 55490
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55464 0.0 0.0 9864 2564 ? Ss 14:48 0:00 nginx: master process nginx
root 55490 0.0 0.1 9764 6656 ? S 14:50 0:00 nginx: master process nginx
nginx 55497 0.0 0.1 14200 5124 ? S 14:58 0:00 nginx: worker process
root 55501 0.0 0.0 221664 2304 pts/0 S+ 14:59 0:00 grep --color=auto nginx
[root@nginx-node1 ~]# curl -I 172.25.254.100
HTTP/1.1 200 OK
Server: nginx/1.24.0 # 旧版本回滚完成
Date: Fri, 16 Aug 2024 06:59:50 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Fri, 16 Aug 2024 04:47:28 GMT
Connection: keep-alive
ETag: "66bed9e0-267"
Accept-Ranges: bytes
Nginx服务的启动脚本
[root@nginx-node1 sbin]# ll
total 6004
-rwxr-xr-x 1 root root 6144176 Aug 20 22:04 nginx
[root@nginx-node1 sbin]#
[root@nginx-node1 sbin]#
[root@nginx-node1 sbin]# nginx
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55793 0.0 0.0 9896 2064 ? Ss 22:05 0:00 nginx: master process nginx
nginx 55794 0.0 0.1 14240 5136 ? S 22:05 0:00 nginx: worker process
root 55796 0.0 0.0 221664 2304 pts/0 S+ 22:05 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# nginx -s stop
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55799 0.0 0.0 221664 2304 pts/0 S+ 22:05 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# nginx -v
nginx version: nginx/1.26.2
[root@nginx-node1 sbin]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server #描述
After=syslog.target network-online.target remote-fs.target nss-lookup.target #指定哪些服务在启动Nginx的时候被激活
Wants=network-online.target #期望什么服务在启动时被激活
[Service]
Type=forking # 类型
PIDFile=/usr/local/nginx/logs/nginx.pid #PID的位置
ExecStartPre=/usr/local/nginx/sbin/nginx -t #执行启动命令之前检测配置文件是否有问题
ExecStart=/usr/local/nginx/sbin/nginx #启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@nginx-node1 sbin]# systemctl daemon-reload
[root@nginx-node1 sbin]# nginx -s stop
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55844 0.0 0.0 221664 2304 pts/0 S+ 22:07 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx-node1 sbin]# ps aux | grep nginx
root 55887 0.0 0.0 9892 2068 ? Ss 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 55888 0.0 0.1 14240 5012 ? S 22:07 0:00 nginx: worker process
root 55890 0.0 0.0 221664 2304 pts/0 S+ 22:07 0:00 grep --color=auto nginx
Nginx核心配置参数
配置文件说明
nginx 官方帮助文档:http://nginx.org/en/docs/
Nginx的配置文件的组成部分:
主配置文件:nginx.conf
子配置文件: include conf.d/*.conf
fastcgi, uwsgi,scgi 等协议相关的配置文件
mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
nginx 配置文件格式说明
- 配置文件由指令与指令块构成
- 每条指令以;分号结尾,指令与值之间以空格符号分隔
- 可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐
- 指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块
- include语句允许组合多个配置文件以提升可维护性
- 使用#符号添加注释,提高可读性
- 使用$符号使用变量
- 部分指令的参数支持正则表达式
Nginx 主配置文件的配置指令方式
directive value [value2 …];
注意
(1) 指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义,格式: set variable_name value;
引用变量:$variable_name
主配置文件结构:四部分
main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
…}
#http/https 协议相关配置段
http {
…}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
…}
#stream 服务器相关配置段
stream {
…}
全局配置参数优化调整
root和alias
[root@nginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 100000;
use epoll;
}
=======================================
#gzip on;
include "/usr/local/nginx/conf.d/*.conf"; #包含子配置文件
=======================================
[root@nginx-node1 ~]# mkdir -p /usr/local/nginx/conf.d
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
}
[root@nginx-node1 ~]# mkdir -p /data/web/html
[root@nginx-node1 ~]# echo www.haha.org > /data/web/html/index.html
[root@nginx-node1 ~]# 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-node1 ~]# nginx -s reload
在Windows解析文件上添加解析
172.25.254.100 www.haha.org
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前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
实验每一个匹配的步骤:
- 建立起想要访问的页面目录文件
- 在vhost文件写入相应的配置
- 在网页上测试
Nginx下的用户认证
# 创建认证文件
[root@nginx-node1 ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@nginx-node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$L3M5h8XO$aBF8qjVg6z5SHy1uBaqfp0
[root@nginx-node1 ~]# htpasswd -m /usr/local/nginx/.htpasswd lee
New password:
Re-type new password:
Adding password for user lee
[root@nginx-node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$L3M5h8XO$aBF8qjVg6z5SHy1uBaqfp0
lee:$apr1$QNC.iHFe$ejcSQ9QwIOsUzL4OJYZ/o0
[root@nginx-node1 ~]# mkdir /data/web/lee
[root@nginx-node1 ~]# echo lee > /data/web/lee/index.html
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
[root@nginx-node1 ~]# nginx -s reload
自定义错误页面
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx-node1 ~]# mkdir -p /data/web/errorpage
[root@nginx-node1 ~]# echo error page > /data/web/errorpage/40x.html
[root@nginx-node1 ~]# nginx -s reload
Nginx下的自定义日志
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/haha.org/error.log;
access_log /var/log/haha.org/access.log;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx-node1 ~]# mkdir /var/log/haha.org
[root@nginx-node1 ~]# nginx -s reload
[root@nginx-node1 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 nginx-node1.example.com
172.25.254.100 www.haha.org
[root@nginx-node1 ~]# curl www.haha.org
www.haha.org
[root@nginx-node1 ~]# cat /var/log/haha.org/access.log
172.25.254.100 - - [17/Aug/2024:14:37:52 +0800] "GET / HTTP/1.1" 200 14 "-" "curl/7.76.1"
[root@nginx-node1 ~]# curl www.haha.org/aaa
error page
[root@nginx-node1 ~]# cat /var/log/haha.org/error.log
2024/08/17 14:38:26 [error] 56859#0: *10258 open() "/data/web/html/aaa" failed (2: No such file or directory), client: 172.25.254.100, server: www.haha.org, request: "GET /aaa HTTP/1.1", host: "www.haha.org"
Nginx下的文件检测
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/haha.org/error.log;
access_log /var/log/haha.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx-node1 ~]# nginx -s reload
[root@nginx-node1 ~]# curl www.haha.org
www.haha.org
[root@nginx-node1 ~]# rm -rf /data/web/html/index.html
[root@nginx-node1 ~]# rm -rf /data/web/html/error
[root@nginx-node1 ~]# curl www.haha.org
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.26.2</center>
</body>
</html>
[root@nginx-node1 ~]# mkdir -p /data/web/html/error
[root@nginx-node1 ~]# echo error default > /data/web/html/error/default.html
[root@nginx-node1 ~]# curl www.haha.org
error default
Nginx长链接控制
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长,0表示禁止长连接,默认为75s
#通常配置在http字段作为站点全局配置keepalive_requests 数字;
#在一次长连接上所允许请求的资源的最大数量
#默认为100次,建议适当调大,比如:500
[root@nginx-node1 ~]# dnf install telnet -y
[root@nginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 65;
keepalive_requests 2;
[root@nginx-node1 ~]# nginx -s reload
[root@nginx-node1 ~]# telnet www.haha.org 80
Trying 172.25.254.100...
Connected to www.haha.org.
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.haha.org
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 07:12:19 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 07:09:20 GMT
Connection: keep-alive
ETag: "66c04ca0-e"
Accept-Ranges: bytes
www.haha.org
GET / HTTP/1.1
HOST: www.haha.org
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 07:12:34 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sat, 17 Aug 2024 07:09:20 GMT
Connection: close
ETag: "66c04ca0-e"
Accept-Ranges: bytes
www.haha.org
Connection closed by foreign host.
[root@nginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 65 60;
keepalive_requests 2;
#telent www.haha.org 80
Nginx下载服务器
[root@nginx-node1 ~]# mkdir /data/web/download
[root@nginx-node1 ~]#
[root@nginx-node1 ~]# dd if=/dev/zero of=/data/web/download/leefile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.032775 s, 3.2 GB/s
[root@nginx-node1 ~]#
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/haha.org/error.log;
access_log /var/log/haha.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
location /download {
root /data/web;
autoindex on;
}
}
[root@nginx-node1 ~]# nginx -s reload
[root@nginx-node1 ~]# curl www.haha.org/download/
<html>
<head><title>Index of /download/</title></head>
<body>
<h1>Index of /download/</h1><hr><pre><a href="../">../</a>
<a href="leefile">leefile</a> 17-Aug-2024 07:49 104857600
</pre><hr></body>
</html>
限制下载速度
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/haha.org/error.log;
access_log /var/log/haha.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /lee {
root /data/web;
auth_basic "input your password!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
location /download {
root /data/web;
autoindex on; #自动索引功能
autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间
autoindex_exact_size off; #计算文件确切大小(单位bytes),此为默认值,off只显示大概大小(单位kb、mb、gb)
limit_rate 1024k; #限速,默认不限速
}
}
nginx的高级配置
[root@nginx-node1 ~]# cd /usr/local/nginx/conf.d/
[root@nginx-node1 conf.d]# vim status.conf
server {
listen 80;
server_name status.haha.org;
root /data/web/html;
index index.html;
location /status {
stub_status;
}
}
[root@nginx-node1 conf.d]# nginx -s reload
指定用户访问状态页
[root@nginx-node1 conf.d]# vim status.conf
server {
listen 80;
server_name status.haha.org;
root /data/web/html;
index index.html;
location /status {
stub_status;
#auth_basic "login";
#auth_basic_user_file "/usr/local/nginx/.htpasswd";
allow 172.25.254.1; #Windows主机IP地址
deny all;
}
}
[root@nginx-node1 conf.d]# nginx -s reload
[root@nginx-node1 conf.d]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.100 nginx-node1.example.com
172.25.254.100 www.haha.org
172.25.254.100 status.haha.org
[root@nginx-node1 conf.d]# curl status.haha.org/status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.26.2</center>
</body>
</html>
数据压缩功能
此处的压缩仅是发送时的大小会有所改变,而存储在服务器上的文件大小没有改变
[root@nginx-node1 conf.d]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;
keepalive_timeout 65 60;
keepalive_requests 2;
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-node1 conf.d]# 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-node1 conf.d]# nginx -s reload
[root@nginx-node1 ~]# echo hello hello > /data/web/html/small.html[root@nginx-node1 ~]# du -sh /usr/local/nginx/logs/access.log
1.1M /usr/local/nginx/logs/access.log
[root@nginx-node1 ~]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html
[root@nginx-node1 ~]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 08:40:39 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 17 Aug 2024 08:37:51 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66c0615f-c"
Accept-Ranges: bytes
[root@nginx-node1 ~]# curl --head --compressed 172.25.254.100/big.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 08:40:46 GMT
Content-Type: text/html
Last-Modified: Sat, 17 Aug 2024 08:39:14 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: W/"66c061b2-106ac8"
Content-Encoding: gzip
Nginx中的变量
内置变量
[root@nginx-node1 conf.d]# vim var.conf
server {
listen 80;
server_name var.timinglee.org.org;
root /data/web/html;
index index.html;
location /var {
default_type text/html;
echo $remote_addr;
echo $args;
echo $is_args;
echo $document_root;
echo $document_uri;
echo $host;
echo $remote_port;
echo $remote_user;
echo $request_method;
echo $request_filename;
echo $request_uri;
echo $scheme;
echo $server_protocol;
echo $server_addr;
echo $server_name;
echo $server_port;
echo $http_user_agent;
echo $http_cookie;
echo $cookie_key2;
}
}
[root@nginx-node1 conf.d]# vim /etc/hosts
[root@nginx-node1 conf.d]# curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
172.25.254.100
name=lee
?
/data/web/html
/var
var.timinglee.org
36818
lee
GET
/data/web/html/var
/var?name=lee
http
HTTP/1.1
172.25.254.100
var.timinglee.org
80
curl/7.76.1
key1=lee,key2=lee1
lee1
[root@nginx-node1 conf.d]# vim zdyvar.conf
server {
listen 80;
server_name var.timinglee.org;
root /data/web/html;
index index.html;
location /var {
default_type text/html;
set $timinglee lee;
echo $timinglee;
}
}
[root@nginx-node1 conf.d]# nginx -s reload
[root@nginx-node1 conf.d]# curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
lee
自定义变量
set $name haha;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name xu.yara.org;
root /webdata/nginx/yara.org/xu;
location /var {
default_type text/html;
set $name yara;
echo $name;
set $web_port $server_port;
echo $web_port;
}
}
Nginx反向代理及动静分离实现
客户在需要访问后端真实的资源时,首先被定义到Nginx上,Nginx根据我们客户的请求,把资源分别定义到不同的位置,通过location来把资源定义到不同位置。
实验1:
1.另外两台主机172.25.254.10/20
2.关闭防火墙,关闭selinux
3.写入网页内容10:172.25.254.10 20:20,打开httpd服务,nginx测试访问
4.location中实现反向代理单台服务器
[root@web10 html]# echo 172.25.254.10 > /var/www/html/index.html
[root@web10 html]# systemctl restart httpd
[root@web20 html]# echo 172.25.254.20 > /var/www/html/index.html
[root@web20 html]# systemctl restart httpd
[root@nginx-node1 ~]# curl 172.25.254.10
172.25.254.10
[root@nginx-node1 ~]# curl 172.25.254.20
172.25.254.20
[root@web20 html]# vim /etc/httpd/conf/httpd.conf
Listen 8080 #监听端口修改为8080
[root@web20 html]# mkdir -p /var/www/html/static
[root@web20 html]# echo static 172.25.254.20 > /var/www/html/static/index.html
[root@web20 html]# systemctl restart httpd
[root@web10 html]# dnf install php -y
[root@web10 html]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@web10 html]# systemctl restart httpd
[root@nginx-node1 ~]# cd /usr/local/nginx/conf.d/
[root@nginx-node1 conf.d]# vim vhost.conf
server {
listen 80;
server_name www.haha.org;
location ~ \.php$ {
proxy_pass http://172.25.254.10:80;
}
location /static {
proxy_pass http://172.25.254.20:8080;
}
}
[root@nginx-node1 conf.d]# nginx -s reload
实验2:
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速 度。降低原来单个服务器的压力
server {
listen 80;
server_name www.haha.org;
location / {
proxy_pass http://172.25.254.10;
}
location ~ /static {
proxy_pass http://172.25.254.20:8080;
}
}
#后端web服务器必须要有相对于的访问URL
[root@nginx-node1 conf.d]# mkdir /var/www/html/static
[root@nginx-node1 conf.d]# echo static 172.25.254.20 > /var/www/html/static/index.html
[root@web10 html]# echo 172.25.254.30 > /var/www/html/index.html
#重启Nginx并访问测试:
[2024-08-17 17:09.35] ~
➤ curl www.haha.org/static/
static 172.25.254.20
[2024-08-17 17:09.39] ~
➤ curl www.haha.org
172.25.254.30
FastCGI
CGI的由来: 最早的Web服务器只能简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏 览器,也就是静态html文件,但是后期随着网站功能增多网站开发也越来越复杂,以至于出现动态技 术,比如像php(1995年)、java(1995)、python(1991)语言开发的网站,但是nginx/apache服务器并不 能直接运行 php、java这样的文件,apache实现的方式是打补丁,但是nginx缺通过与第三方基于协议实 现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户 的请求,处理完成后返回数据给Nginx并回收进程,最后nginx在返回给客户端,那这个约定就是通用网 关接口(common gateway interface,简称CGI),CGI(协议) 是web服务器和外部应用程序之间的接口 标准,是cgi程序和web服务器之间传递信息的标准化接口。
为什么会有FastCGI?
CGI协议虽然解决了语言解析器和 Web Server 之间通讯的问题,但是它的效率很低,因为 Web Server 每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候 再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性 能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请 求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率。
什么是PHP-FPM?
PHP-FPM(FastCGI Process Manager:
FastCGI进程管理器)是一个实现了Fastcgi的程序,并且提供进程管理的功能。
进程包括master进程和worker进程。master进程只有一个,负责监听端口,接受来自web server 的请求 worker进程一般会有多个
每个进程中会嵌入一个PHP解析器,进行PHP代码的处理。
FastCGI配置指令
fastcgi_pass address:port;
#转发请求到后端服务器,address为后端的fastcgi server的地址,可用位置:location, if in location
fastcgi_index name;
#fastcgi默认的主页资源,示例:fastcgi_index index.php;
fastcgi_param parameter value [if_not_empty];
#设置传递给FastCGI服务器的参数值,可以是文本,变量或组合,可用于将Nginx的内置变量赋值给自定义key
fastcgi_param REMOTE_ADDR $remote_addr; #客户端源IP
fastcgi_param REMOTE_PORT $remote_port; #客户端源端口
fastcgi_param SERVER_ADDR $server_addr; #请求的服务器IP地址
fastcgi_param SERVER_PORT $server_port; #请求的服务器端口
fastcgi_param SERVER_NAME $server_name; #请求的server name
Nginx默认配置示例:
location ~ \.php$ {
root /scripts;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #默认脚本路径
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; #此文件默认系统已提供,存放的相对路径为
prefix/conf
}
FastCGI实战案例
Nginx与php-fpm在同一服务器
./configure --prefix=/usr/local/nginx \
--add-module=/nginx/echo-nginx-module-0.63 \
--add-module=/nginx/memc-nginx-module-0.20 \
--add-module=/nginx/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
yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel -y
php高速缓存
[root@nginx-node1 ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
server {
listen 80;
server_name www.haha.org;
root /data/web/html;
index index.html;
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
root /data/web/php;
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
fastcgi_pass 172.25.254.100:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@nginx-node1 ~]# nginx -s reload
#测试结果
[root@nginx-node1 ~]# ab -n500 -c10 http://www.haha.org/index.php
Concurrency Level: 10
Time taken for tests: 0.255 seconds
Complete requests: 500
Failed requests: 0
nginx二次开发版本
[root@nginx-node1 ~]# killall -9 nginx
[root@nginx-node1 ~]# ps aux | grep nginx
nginx 198165 0.0 0.9 265356 17824 ? S 18:01 0:00 php-fpm: pool www
nginx 198166 0.0 0.9 265356 16288 ? S 18:01 0:00 php-fpm: pool www
root 204714 0.0 0.1 221664 2432 pts/3 S+ 20:52 0:00 grep --color=auto nginx
[root@nginx-node1 ~]# dnf -yq install gcc pcre-devel openssl-devel perl
[root@nginx-node1 ~]# useradd -r -s /sbin/nologin nginx
[root@nginx-node1 ~]# cd /usr/local/src
[root@nginx-node1 src]# wget https://openresty.org/download/openresty-1.25.3.1.tar.gz
[root@nginx-node1 src]# tar zxf openresty-1.25.3.1.tar.gz
[root@nginx-node1 src]# cd openresty-1.25.3.1/
[root@nginx-node1 openresty-1.25.3.1]# ./configure \
--prefix=/usr/local/openresty \
--with-stream_realip_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-http_sub_module \
--without-http_memcached_module \
--with-stream \
--with-stream_ssl_module
[root@nginx-node1 openresty-1.25.3.1]# gmake -j2 && gmake install
[root@nginx-node1 openresty-1.25.3.1]# cd /usr/local/openresty/
[root@nginx-node1 openresty]# ls
bin COPYRIGHT luajit lualib nginx pod resty.index site
[root@nginx-node1 openresty]# cd bin/
[root@nginx-node1 bin]# ls
md2pod.pl nginx-xml2pod openresty opm resty restydoc restydoc-index
[root@nginx-node1 bin]# ll
total 168
-rwxr-xr-x 1 root root 19185 Aug 19 21:12 md2pod.pl
-rwxr-xr-x 1 root root 15994 Aug 19 21:12 nginx-xml2pod
lrwxrwxrwx 1 root root 37 Aug 19 21:12 openresty -> /usr/local/openresty/nginx/sbin/nginx
-rwxr-xr-x 1 root root 63650 Aug 19 21:12 opm
-rwxr-xr-x 1 root root 36881 Aug 19 21:12 resty
-rwxr-xr-x 1 root root 14957 Aug 19 21:12 restydoc
-rwxr-xr-x 1 root root 8873 Aug 19 21:12 restydoc-index
[root@nginx-node1 bin]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=$PATH:/usr/local/nginx/sbin:/usr/local/php/bin:/usr/local/php/sbin:/usr/
local/openresty/bin
[root@nginx-node1 bin]# source ~/.bash_profile
[root@nginx-node1 bin]# openresty
[root@nginx-node1 bin]# netstat -antlulpe | grep 80
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 980 180281 191583/memcached
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 227829 215315/nginx: maste
tcp6 0 0 ::1:11211 :::* LISTEN 980 180282 191583/memcached