nginx编译安装
关闭selinux和防火墙
保证是一个纯净的实验环境
下载源码包
wgte http://nginx.org/download/nginx-1.24.0.tar.gz
下载依赖包
dnf install gcc pcre-devel zlib-devel openssl-devel -y
创建一个不可以远程登录的没有家目录的用户
useradd -s /sbin/nologin -M nginx
解压安装包
tar zxf nginx-1.24.0.tar.gz
关闭debug
cd 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_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
--user=nginx # 指定nginx运行用户
--group=nginx # 指定nginx运行组
--with-http_ssl_module # 支持https://
--with-http_v2_module # 支持http版本2
--with-http_realip_module # 支持ip透传
--with-http_stub_status_module # 支持状态页面
--with-http_gzip_static_module # 支持压缩
--with-pcre # 支持正则
--with-stream # 支持tcp反向代理
--with-stream_ssl_module # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
安装nginx 安装速度有电脑性能决定
make install
nginx完成安装以后,有四个主要的目录
[root@nginx nginx]# ls /usr/local/nginx/
conf html logs sbin
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他 的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params 两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀 去掉即可。
html:目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web 文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比 如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
验证版本及编译参数
vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
source ~/.bash_profile
执行完后,在启动nginx服务只需要输入nginx就可以了
查看nginx版本
nginx -V
平滑升级回滚
升级
下载高版本的nginx并解压
wget http://nginx.org/download/nginx-1.26.2.tar.gz
tar zxf nginx-1.26.2.tar.gz
关闭debug
cd nginx-1.24.0/
将echo-nginx-module-0.63.tar.gz文件拖入虚拟机并解压
编译安装的程序
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --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无需要make install
make
把之前的旧版的nginx命令备份
cd /usr/local/nginx/sbin/
cp nginx nginx.24
把新版本的nginx命令复制过去
\cp -f /root/nginx/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin
检测一下有没有问题
nginx -t
kill -USR2 1567 #nginx worker ID
#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的 nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进 程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
回收旧版本
kill -WINCH 1567
查看版本
回滚
[root@nginx sbin]#mv nginx nginx.26
[root@nginx sbin]# mv nginx.24 nginx
kill -HUP 1567
回收新版本
kill -WINCH 1575
查看版本
nginx的启动文件编写
先关闭nginx服务
nginx -s stop
编写启动文件
保存退出后,我们就可以使用systemctl restart nginx等命令 和自启动了
nginx全局配置
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
编辑子配置文件
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
测试
映射
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
location详细使用
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立 即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头 #对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写 不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
= 后面跟的是文件不是目录
优先级
以目录为目标
(~* | ~) > 不带符号 > ^~ > =
以文件为目标
= > (~* | ~) > 不带符号 > ^~
加密
生成账户密码
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd haha
在配置文件中写入加密
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
重定向错误文件
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
测试,我们访问一个不存在的文件,访问后直接跳转到自定义的报错页面
定义错误日志和成功日志
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
测试
自动检测文件是否存在
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]#echo error default > /data/web/html/error/default.html
[root@nginx ~]# nginx -s reload
长连接配置
主配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
下载telnet
dnf install telnet -y
测试
下载服务器配置
子配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
在浏览器测试访问
下载文件测试
配置nginx的状态页
新建子配置文件
[root@nginx ~]# vim /usr/local/nginx/conf.d/status.conf
测试
Active connections: #当前处于活动状态的客户端连接数 #包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active
Nginx 压缩功能
压缩不是在服务器上对文件进行压缩,而是在传输的过程的对文件进行压缩,会消耗cpu的性能,所以不支持小文件压缩
开启压缩功能
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
制作一个小文件
[root@nginx ~]# echo hahahaha >/data/web/html/small.html
复制日志文件作为大文件用来压缩
cat /usr/local/nginx/logs/access.log > /data/web/html/big.html
测试
小文件
大文件
nginx变量使用
变量在配置文件中引用
变量可以分为内置变量和自定义变量
内置变量由nginx系统设定,自定义变量由用户设定
$remote_addr; 远程主机的IP地址
$is_args; 如果有显示?否则显示为空
$host; 请求的名字
$remote_port; 远程连接主机所用的端口
$remote_user; 登录的用户
$request_method; 请求的资源
$request_filename; 最终访问的文件
$request_uri; 包含请求参数的原始URI
$scheme; 请求的协议
$server_addr; 服务器的IP
Nginx rewrite相关功能
if判断
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
文件不存在时
[root@nginx ~]# curl www.li.org/test2/index.html
文件存在
set指令
指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key 另外set定义格式为set $key value,value可以是text, variables和两者的组合。
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl www.li.org/break
break 指令
用于中断当前相同作用域(location)中的其他Nginx配置
与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效
位于后面的 ngx_http_rewrite_module 模块中指令就不再执行
Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置、该指令可以在server块和locationif块中使用
注意: 如果break指令在location块中后续指令还会继续执行,只是不执行 ngx_http_rewrite_module 模块的指令,其它指令还会执行
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
[root@nginx ~]# curl www.li.org/break
[root@nginx ~]# curl -A "firefox" www.li.org/break
return 指令
return用于完成对请求的处理,并直接向客户端返回响应状态码,比如:可以指定重定向URL(对于特殊重 定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配 置都将不被执行,return可以在server、if 和 location块进行配置
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl -I www.li.org/return
curl 不支持跳转功能,可以使用本机的浏览器输入地址后自动跳转到百度
www.li.org/return
创建文件后在测试
[root@nginx ~]# mkdir -p /data/web/html/return
[root@nginx ~]# curl -I www.li.org/return
rewrite 指令
redirect---------------------------------------#临时重定向,重写完成后以临时重定向方式直接返回 重写后生成的新URL给客户端
#由客户端重新发起请求;使用相对路径,或者http://或 https://开头,状态码:302
permanent-----------------------------------#重写完成后以永久重定向方式直接返回重写后生成的 新URL给客户端
#由客户端重新发起请求,状态码:301
break;----------------------------------------#重写完成后,停止对当前URL在当前location中后续的 其它重写操作
#而后直接跳转至重写规则配置块之后的其它配置,结 束循环,建议在location中使用
#适用于一个URL一次重写
last-------------------------------------------#重写完成后,停止对当前URI在当前location中后续的其 它重写操作,
#而后对新的URL启动新一轮重写检查,不建议在 location中使用
#适用于一个URL多次重写,要注意避免出现超过十次 以及URL重写后返回错误的给用户
rewirte : 域名永久与临时重定向
利用nginx的rewrite的指令,可以实现url的重新跳转,rewrite有四种不同的flag,分别是redirect(临时 重定向302)、permanent(永久重定向301)、break和last。其中前两种是跳转型的flag,后两种是代理型
-
跳转型指由客户端浏览器重新对新地址进行请求
-
代理型是在WEB服务器内部实现跳转
永久
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl -I www.li.org
临时
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl -I www.li.org
rewrite : break 与 last
创建测试文件
[root@nginx ~]# mkdir /data/web/html/{test1,test2,break,last} -p
[root@nginx ~]# echo test1 > /data/web/html/test1/index.html
[root@nginx ~]# echo test2 > /data/web/html/test2/index.html
[root@nginx ~]# echo last > /data/web/html/last/index.html
[root@nginx ~]# echo break > /data/web/html/break/index.html
[root@nginx html]# vim /usr/local/nginx/conf.d/vhosts.conf
遇到break之后,不跳出location但是后面的所有东西都不在查询
遇到last ,跳出该location 但会接着查询后面的location
测试
全栈加密
创建秘钥和证书
[root@nginx html]# cd /usr/local/nginx/
[root@nginx nginx]# mkdir certs
[root@nginx nginx]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/li.org.key -x509 -days 365 -out /usr/local/nginx/certs/li.org.crt
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhosts.conf
测试 (如果测试不成功可能是浏览器的缓存)
http自动跳转到https
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhosts.conf
直接在浏览器上输入域名或者Http://www.li.org 然后回车
回车后页面直接跳转到https
访问的文件不存在就重定向到主界面
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhosts.conf
测试
防盗链
防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标 记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗 链,referer就是之前的那个网站域名
当一个人建立了一个网站,但是他网站的资源却是链接到我们网站的时候
例如:
当我们的网页中有一个图片
查看图片
而别人却做了一个网站,里面有一个链接,直接连接到我们网站的图片,直接不劳而获
打开该网页文件
点击 "点击"将直接跳转到我们的网页
这时候我们就要做一些措施防止此类事情的发生
点击 "点击"后也无法访问网页
让盗图的人的连接访问到我们指定的位置
测试
nginx的反向代理
nginx主机
[root@nginx html]# vim /usr/local/nginx/conf.d/vhosts.conf
server1主机
下载httpd
yum install httpd -y
写入数据做测试
echo server1 172.25.119.110 > /var/www/html/index.html
server2 主机
下载httpd
yum install httpd -y
创建文件写入数据修改端口,方便测试观察
[root@server2 ~]# mkdir /var/www/html/static
[root@server2 ~]# echo server2 172.25.119.120 > /var/www/html/static/index.html
[root@server2 ~]# vim /etc/httpd/conf/httpd.conf
测试
动静分离
在server上下载动态服务php
yum install php -y
写配置
[root@server1 html]# vim /var/www/html/index.php
<?php
phpinfo();
?>[root@server1 html]# systemctl restart httpd
nginx主机
[root@nginx html]# vim /usr/local/nginx/conf.d/vhosts.conf
[root@nginx html]# nginx -s reload
访问测试
反向代理:缓存功能
在主配置文件中开启缓存
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
子配置文件
[root@nginx html]# vim /usr/local/nginx/conf.d/vhosts.conf
proxy_cache_valid any 1m; ------其他数据缓存响应时间
测试
开启缓存前
开启缓存后
缓存的内容存放
反向代理:负载均衡
子配置文件(七层)
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhosts.conf
测试 (默认轮询)
hash
ip hash
测试
uri hash
测试
cookie hash
测试 (不写cookie,将以轮询方式调度
down强制下线
测试
四层
以dns服务器为例
下载bind (两台server主机都要做)
yum install -y bind
修改配置文件
server1
[root@server1 ~]# vim /etc/named.conf
[root@server1 ~]# vim /etc/named.rfc1912.zones
[root@server1 ~]# cd /var/named/
[root@server1 named]# cp named.localhost li.org.zone -p
[root@server1 named]# vim li.org.zone
[root@server1 named]# systemctl restart named
[root@server1 named]# dig www.li.org @172.25.119.110
server2
主配置文件
[root@server2 ~]# vim /etc/named.rfc1912.zones
[root@server2 ~]# cd /var/named/
[root@server2 named]# cp named.localhost li.com.zone -p
[root@server2 named]# vim li.org.zone
[root@server2 named]# systemctl restart named
[root@server2 named]# dig www.li.org @172.25.119.120
nginx
定义全局的子配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# mkdir /usr/local/nginx/tcpconf.d/
测试
fastcgi
源码编译
编译需要的包
memc-nginx-module-0.20.tar.gz
php-8.3.9.tar.gz
srcache-nginx-module-0.33.tar.gz
全部解压
对nginx重新编译 (也可以等到做php告诉缓存的时候在编译)
删除之前的
rm -rf /usr/local/nginx
编译新的NGINX
./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 install
安装php
下载安装依赖包
yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel
部分包安装不上
用 yum whatprovides */包名 查找 需要安装的基本都是 包名+devel+版本
oniguruma-devel 这个包需要再网上下载
wget https://repo.almalinux.org/almalinux/9/CRB/x86_64/os/Packages/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
在用yum安装即可
编码
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
--prefix=/usr/local/php \ #安装路径
--with-config-file-path=/usr/local/php/etc \ #指定配置路径
--enable-fpm \ #用cgi方式启动程序
--with-fpm-user=nginx \ #指定运行用户身份
--with-fpm-group=nginx \
--with-curl \ #打开curl浏览器支持
--with-iconv \ #启用iconv函数,转换字符编码
--with-mhash \ #mhash加密方式扩展库
--with-zlib \ #支持zlib库,用于压缩http压缩传输
--with-openssl \ #支持ssl加密
--enable-mysqlnd \ #mysql数据库
--with-mysqli \
--with-pdo-mysql \
--disable-debug \ #关闭debug功能
--enable-sockets \ #支持套接字访问
--enable-soap \ #支持soap扩展协议
--enable-xml \ #支持xml
--enable-ftp \ #支持ftp
--enable-gd \ #支持gd库
--enable-exif \ #支持图片元数据
--enable-mbstring \ #支持多字节字符串
--enable-bcmath \ #打开图片大小调整,用到zabbix监控的时候用到了这个 模块
--with-fpm-systemd #支持systemctl 管理cgi
安装 过程比较长
make install
php相关配置优化
[root@nginx php-8.3.9]# cd /usr/local/php/etc/
[root@nginx etc]# ls
php-fpm.conf.default php-fpm.d
[root@nginx etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@nginx etc]# vim php-fpm.conf
指定pid文件存放位置
[root@nginx etc]# cd php-fpm.d/
[root@nginx php-fpm.d]# cp www.conf.default www.conf -p
生成主配置文件
如果编译的时候没有加参数--with-config-file-path=/usr/local/php/etc 配置文件就在/usr/local/php/lib/ 如果加了就在/usr/local/php/etc
[root@nginx php-8.3.9]# cp php.ini-production /usr/local/php/lib/php.ini
修改时区
vim /usr/local/php/lib/php.ini
生成启动文件
[root@nginx php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
[root@nginx php-8.3.9]# vim /lib/systemd/system/php-fpm.service
配置环境变量
[root@nginx php-8.3.9]# cd /usr/local/php/bin/
[root@nginx bin]# vim ~/.bash_profile
[root@nginx bin]# source ~/.bash_profile
定义子配置文件
[root@nginx bin]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx bin]# mkdir /usr/local/nginx/conf.d/
[root@nginx bin]# vim /usr/local/nginx/conf.d/vhosts.conf
准备php测试页面
[root@nginx ~]# cat /data/php/index.php
<?php
phpinfo();
?>
测试
安装memcache模块
解压安装包
tar zxf memcache-8.2.tgz
下载autoconf
cd memcache-8.2/
yum install autoconf
phpize
./configure && make && make install
复制测试文件到nginx发布目录中
[root@nginx memcache-8.2]# cp example.php memcache.php /data/php/
[root@nginx memcache-8.2]# vim /data/php/memcache.php
配置php加载memcache模块
[root@nginx memcache-8.2]# vim /usr/local/php/lib/php.ini
systemctl reload php-fpm
部署memcached
[root@nginx ~]# yum install memcached -y
[root@nginx ~]# systemctl enable --now memcached.service
cat /etc/sysconfig/memcached
测试 MEMCACHE INFO (li.org) 查看命中效果
li.org/example.php 不断刷新
php高速缓存
在我们安装的nginx中默认不支持memc和srcache功能,需要借助第三方模块来让nginx支持此功能,所以nginx需要重新编译 ,这就是为什么我们之前要重新编码nginx
编辑所需软件包
srcache-nginx-module-0.33.tar.gz
memc-nginx-module-0.20.tar.gz
编码内容
./configure --prefix=/apps/nginx --user=nginx -- group=nginx --with-http_ssl_module --with-http_v2_module --withhttp_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module -- add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module0.33
安装
make && make install
编辑子配置文件
[root@nginx logs]# vim /usr/local/nginx/conf.d/vhosts.conf
systemctl start nginx.service
ab -n500 -c10 http://www.li.org/index.php
nginx 二次开发版本
openresty
openresty与nginx只能运行一个,先把nginx关闭
systemctl stop nginx
dnf -yq install gcc pcre-devel openssl-devel perl
创建一个没有家目录不能远程登录的用户,如果有就不用创建
[root@nginx ~]#useradd -r -s /sbin/nologin nginx
[root@nginx ~]#cd /usr/local/src
[root@nginx ~]# wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
[root@nginx ~]# tar -zxf openresty-1.17.8.2.tar.gz
[root@nginx ~]# cd openresty-1.17.8.2
编码
[root@nginx bin]# ./configure --prefix=/usr/local/openresty --user=nginx --group=nginx --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
安装
[root@nginx bin]# make && make install
环境变量
[root@nginx bin]# vim ~/.bash_profile
[root@nginx bin]# source ~/.bash_profile
查看版本
开启
[root@nginx bin]# openresty
查看端口
[root@nginx bin]# netstat -antlulpe |grep 80
openresty与NGINX的用法基本一致