目录
nginx安装
[root@nginx2 ~]# vmset.sh eth0 172.25.254.100 nginx.example.org
#解决依赖
[root@nginx2 ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@nginx ~]# tar zxvf nginx-1.26.1.tar.gz
#进入配置目录
[root@nginx2 ~]# cd nginx-1.24.0/
[root@nginx ~]# ./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
#编译
[root@nginx2 nginx-1.24.0]# make -j2
#拷贝文件到指定的位置
[root@nginx2 nginx-1.24.0]# make install
#创建用户
[root@nginx2 sbin]# useradd -s /sbin/nologin -M nginx
#启动nginx 默认一个主进程,一个worker进程
[root@nginx2 sbin]# ./nginx
[root@nginx2 sbin]# ps aux |grep nginx
root 40506 0.0 0.0 9860 932 ? Ss 16:19 0:00 nginx: master process ./nginx
nginx 40507 0.0 0.1 13756 4864 ? S 16:19 0:00 nginx: worker process
root 40509 0.0 0.0 221664 2376 pts/1 S+ 16:19 0:00 grep --color=auto nginx
#查看端口
[root@nginx2 sbin]# netstat -antlupe | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 69139 40506/nginx: master
#测试
[root@nginx ~]# curl -I 172.25.254.200
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 15 Aug 2024 08:21:35 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 08:17:34 GMT
Connection: keep-alive
ETag: "66bdb99e-267"
Accept-Ranges: bytes
#查看文件大小
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# du -sh nginx
5.5M nginx
#关闭nginx
[root@nginx2 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@nginx2 ~]# netstat -antlupe | grep nginx
#重启
[root@nginx2 ~]# /usr/local/nginx/sbin/nginx -s restart
最小化安装
#删除软件
[root@nginx2 nginx-1.24.0]# rm -rf /usr/local/nginx/
#删除之前编译的东西
[root@nginx2 nginx-1.24.0]# make clean
rm -rf Makefile objs
#检查
[root@nginx ~]# ./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
#编译
[root@nginx ~]# make && make install
#大小
[root@nginx2 nginx-1.24.0]# du -sh /usr/local/nginx/sbin/nginx
1.2M /usr/local/nginx/sbin/nginx
关闭debug功能
[root@nginx2 nginx-1.24.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
配置环境
[root@nginx2 ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@nginx2 ~]# source ~/.bash_profile
设定开机启动脚本
nginx默认情况下没有启动文件
#进入目录
[root@nginx ~]# cd /usr/local/nginx/sbin/
#创建文件
[root@nginx 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
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.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 sbin]# systemctl daemon-reload
#检测
[root@nginx sbin]# nginx -s stop
[root@nginx sbin]# ps aux | grep nginx
root 52270 0.0 0.0 221664 2372 pts/1 S+ 15:51 0:00 grep --color=auto nginx
[root@nginx sbin]# nginx
[root@nginx sbin]# ps aux | grep nginx
root 52272 0.0 0.0 9896 952 ? Ss 15:51 0:00 nginx: master process nginx
nginx 52273 0.0 0.1 13808 4964 ? S 15:51 0:00 nginx: worker process
root 52275 0.0 0.0 221664 2312 pts/1 S+ 15:51 0:00 grep --color=auto nginx
查看配置文件
[root@nginx2 ~]# cd /usr/local/nginx/
[root@nginx2 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
平滑升级
[root@nginx2 ~]# tar zxf echo-nginx-module-0.63.tar.gz
[root@nginx2 ~]# tar zxf nginx-1.26.1.tar.gz
[root@nginx2 ~]# cd nginx-1.26.1/
#编译目录的时候,把niginx模块也加进去,让其内部可以运行
[root@nginx2 nginx-1.26.1]# ./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 --add-module=/root/srcache-nginx-module-0.33 --add-module=/root/memc-nginx-module-0.20
#不要install
[root@nginx2 nginx-1.26.1]# make
#编译好后生成obj文件夹,里面有nginx
[root@nginx2 nginx-1.26.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@nginx2 nginx-1.26.1]# cd objs/
[root@nginx2 objs]# ls
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
#进入运行环境
[root@nginx2 objs]# cd /usr/local/nginx/sbin/
[root@nginx2 sbin]# ll
total 1216
-rwxr-xr-x 1 root root 1242720 Aug 15 21:47 nginx
#把旧的nginx备份一份nginx.24
[root@nginx2 sbin]# cp nginx nginx.24
#用26的把24的覆盖
[root@nginx2 sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/nginx
[root@nginx2 sbin]# ls
nginx nginx.24
老的nginx的进程,新的运行不了

新的怎么启动?用命令
#旧的进程
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9155 0.0 0.0 9860 932 ? Ss 22:06 0:00 nginx: master process nginx
nginx 9156 0.0 0.1 13756 5408 ? S 22:06 0:00 nginx: worker process
root 9193 0.0 0.0 221796 2360 pts/1 S+ 22:14 0:00 grep --color=auto nginx
#只显示id
[root@nginx2 sbin]# pidof nginx
9156 9155
#回收旧版本
[root@nginx2 sbin]# kill -USR2 9155
[root@nginx2 sbin]# kill -WINCH 9155
更改版本变量
[root@nginx2 nginx-1.26.1]# cd src/core/
[root@nginx2 core]# vim nginx.h
#define nginx_version 1026001
#define NGINX_VERSION "1.26.1"
#define NGINX_VER "example/" NGINX_VERSION
平滑回滚
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9155 0.0 0.0 9860 2568 ? Ss 22:06 0:00 nginx: master process nginx
root 9195 0.0 0.1 9896 5924 ? S 22:15 0:00 nginx: master process nginx
nginx 9196 0.0 0.1 13792 5300 ? S 22:15 0:00 nginx: worker process
root 9219 0.0 0.0 221796 2372 pts/1 S+ 22:37 0:00 grep --color=auto nginx
[root@nginx2 sbin]# kill -HUP 9155
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi
root 9155 0.0 0.0 9860 2568 ? Ss 22:06 0:00 nginx
root 9195 0.0 0.1 9896 5924 ? S 22:15 0:00 nginx
nginx 9196 0.0 0.1 13792 5300 ? S 22:15 0:00 nginx
nginx 9225 0.0 0.1 13756 4868 ? S 22:40 0:00 nginx
root 9227 0.0 0.0 221796 2376 pts/1 S+ 22:41 0:00 grep
[root@nginx2 sbin]# kill -WINCH 9195
[root@nginx2 sbin]# curl -I 172.25.254.200
把名字该回去,然后删掉
[root@nginx2 sbin]# ls
nginx nginx.old
[root@nginx2 sbin]# cp nginx nginx.new
[root@nginx2 sbin]# ls
nginx nginx.new nginx.old
[root@nginx2 sbin]# \cp -f nginx.old nginx
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9155 0.0 0.0 9860 2568 ? Ss 22:06 0:00 nginx: master process nginx
root 9195 0.0 0.1 9896 6424 ? S 22:15 0:00 nginx: master process nginx
nginx 9225 0.0 0.1 13756 5408 ? S 22:40 0:00 nginx: worker process
root 9258 0.0 0.0 221796 2372 pts/1 S+ 22:45 0:00 grep --color=auto nginx
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9155 0.0 0.0 9860 2568 ? Ss 22:06 0:00 nginx: master process nginx
root 9195 0.0 0.1 9896 6424 ? S 22:15 0:00 nginx: master process nginx
nginx 9225 0.0 0.1 13756 5408 ? S 22:40 0:00 nginx: worker process
root 9260 0.0 0.0 221796 2376 pts/1 S+ 22:46 0:00 grep --color=auto nginx
[root@nginx2 sbin]# kill -9 9195
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9155 0.0 0.0 9860 2568 ? Ss 22:06 0:00 nginx: master process nginx
nginx 9225 0.0 0.1 13756 5408 ? S 22:40 0:00 nginx: worker process
root 9262 0.0 0.0 221796 2352 pts/1 S+ 22:47 0:00 grep --color=auto nginx
常用参数
-v
#版本参数
[root@nginx2 sbin]# nginx -v
nginx version: example/1.26.1
#详细参数
[root@nginx2 sbin]# nginx -V
nginx version: example/1.26.1
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 --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
-t
检测配置文件语法
[root@nginx2 sbin]# 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@nginx2 sbin]# nginx -T
[root@nginx2 sbin]# 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
# configuration file /usr/local/nginx/conf/nginx.conf:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
.....
reload
#在不关闭进程的情况下,重新加载配置
[root@nginx2 sbin]# nginx -s reload
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9276 0.0 0.0 10028 3488 ? Ss 22:51 0:00 nginx: master process nginx
nginx 9286 0.0 0.1 13932 5068 ? S 22:55 0:00 nginx: worker process
root 9289 0.0 0.0 221796 2372 pts/1 S+ 22:55 0:00 grep --color=auto nginx
[root@nginx2 sbin]# nginx -s reload
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9276 0.0 0.0 9912 3500 ? Ss 22:51 0:00 nginx: master process nginx
nginx 9291 0.0 0.1 13808 4924 ? S 22:55 0:00 nginx: worker process
root 9293 0.0 0.0 221796 2360 pts/1 S+ 22:55 0:00 grep --color=auto nginx
-g
#没有指定的时候,之开启一个master和一个worker
[root@nginx2 sbin]# nginx -s stop
[root@nginx2 sbin]# nginx
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9296 0.0 0.0 9896 952 ? Ss 22:58 0:00 nginx: master process nginx
nginx 9297 0.0 0.1 13808 4944 ? S 22:58 0:00 nginx: worker process
root 9301 0.0 0.0 221796 2372 pts/1 S+ 22:59 0:00 grep --color=auto nginx
#记得注释,不然你指定不了
[root@nginx2 sbin]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#关机
[root@nginx2 sbin]# nginx -s stop
#指定
[root@nginx2 sbin]# nginx -g "worker_processes 6;"
[root@nginx2 sbin]# nginx
[root@nginx2 sbin]# ps aux | grep nginx
avahi 882 0.0 0.1 15528 6224 ? Ss 21:41 0:00 avahi-daemon: running [nginx2.local]
root 9371 0.0 0.0 9896 952 ? Ss 23:01 0:00 nginx: master process nginx -g worker_processes 6;
nginx 9372 0.0 0.1 13808 4948 ? S 23:01 0:00 nginx: worker process
nginx 9373 0.0 0.1 13808 4876 ? S 23:01 0:00 nginx: worker process
nginx 9374 0.0 0.1 13808 4812 ? S 23:01 0:00 nginx: worker process
nginx 9375 0.0 0.1 13808 4948 ? S 23:01 0:00 nginx: worker process
nginx 9376 0.0 0.1 13808 4884 ? S 23:01 0:00 nginx: worker process
nginx 9377 0.0 0.1 13808 4896 ? S 23:01 0:00 nginx: worker process
root 9381 0.0 0.0 221796 2356 pts/1 S+ 23:03 0:00 grep --color=auto nginx
全局配置参数
#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路
径,日志路径等。
user nginx nginx;
worker_processes 1; #启动工作进程数数量
events { #events #设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多
个网络连接,使用哪种事件驱动模型 #处理请求,每个工作进程可以同时支持的
最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
worker_connections 1024; #设置单个nginx工作进程可以接受的最大并发,作为web服务器
的时候最大并发数为 #worker_connections *
worker_processes,作为反向代理的时候为
#(worker_connections * worker_processes)/2
}
http { #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格
式定义等绝大多数功能和第三方模块都 #可以在这设置,http块可
以包含多个server块,而一个server块中又可以包含多个location块,
#server块可以配置文件引入、MIME-Type定义、日志自定义、是
否启用sendfile、连接超时时间和 #单个链接的请求上限等。
include mime.types;
default_type application/octet-stream;
sendfile on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是
否使用
#sendfile系统调用来传输文件
#sendfile系统调用在两个文件描述符之间直接传递数据(完全在
内核中操作)
#从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率
很高,被称之为零拷贝,
#硬盘 >> kernel buffer (快速拷贝到kernelsocket
buffer) >>协议栈。
keepalive_timeout 65; #长连接超时时间,单位是秒
server { #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多
个location模块
#比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个
server 可以使用一个端口比如都使用 #80端口提供web服务
listen 80; #配置server监听的端口
3.2 全局配置
Main 全局配置段常见的配置指令分类
正常运行必备的配置
优化性能相关的配置
用于调试及定位问题相关的配置
事件驱动相关的配置
全局配置说明:
server_name localhost; #本server的名称,当访问此名称的时候nginx会调用当前serevr
内部的配置进程匹配。
location / { #location其实是server的一个指令,为nginx服务器提供比较
多而且灵活的指令
#都是在location中体现的,主要是基于nginx接受到的请求字符
串
#对用户请求的UIL进行匹配,并对特定的指令进行处理
#包括地址重定向、数据缓存和应答控制等功能都是在这部分实现
#另外很多第三方模块的配置也是在location模块中配置。
root html; #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使
用绝对路径配置。
index index.html index.htm; #默认的页面文件名称
}
error_page 500 502 503 504 /50x.html; #错误页面的文件名称
location = /50x.html { #location处理对应的不同错误码的页面定
义到/50x.html
#这个跟对应其server中定义的目录下。
root html; #定义默认页面所在的目录
}
}
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#tcp代理配置,1.9版本以上支持
#stream {
# ...
# } stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}
优化调整
cpu的核心绑定
[root@nginx2 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity 0001 0010;
[root@nginx2 ~]# nginx -s reload
[root@nginx2 ~]# ps aux | grep nginx
avahi 885 0.0 0.1 15532 6348 ? Ss 17:30 0:00 avahi-daemon: running [nginx2.local]
root 3231 0.0 0.1 10028 3560 ? Ss 17:42 0:00 nginx: master process nginx
nginx 3259 0.0 0.1 13932 4968 ? S 17:44 0:00 nginx: worker process
nginx 3260 0.0 0.1 13932 4920 ? S 17:44 0:00 nginx: worker process
root 3264 0.0 0.0 221796 2372 pts/0 S+ 17:44 0:00 grep --color=auto nginx
#可以看见这个时候有两个worker
event
#最多能打开1024个文字符
[root@nginx2 ~]# ulimit -a
open files (-n) 1024
#更改
[root@nginx2 ~]# vim /etc/security/limits.conf
# End of file
nginx - nofile 10000
[root@nginx2 nginx]# sudo -u nginx ulimit -a
open files (-n) 10000
#更改配置文件
[root@nginx2 nginx]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000;
}
#压力测试
[root@nginx2 nginx]# ab -n 10000 -c 5000 http://172.25.254.200/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.25.254.200 (be patient)
socket: Too many open files (24)
创建一个站点
[root@nginx2 nginx]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000;
use epoll;
}
#gzip on; 子配置文件路径,写在后面不生效噢
include "/usr/local/nginx/conf.d/*.conf";
[root@nginx2 nginx]# mkdir -p /usr/local/nginx/conf.d
[root@nginx2 nginx]# mkdir -p /data/web/html
[root@nginx2 nginx]# echo www.example.org > /data/web/html/index.html
[root@nginx2 nginx]# vim /usr/local/nginx/conf.d/vhost.conf
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
}
#重新加载nginx
[root@nginx2 nginx]# nginx -s reload
#在windows里面做域名解析
172.25.254.200 www.example.org
root
[root@nginx2 nginx]# mkdir /data/web/test1 -p
[root@nginx2 nginx]# echo /data/web/test1 > /data/web/test1/index.html
[root@nginx2 nginx]# vim /usr/local/nginx/conf.d/vhost.conf
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
#它不会管前面的root噢~
location /test1/ {
root /data/web;
}
}
[root@nginx2 nginx]# nginx -s reload
#查看日志
[root@nginx2 nginx]# tail /usr/local/nginx/logs/error.log
alias别名
输入test2实际访问的是alias里面的地址
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
location /test1/ {
root /data/web;
}
location /test2 {
alias /data/web/test1;
}
}
location的详细使用
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
#对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号#匹配优先级从高到低:
~/~*
不带符号
^~
=
#/data/web/test/index.html
location /test {
root /data/web;
}
#实际目录 /data/web/html/test/index.html
location test {
root /data/web;
}
nginx账户认证功能
#第一步:创建认证文件
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
##.htpasswd隐藏文件,安全 记得结尾加用户
##如果只是增加用户,要把-c去掉,否则会覆盖
[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
admin:$apr1$OWjOtehT$SvrN5paUET5pg.IOyOK030
lee:$apr1$3D6uKBrp$BmJGKK1uWzloILWxnZ8Cq0
#创建默认配置文件
[root@nginx ~]# mkdir -p /data/web
[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
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
location /lee {
root /data/web;
auth_basic "login password";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
[root@nginx ~]# nginx -s reload
nginx自定义错误页面
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
#404状态码重定向
error_page 404 /40x.html;
location /lee {
root /data/web;
auth_basic "login password";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx ~]# mkdir -p /data/web/errorpage
[root@nginx ~]# echo errorpage > /data/web/errorpage/40x.html
nginx自定义日志
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
#使用参数独立日志
error_log /var/log/example.org/error.log;
access_log /var/log/example.org/access.log;
[root@nginx ~]# mkdir /var/log/example.org
[root@nginx ~]# nginx -s reload
nginx中的文件检测
检测文件是否存在
server{
listen 80;
server_name www.example.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/example.org/error.log;
access_log /var/log/example.org/access.log;
#先检测路径,路径下的html,路径下的index.html 然后错误页面
try_files $uri $uri.html $uri/index.html /error/default.html;
500报错
[root@nginx ~]# curl nginx.example.org
exaple.org
[root@nginx ~]# rm -rf /data/web/html/index.html
[root@nginx ~]# curl nginx.example.org
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@ng
建立文件
[root@nginx ~]# mkdir /data/web/html/error
[root@nginx ~]# echo error default > /data/web/html/error/default.html
[root@nginx ~]#
[root@nginx ~]#
[root@nginx ~]# curl nginx.example.org
error default
长连接设定
keepalive_timeout timeout [header_timeout]; #设定保持连接超时时长,0表示禁止长连接,
默认为75s
#通常配置在http字段作为站点全局配置
keepalive_requests 数字; #在一次长连接上所允许请求的资源的最大数量
#默认为100次,建议适当调大,比如:500
#下载测试工具
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_requests 3;
keepalive_timeout 65 60;
#第二个数字是显示等待多少秒,第一个数字是实际等待时间
#使用命令测试:
[root@nginx ~]# telnet lee.timinglee.org 80
作为下载服务器进行配置
[root@nginx ~]# mkdir /data/web/download
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
location /download {
root /data/web;
autoindex on;
}
[root@nginx ~]# mkdir /data/web/download
[root@nginx ~]# 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.0396294 s, 2.6 GB/s
#测试
[root@nginx ~]# curl nginx.example.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> 18-Aug-2024 13:10 104857600
</pre><hr></body>
</html>
调整时区
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
location /download {
root /data/web;
autoindex on;
#真实时间
autoindex_localtime on;
#粗略大小
autoindex_exact_size off;
#限速 1M/s
limit_rate 1024k;
}
nginx状态页面
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
[root@nginx ~]# cd /usr/local/nginx/conf.d/
[root@nginx conf.d]# vim vhost.conf
[root@nginx conf.d]# vim status.conf
[root@nginx conf.d]# nginx -s relod
server {
listen 80;
server_name status.example.org;
root /data/web/html;
index index.html;
location /status {
stub_status;
auth_basic "login";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
指定用户访问
[root@nginx conf.d]# vim status.conf
server {
listen 80;
server_name status.example.org;
root /data/web/html;
index index.html;
location /status {
stub_status;
allow 172.25.254.1;
deny all;
}
}
[root@nginx conf.d]# nginx -s reload
#拒绝
[root@nginx conf.d]# curl status.example.org/status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
nginx数据压缩功能
[root@nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_buffers number size;
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 conf.d]# nginx -s reload
[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# echo hello example xixi > /data/web/html/small.html
[root@nginx conf.d]# cat /var/log/example.org/access.log > /data/web/html/big.html
测试
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sun, 18 Aug 2024 14:02:58 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Sun, 18 Aug 2024 13:59:20 GMT
Connection: keep-alive
ETag: "66c1fe38-13"
Accept-Ranges: bytes
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/big.html
...
Accept-Ranges: gzip
nginx变量
内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP$args;
#变量中存放了URL中的所有参数
#例如:https://search.jd.com/Search?keyword=手机&enc=utf-8
#返回结果为: keyword=手机&enc=utf-8$is_args
#如果有参数为? 否则为空$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/webdata/nginx/timinglee.org/lee。$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令
#比如:http://lee.timinglee.org/var?\id=11111会被定义为/var
#返回结果为:/var$host;
#存放了请求的host名称
limit_rate 10240;echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口$remote_user;
#已经经过Auth Basic Module验证的用户名
curl -u lee:lee ...$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称$request_method;
#请求资源的方式,GET/PUT/DELETE等$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,
#如:webdata/nginx/timinglee.org/lee/var/index.html$request_uri;
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,
#例如:/main/index.do?id=20190221&partner=search$scheme;
#请求的协议,例如:http,https,ftp等$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等$server_addr;
#保存了服务器的IP地址$server_name;
#虚拟主机的主机名$server_port;
#虚拟主机的端口号$http_user_agent;
#客户端浏览器的详细信息$http_cookie;
#客户端的所有cookie信息$cookie_<name>
#name为任意请求报文首部字部cookie的key名$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有横线需要替换为下划线
#示例:
echo $http_user_agent;
echo $http_host;
$sent_http_<name>#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有问题
echo $sent_http_server;
$arg_<name>#此变量存放了URL中的指定参数,name为请求url中指定的参数
echo $arg_id;
[root@nginx2 ~]# cd /usr/local/nginx/conf.d
[root@nginx2 conf.d]# vim var.conf
server {
listen 80;
server_name var.example.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@nginx2 conf.d]# vim /etc/hosts
172.25.254.200 nginx2.example.org var.example.org
[root@nginx2 conf.d]# nginx -s reload
[root@nginx2 conf.d]# curl -b "key1=lee,key2=lee1" -u lee:lee var.example.org/var?name=lee&&id=6666
172.25.254.200
name=lee
?
/data/web/html
/var
var.example.org
34732
lee
GET
/data/web/html/var
/var?name=lee
http
HTTP/1.1
172.25.254.200
var.example.org
80
curl/7.76.1
key1=lee,key2=lee1
lee1
自定义变量
set $example lisa #做个声明在location里即可
[root@nginx2 conf.d]# vim var.conf
server {
listen 80;
server_name var.example.org;
root /data/web/html;
index index.html;
location /var {
default_type text/html;
#设定内置变量
set $timinglee lee;
echo $timinglee;
}
}
[root@nginx2 conf.d]# nginx -s reload
[root@nginx2 conf.d]# curl -b "key1=lee,key2=lee1" -u lee:lee var.example.org/var?name=lee&&id=6666
lee
Rewrite相关功能
if判断
符号
= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)#注意:
#如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。
#nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false
实验
[root@nginx2 conf.d]# vim var.conf
location /test2 {
#最终访问文件
if ( !-e $request_filename ){
echo "$request_filename is not exist";
}
}
[root@nginx2 conf.d]# nginx -s reload
[root@nginx2 conf.d]# curl var.example.org/test2/index.html
/data/web/html/test2/index.html is not exist
#如果文件存在就显示内容
set设置
已经在自定义变量中设置过了
break中断
location里面遇到break就不再执行它下面的动作了
[root@nginx2 conf.d]# vim var.conf
#break
location /break {
default_type text/html;
set $name lee;
echo $name;
if ( $http_user_agent = "curl/7.76.1" ){
break;
}
set $id 666;
echo $id;
}
[root@nginx2 conf.d]# nginx -s reload
[root@nginx2 conf.d]# curl var.example.org/break
lee
[root@nginx2 conf.d]# curl -A "firefox" var.example.org/break
lee
666
return返回
[root@nginx2 conf.d]# vim var.conf
#return
location /return {
default_type text/html;
#如果文件不存在,就重定向到百度
if ( !-e $request_filename){
return 301 http://www.baidu.com;
}
echo "$request_filename is exist";
}
#不存在
[root@nginx2 conf.d]# curl -I var.example.org/return
HTTP/1.1 301 Moved Permanently
Server: example/1.26.1
Date: Sun, 18 Aug 2024 06:04:49 GMT
Content-Type: text/html
Content-Length: 171
Connection: keep-alive
Location: http://www.baidu.com
#存在
[root@nginx2 conf.d]# mkdir -p /data/web/html/return
[root@nginx2 conf.d]# curl -I var.example.org/return
HTTP/1.1 200 OK
Server: example/1.26.1
Date: Sun, 18 Aug 2024 06:08:39 GMT
Content-Type: text/html
Connection: keep-alive
Rewrite指令
临时域名和永久重定向
[root@nginx conf.d]# vim vhost.conf
server {
listen 80;
server_name var.example.org;
root /data/web/html;
index index.html;
location / {
root /data/web/var;
index index.html;
rewrite / http://www.example.com permanent;
#rewrite / http://www.example.com redirect;
}
}
[root@nginx conf.d]# mkdir /data/web/var -p
[root@nginx nginx]# curl var.example.org
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
last和break
break;
#重写完成后,停止对当前URL在当前location中后续的其它重写操作
#而后直接跳转至重写规则配置块之后的其它配置,结束循环,建议在location中使用
#适用于一个URL一次重写last;
#重写完成后,停止对当前URI在当前location中后续的其它重写操作,
#而后对新的URL启动新一轮重写检查,不建议在location中使用
#适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户
[root@nginx-node1 conf.d]# mkdir /data/web/html/{test1,test2,break,last} -p
[root@nginx-node1 conf.d]# echo test1 > /data/web/html/test1/index.html
[root@nginx-node1 conf.d]# echo test2 > /data/web/html/test2/index.html
[root@nginx-node1 conf.d]# echo last > /data/web/html/last/index.html
[root@nginx-node1 conf.d]# echo break > /data/web/html/break/index.html
要点:
break访问得到的是test1,他不会继续读location之外的内容
last会读location之外的内容,它会读到test1然后重定向返回值
全站加密
#创建加密目录
[root@nginx ~]# cd /usr/local/nginx/
[root@nginx nginx]# mkdir certs
#加密
[root@nginx ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/example.org.key -x509 -days 365 -out /usr/local/nginx/certs/example.org.crt
[root@nginx2 certs]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
listen 443 ssl;
server_name www.example.org;
root /data/web/html;
index index.html;
#证书目录
ssl_certificate /usr/local/nginx/certs/example.org.crt;
#钥匙证书
ssl_certificate_key /usr/local/nginx/certs/example.org.key;
#session会话
ssl_session_cache shared:SSL:1m;
#连接时间
ssl_session_timeout 5m;
location / {
#如果协议不是http,就重定向https
if ( $scheme = http ){
rewrite / https://$host redirect;
}
#如果访问的页面不存在,转到主页面
if ( !-e $request_filename ){
rewrite /(.*) https://$host/index.html redirect;
}
}
}
防盗链
原本网页
[root@nginx ~]# cd /data/web/html/images
[root@nginx images]# ls
xixi.jpg
盗链网页172.25.254.10
[root@webserver1 ~]# cd /var/www/html/
[root@webserver1 html]# vim index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://www.example.org/images/xixi.jpg" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://www.example.org>狂点老李</a>出门见喜</p>
</body>
</html>
[root@webserver1 html]# systemctl restart httpd
访问盗链网页

防盗链
server {
listen 80;
listen 443 ssl;
server_name www.example.org;
root /data/web/html;
index index.html;
ssl_certificate /usr/local/nginx/certs/example.org.crt;
ssl_certificate_key /usr/local/nginx/certs/example.org.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#允许访问的
location /images {
valid_referers none blocked server_names *.example.org ~/.baidu/.;
#如果是非法的,就重定向
if ( $invalid_referer ){
rewrite ^/ http://www.example.org/haha.jpg;
}
}
}
nginx方向代理功能
RS10
[root@webserver1 ~]# echo 172.25.254.10 > /var/www/html/index.html [root@webserver1 ~]# systemctl restart httpd
RS20
[root@webserver2 ~]# mkdir -p /var/www/html/static [root@webserver2 ~]# echo 172.25.254.20 > /var/www/html/static/index.html [root@webserver2 ~]# vim /etc/httpd/conf/httpd.conf listen 8080;
开始构建代理
[root@nginx2 conf.d]# vim vhost.conf
server {
listen 80;
server_name www.example.org;
location / {
proxy_pass http://172.25.254.10:80;
}
location /static {
proxy_pass http://172.25.254.20:8080;
}
}
[root@nginx2 conf.d]# nginx -s reload
测试结果

动静分离
10部署php
[root@webserver1 ~]# dnf install php -y [root@webserver1 ~]# vim /var/www/html/index.php <?php phpinfo(); ?> [root@webserver1 ~]# systemctl restart httpd
nginx主机
server {
listen 80;
server_name www.example.org;
location ~ \.php$ {
proxy_pass http://172.25.254.10:80;
}
location /static {
proxy_pass http://172.25.254.20:8080;
}
}
测试

缓存功能
缓存功能默认关闭状态,需要先动配置才能启用
proxy_cache zone_name | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
#zone_name 表示缓存的名称.需要由proxy_cache_path事先定义proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中示例:
#缓存200 302 缓存10分钟
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;#示例:在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建
levels=1:2:2 #定义缓存目录结构层次
#1:2:2可以生成
2^4x2^8x2^8=2^20=1048576个目录
keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata
inactive=120s #缓存有效时间
max_size=10g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
非缓存场景
[root@webserver1 ~]# ab -n1000 -c100 http://www.example.org/static/index.html
Requests per second: 400.02 [#/sec] (mean)
Requests per second: 709.67 [#/sec] (mean)
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf #gzip on; include "/usr/local/nginx/conf.d/*.conf"; proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g; [root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf server { listen 80; server_name www.example.org; location ~ \.php$ { proxy_pass http://172.25.254.10:80; } location /static { proxy_pass http://172.25.254.20:8080; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 10m; proxy_cache_valid any 1m; } } [root@nginx ~]# nginx -s reload
[root@webserver1 ~]# ab -n1000 -c100 http://www.example.org/static/index.html
Requests per second: 400.02 [#/sec] (mean)
Requests per second: 709.67 [#/sec] (mean)
反向代理负载均衡
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检
测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性
检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再
次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器
down #标记为down状态,可以平滑下线后端服务器
6.1.2.2 反向代理示例: 后端多台 web服务器
环境说明:
部署后端 Apache服务器
配置 nginx 反向代理
注意: 本节实验过程中先关闭缓存
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
hash KEY [consistent];
#基于指定请求报文中首部字段或者URI等key做hash计算,使用consistent参数,将使用ketama一致性
hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算hash $request_uri consistent; #基于用户请求的uri做hash
hash $cookie_sessionid #基于cookie中的sessionid这个key进行hash调度,实现会话绑定ip_hash;
#源地址hash调度方法,基于的客户端的remote_addr(源地址IPv4的前24位或整个IPv6地址)做hash计算,以实现会话保持least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器,相当于LVS中的WLC
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
ip_hash;
#hash $request_uri consistent;
#hash $cookie_lee;
server 172.25.254.20:8080 weight=1 fail_timeout=15s max_fails=3;
server 172.25.254.10:80 weight=1 fail_timeout=15s max_fails=3;
#指定算法之后不能加backup
}
server {
listen 80;
server_name www.example.org;
location / {
proxy_pass http://webcluster;
}
}
[root@nginx ~]# nginx -s reload
方向代理示例
使用的NAT网络,都使用的172.25.254.2去访问
hash $request_uri consistent测试结果

实现nginx四层负载
域名解析
[root@webserver1 ~]# dnf install bind -y
[root@webserver1 ~]# vim /etc/named.conf
#注释
options {
// listen-on port 53 { 127.0.0.1; };
// listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
secroots-file "/var/named/data/named.secroots";
recursing-file "/var/named/data/named.recursing";
// allow-query { localhost; };
dnssec-validation no;
#增加example的域名解析
[root@webserver1 ~]# vim /etc/named.rfc1912.zones
zone "example.org" IN {
type master;
file "example.org.zone";
allow-update { none; };
};
#编译配置文件
[root@webserver1 ~]# cd /var/named/
[root@webserver1 named]# cp named.localhost example.org.zone -p
[root@webserver1 named]# vim example.org.zone
$TTL 1D
@ IN SOA ns.example.org. root.example.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.example.org.
ns A 172.25.254.10
www A 172.25.254.10
[root@webserver1 named]# systemctl restart named
[root@webserver1 named]# dig www.example.org @172.25.254.10
;; QUESTION SECTION:
;www.example.org. IN A
;; ANSWER SECTION:
www.example.org. 86400 IN A 172.25.254.10
#复制到webserver2
[root@webserver1 named]# scp -p /etc/named.{conf,rfc1912.zones} root@172.25.254.20:/etc/
[root@webserver1 named]# scp -p /var/named/example.org.zone root@172.25.254.20://var/named/example.org.zone
FastCGI
源码安装php
#解压
[root@nginx ~]# tar zxf php-8.3.9.tar.bz2
#编译目录
[root@nginx ~]# ./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
#下载依赖
[root@Nginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel
libpng-devel libcurl-devel oniguruma-devel
[root@nginx php-8.3.9]# dnf list oniguruma
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
Last metadata expiration check: 13:50:15 ago on Sun 18 Aug 2024 09:03:16 PM CST.
Installed Packages
oniguruma.x86_64 6.9.6-1.el9.5 @AppStream
Available Packages
oniguruma.i686 6.9.6-1.el9.5 rhel9A
#oniguruma-devel软件包安装失败,需要使用阿里云上的
[root@nginx-node1 php-8.3.9]# cd /usr/local/php/etc/
[root@nginx-node1 etc]# ls
php-fpm.conf.default php-fpm.d
[root@nginx-node1 etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@nginx-node1 etc]# ls
php-fpm.conf php-fpm.conf.default php-fpm.d
[root@nginx-node1 etc]# vim php-fpm.conf
#去掉注释
pid = run/php-fpm.pid #指定pid文件存放位置
[root@nginx-node1 etc]# cd php-fpm.d/
[root@nginx-node1 php-fpm.d]# ls
www.conf.default
[root@nginx-node1 php-fpm.d]# cp -p www.conf.default www.conf
[root@nginx-node1 etc]# cd /root/php-8.3.9/
[root@nginx-node1 php-8.3.9]# ls
[root@nginx-node1 php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@nginx-node1 php-8.3.9]# vim /usr/local/php/etc/php.ini
date.timezone = Asia/Shanghai #修改时区
[root@nginx-node1 php-8.3.9]# cd sapi/
[root@nginx-node1 sapi]# cd fpm/
[root@nginx-node1 fpm]# cp php-fpm.service /lib/systemd/system/
[root@nginx-node1 fpm]# vim /lib/systemd/system/php-fpm.service
#ProtectSystem=full #注释该内容
[root@nginx-node1 fpm]# systemctl start php-fpm.service
[root@nginx-node1 fpm]# systemctl daemon-reload
[root@nginx-node1 fpm]# systemctl start php-fpm
[root@nginx-node1 fpm]# netstat -antlupe | grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0 126634 156497/php-fpm: mas
[root@nginx-node1 fpm]# mkdir -p /data/web/php
[root@nginx-node1 fpm]# cd /usr/local/php/bin/
[root@nginx-node1 bin]# ls
phar phar.phar php php-cgi php-config phpdbg phpize
[root@nginx-node1 bin]# vim ~/.bash_profile
[root@nginx-node1 bin]# cat ~/.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
[root@nginx-node1 bin]# source ~/.bash_profile
[root@nginx-node1 bin]# php
php php-cgi php-config phpdbg php-fpm phpize
[root@nginx-node1 bin]# vim /data/web/php/index.php
<?php
phpinfo();
?>
[root@nginx-node1 bin]# cd /usr/local/
[root@nginx-node1 local]# cd nginx/
[root@nginx-node1 nginx]# cd conf/
[root@nginx-node1 conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@nginx-node1 conf]# vim fastcgi.conf
[root@nginx-node1 conf]# cd ..
[root@nginx-node1 nginx]# mkdir conf.d
[root@nginx-node1 nginx]# vim conf/nginx.conf
include "/usr/local/nginx/conf.d/*.conf";
[root@nginx-node1 conf.d]# nginx -s reload
[root@nginx-node1 conf.d]#
[root@nginx-node1 conf.d]# vim /usr/local/php/etc/php-fpm.d/www.conf
[root@nginx php-fpm.d]# vim www.conf
; Note: This value is mandatory.
listen = 0.0.0.0:9000
[root@nginx-node1 conf.d]# systemctl restart php-fpm.service
[root@nginx-node1 conf.d]# netstat -antlupe | grep php
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 0 128283 156715/php-fpm: mas
[root@nginx-node1 conf.d]# cd /usr/local/nginx/conf.d/
[root@nginx-node1 conf.d]# vim vhosts.conf
[root@nginx-node1 conf.d]# cat vhosts.conf
server {
listen 80;
server_name www.test.org;
root /data/web/html;
index index.html;
location ~ \.php$ {
root /data/web/php;
fastcgi_pass 172.25.254.100:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@nginx-node1 conf.d]# nginx -s reload
安装memcache
<---
[root@nginx-node1 ~]# cd /usr/local/php/
[root@nginx-node1 php]# ls
bin etc include lib php sbin var
[root@nginx-node1 php]# cd etc/
[root@nginx-node1 etc]# ls
php-fpm.conf php-fpm.conf.default php-fpm.d php.ini
[root@nginx-node1 etc]# cp php.ini ../lib/
[root@nginx-node1 etc]# cd ../lib/
[root@nginx-node1 lib]# ls
php php.ini
--->
#以上操作是因为以下没有指定配置路径,所以需要移动位置:
#解压源码并安装
[root@Nginx ~]# ./configure \
...
--with-config-file-path=/usr/local/php/etc \ #指定配置路径
...
[root@nginx-node1 ~]# ls
anaconda-ks.cfg nginx-1.26.1
echo-nginx-module-0.63 nginx-1.26.1.tar.gz
echo-nginx-module-0.63.tar.gz php-8.3.9
memcache-8.2.tgz php-8.3.9.tar.gz
memc-nginx-module-0.20 srcache-nginx-module-0.33
memc-nginx-module-0.20.tar.gz srcache-nginx-module-0.33.tar.gz
nginx-1.24.0 testfile
nginx-1.24.0.tar.gz testfile.1
[root@nginx-node1 ~]# tar zxf memcache-8.2.tgz
[root@nginx-node1 ~]# ls
anaconda-ks.cfg nginx-1.26.1
echo-nginx-module-0.63 nginx-1.26.1.tar.gz
echo-nginx-module-0.63.tar.gz package.xml
memcache-8.2 php-8.3.9
memcache-8.2.tgz php-8.3.9.tar.gz
memc-nginx-module-0.20 srcache-nginx-module-0.33
memc-nginx-module-0.20.tar.gz srcache-nginx-module-0.33.tar.gz
nginx-1.24.0 testfile
nginx-1.24.0.tar.gz testfile.1
[root@nginx-node1 ~]# cd memcache-8.2/
[root@nginx-node1 memcache-8.2]# ls
config9.m4 config.w32 docker example.php memcache.php src
config.m4 CREDITS Dockerfile LICENSE README tests
[root@nginx-node1 memcache-8.2]# phpize
Configuring for:
PHP Api Version: 20230831
Zend Module Api No: 20230831
Zend Extension Api No: 420230831
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
[root@nginx-node1 memcache-8.2]# dnf install autoconf -y
[root@nginx-node1 memcache-8.2]# phpize
Configuring for:
PHP Api Version: 20230831
Zend Module Api No: 20230831
Zend Extension Api No: 420230831
[root@nginx-node1 memcache-8.2]# ./configure && make && make install
[root@nginx-node1 memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
memcache.so opcache.so
[root@nginx-node1 memcache-8.2]#
[root@nginx-node1 memcache-8.2]# vim /usr/local/php/etc/php.ini
[root@nginx-node1 memcache-8.2]# systemctl restart php-fpm.service
[root@nginx-node1 memcache-8.2]# dnf install memcached -y
[root@nginx-node1 memcache-8.2]# vim /etc/sysconfig/memcached
[root@nginx-node1 memcache-8.2]# systemctl start memcached.service
[root@nginx-node1 memcache-8.2]# netstat -antlupe | grep memcache
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 991 140132 163074/memcached
tcp6 0 0 ::1:11211 :::* LISTEN 991 140133 163074/memcached
[root@nginx-node1 memcache-8.2]# php -m
[root@nginx-node1 memcache-8.2]# systemctl restart php-fpm.service
[root@nginx-node1 memcache-8.2]# systemctl restart memcached.service
[root@nginx-node1 memcache-8.2]# php -m
[root@nginx-node1 memcache-8.2]# cp example.php memcache.php /data/web/php/
[root@nginx-node1 memcache-8.2]# cd /data/web/php/
[root@nginx-node1 php]# ls
example.php index.php memcache.php
[root@nginx-node1 php]# vim memcache.php
[root@nginx-node1 php]# systemctl restart php-fpm.service
[root@nginx-node1 php]# systemctl restart memcached.service
#测试对比:
[root@nginx-node1 php]# ab -n1000 -c10 http://www.test.org/index.php
[root@nginx-node1 php]# ab -n1000 -c10 http://www.test.org/index.php
















被折叠的 条评论
为什么被折叠?



