企业高性能web服务器——Nginx
+# 企业高性能web服务器
1、Nginx 编译安装
1.1 编译安装 Nginx
- 这里下载nginx-1.24.0.tar.gz和nginx-1.26.1.tar.gz
- 可以在官方网站上下载:https://nginx.org/en/download.html
示例:nginx-1.24.0.tar.gz
#提前将编译安装出现问题的安装包下载好,如下图所示
[root@nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@nginx ~]# tar zxf nginx-1.24.0.tar.gz
[root@nginx ~]# cd nginx-1.24.0/
[root@nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
#关闭debug功能
[root@nginx nginx-1.24.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g"
[root@nginx nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
#编译安装,这里可以使用./configure --help查看
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--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
#make完成后再看objs目录,又有新文件
[root@nginx nginx-1.24.0]# make && make install
1.2验证版本及编译参数
#将nginx软件的命令执行路陷阱添加到环境变量中
[root@nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@nginx ~]# source ~/.bash_profile
#验证版本
[root@nginx ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --group=nginx --with-http_ssl_module --with-http_v2_module -
-with-http_realip_module --with-http_stub_status_module --withhttp_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --
with-stream_realip_module
#当前nginx进程
[root@nginx nginx-1.24.0]# ps aux | grep nginx
root 47007 0.0 0.0 9864 2560 ? Ss 19:35 0:00 nginx: master process nginx
nginx 47008 0.0 0.1 13760 5236 ? S 19:35 0:00 nginx: worker process
root 48897 0.0 0.0 221664 2292 pts/2 S+ 22:41 0:00 grep --color=auto nginx
#脚本启动nginx
[root@nginx ~]# cd /usr/local/nginx/sbin/
#nginx的配置文件
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf[root@nginx sbin]# nginx -g "worker_processes 6"
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
[root@nginx sbin]# nginx -g "worker_processes 6;"
[root@nginx sbin]# ps aux | grep nginx
root 49104 0.0 0.0 9864 932 ? Ss 23:24 0:00 nginx: master process nginx -g worker_processes 6;
nginx 49105 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49106 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49107 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49108 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49109 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
nginx 49110 0.0 0.1 13760 4756 ? S 23:24 0:00 nginx: worker process
root 49112 0.0 0.0 221664 2292 pts/2 S+ 23:24 0:00 grep --color=auto nginx
```bash
#nginx 启动文件
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
[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 49193 0.0 0.0 221664 2296 pts/2 S+ 23:28 0:00 grep --color=auto nginx
[root@nginx sbin]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx sbin]# ps aux | grep nginx
root 49227 0.0 0.0 9864 928 ? Ss 23:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 49228 0.0 0.1 13760 4748 ? S 23:28 0:00 nginx: worker process
root 49230 0.0 0.0 221664 2292 pts/2 S+ 23:28 0:00 grep --color=auto nginx
[root@nginx ~]# nginx -s quit
[root@nginx ~]# ps aux | grep nginx
root 48970 0.0 0.0 221664 2292 pts/2 S+ 22:48 0:00 grep --color=auto nginx
#前台运行
[root@nginx ~]# nginx -g "daemon off"
nginx: [emerg] unexpected end of parameter, expecting ";" in command line
示例:nginx-1.26.1.tar.gz
[root@nginx ~]# tar zxf nginx-1.26.1.tar.gz
[root@nginx ~]# cd nginx-1.26.1/
#开始编译新版本
[root@nginx 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_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module
#只要make无需要make install,如果make install就会将原来的配置文件覆盖掉
[root@nginx nginx-1.26.1]# make#二进制文件复制
[root@nginx nginx-1.26.1]# cd objs/
[root@nginx objs]# cp -f nginx /usr/local/nginx/sbin/nginx#把之前的旧版的nginx命令备份
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# cp nginx nginx.24
[root@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 sbin]# ps aux | grep nginx
root 49020 0.0 0.0 221664 2268 pts/2 S+ 23:07 0:00 grep --color=auto nginx
#nginx worker ID
[root@nginx sbin]# kill -USR2 49020
2、平滑升级和回滚
有时候我们需要对Nginx版本进行升级以满足对其功能的需求,例如添加新模块,需要新功能,而此时Nginx又在跑着业务无法停掉,这时我们就可能选择平滑升级
[root@Nginx sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 ##依旧是旧版本生生效
Date: Thu, 18 Jul 2024 07:45:58 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 18 Jul 2024 03:41:13 GMT
Connection: keep-alive
ETag: "66988ed9-267"
Accept-Ranges: bytes#回收旧版本
[root@nginx sbin]# kill -WINCH 49227
[root@nginx sbin]# ps aux | grep nginx
root 49227 0.0 0.0 9864 928 ? Ss 23:28 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 49235 0.0 0.0 221664 2280 pts/2 S+ 23:34 0:00 grep --color=auto nginx
3、 Nginx 核心配置详解
3.1 配置文件说明
- 主配置文件:[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#主配置文件结构:四部分
main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
...
}
#http/https 协议相关配置段
http {
默认的nginx.conf 配置文件格式说明
...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}
#stream 服务器相关配置段
stream {
...
}
#默认的nginx.conf 配置文件格式说明
#全局配置端,对全局生效,主要设置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@nginx ~]# lscpu
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# ps axo pid,cmd,psr | grep ngiinx49296 grep --color=auto ngiinx 0
#修改pam限制
[root@nginx ~]# sudo -u nginx ulimit -n
1024
[root@nginx ~]# vim /etc/security/limits.conf
nginx - nofile 100000
[root@nginx ~]# sudo -u nginx ulimit -n
100000
[root@nginx ~]# sudo -u nginx ulimit -a
real-time non-blocking time (microseconds, -R) unlimited
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 14215
max locked memory (kbytes, -l) 8192
max memory size (kbytes, -m) unlimited
open files (-n) 100000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 14215
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
3.2 实现 nginx 的高并发配置
[root@Nginx ~]# ulimit -n 100000
[root@nginx ~]# dnf install httpd-tools -y
[root@nginx ~]# ab -c 5000 -n 10000 http://172.25.254.100/index.html#默认配置不支持高并发,会出现以下错误日志
[root@Nginx ~]# tail /apps/nginx/logs/error.log
2020/09/24 21:19:33 [crit] 41006#0: *1105860 open() "/apps/nginx/html/50x.html"
failed (24: Too many open files), client: 10.0.0.7, server: localhost, request:
"GET / HTTP/1.0", host: "10.0.0.8"
2020/09/24 21:19:33 [crit] 41006#0: accept4() failed (24: Too many open files)
2020/09/24 21:19:33 [crit] 41006#0: *1114177 open()
"/apps/nginx/html/index.html" failed (24: Too many open files), client: 10.0.0.7,
server: localhost, request: "GET / HTTP/1.0", host: "10.0.0.8"
#修改配置
[root@nginx ~]# vim /etc/security/limits.conf* - nproc 100000
[root@Nginx ~]# vim /apps/nginx/conf/nginx.conf
worker_rlimit_nofile 100000;
[root@Nginx ~]# systemctl restart nginx
3.3核心配置示例
3.3.1 新建一个 PC web 站点
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
include "/usr/local/nginx/conf.d/*.conf";
[root@nginx ~]# mkdir -p /usr/local/nginx/conf.d/
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;
}
[root@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 -s reload
[root@nginx ~]# mkdir -p /data/web/html
[root@nginx ~]# echo www.timinglee.org > /data/web/html/index.html
一定要做本地解析,不然访问不到,要以管理员身份打开hosts文件,如果不是可以在属性里修改权限,之后就可以在浏览器中访问解析后的内容
3.3.2root 与 alias
root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location
- root示例:
[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 ~]# mkdir /data/web/test1 -p
[root@nginx ~]# echo /data/web/test1 > /data/web/test1/index.html
这里会出现一个错误然后访问的时候访问不到,此时可以用查看日志的方法寻找错误
[root@nginx ~]# tail /usr/local/nginx/logs/error.log
错误原因:
修改:
再次访问就可以访问的到
- alias示例:
alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于
location上下文,此指令使用较少
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
再次访问,依旧可以访问的到
3.3.3location
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { … }
= #只能精确指定文件,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
#对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
不带等号:
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location /test {root /data/web;}
}[root@nginx ~]# mkdir /data/web/test -p
[root@nginx ~]# echo test page > /data/web/test/index.html
[root@nginx ~]# nginx -s reload
访问:
带等号:
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location /test {root /data/web;}location = /test {root /data/web;}
}
[root@nginx ~]# nginx -s reload
~~标准的uri前,必须要有/ 以/t开头识别,lee不识别
[root@nginx ~]# mkdir -p /data/web1/{test1,tee}
[root@nginx ~]# echo test1 > /data/web1/test1/index.html
[root@nginx ~]# echo tee > /data/web1/tee/index.html
[root@nginx ~]# mkdir -p /data/web1/lee
[root@nginx ~]# echo lee > /data/web1/lee/index.html[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location = /test {root /data/web2;}location /test {root /data/web1;}location ^~ /t {root /data/web1;}
}
[root@nginx ~]# nginx -s reload
~区分大小写
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location = /test {root /data/web2;}location /test {root /data/web1;}location ^~ /t {root /data/web1;}location ~\.html$ {root /data/web1/;}
}
大写:默认访问不到大写
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# echo index.lee > /data/web1/lee/index.lee
server{listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;location = /test {root /data/web2;}location /test {root /data/web1;}location ^~ /t {root /data/web1;}location ~\.html$ {root /data/web1/;}location ~* .HTML$ {root /data/web1;}
}
优先级:~ 等于 ~* 大于 不带= 大于 ^~ 大于 =
3.3.4 Nginx 账户认证功能
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@nginx ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$MuzW5D.k$1xJ1x962htx0E0ECaHMTG0
[root@nginx ~]#
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd lee
New password:
Re-type new password:
Adding password for user lee
[root@nginx ~]#
[root@nginx ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$MuzW5D.k$1xJ1x962htx0E0ECaHMTG0
lee:$apr1$gjJ8UFja$hfJLqGRY/J.q9pDaEiLlQ.
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
server {listen 80;server_name www.timinglee.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";}
}
访问:
3.3.5自定义错误页面
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;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 ~]# nginx -s reload
[root@nginx ~]# mkdir -p /data/web/errorpage
[root@nginx ~]# echo error page > /data/web/errorpage/40x.html
访问:
3.3.5自定义错误日志
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/timinglee.org/error.log;access_log /var/log/timinglee.org/access.log;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 ~]# nginx -s reload
[root@nginx ~]# mkdir /var/log/timinglee.org
[root@nginx ~]# curl www.timinglee.org
www.timinglee.org
[root@nginx ~]# cat /var/log/timinglee.org/access.log
172.25.254.100 - - [16/Aug/2024:14:40:29 +0800] "GET / HTTP/1.1" 200 18 "-" "curl/7.76.1"
[root@nginx ~]# curl www.timinglee.org/aaa
error page
[root@nginx ~]# cat /var/log/timinglee.org/error.log
2024/08/16 14:42:16 [error] 52605#0: *73 open() "/data/web/html/aaa" failed (2: No such file or directory), client: 172.25.254.100, server: www.timinglee.org, request: "GET /aaa HTTP/1.1", host: "www.timinglee.org"
3.3.6检测文件是否存在
#文件存在:
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]#
[root@nginx ~]# nginx -s reload
[root@nginx ~]#
[root@nginx ~]#
[root@nginx ~]# curl www.timinglee.org
www.timinglee.org
#文件不存在:
[root@nginx ~]# rm -rf /data/web/html/index.html
[root@nginx ~]# rm -rf /data/web/html/error/
[root@nginx ~]# curl www.timinglee.org
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>
[root@nginx ~]# mkdir /data/web/html/error
[root@nginx ~]# echo error default > /data/web/html/error/default.html
[root@nginx ~]# curl www.timinglee.org
error default
3.3.7长连接配置——主配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf#keepalive_timeout 0;keepalive_timeout 65;keepalive_requests 2;
[root@nginx ~]# nginx -s reload
#下载测试工具
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# curl -v www.timinglee.org
* Trying 172.25.254.100:80...
* Connected to www.timinglee.org (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: www.timinglee.org
> User-Agent: curl/7.76.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.24.0
< Date: Fri, 16 Aug 2024 06:57:31 GMT
< Content-Type: text/html
< Content-Length: 14
< Last-Modified: Fri, 16 Aug 2024 06:50:38 GMT
< Connection: keep-alive
< ETag: "66bef6be-e"
< Accept-Ranges: bytes
<
error default
* Connection #0 to host www.timinglee.org left intact
#测试两次:
[root@nginx ~]# telnet www.timinglee.org 80
Trying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.orgHTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 07:02:50 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Fri, 16 Aug 2024 06:50:38 GMT
Connection: keep-alive
ETag: "66bef6be-e"
Accept-Ranges: bytesTrying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.orgHTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 07:02:50 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Fri, 16 Aug 2024 06:50:38 GMT
Connection: keep-alive
ETag: "66bef6be-e"
Accept-Ranges: byteswww.timinglee.org
3.3.8 作为下载服务器配置
[root@nginx ~]# mkdir /data/web/download
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.timinglee.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/timinglee.org/error.log;access_log /var/log/timinglee.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.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;}location /dawnload {root /data/web;}
}
[root@nginx ~]# nginx -s reload
[root@nginx ~]# dd if=/dev/zero of=/data/web/download/leeefile bs=1M count=100100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0783402 s, 1.3 GB/s
默认找不到:
[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
autoindex_localtime on;
[root@nginx ~]# nginx -s reload
限速:
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
limit_rate 1024k;
[root@nginx ~]# nginx -s reload
3.3.9状态页
需要做本地解析:
[root@nginx ~]# cd /usr/local/nginx/conf.d/[root@nginx conf.d]# vim status.conf
server {listen 80;server_name status.timinglee.org;root /data/web/html;index index.html;location /status {stub_status;}
}
状态页用于输出nginx的基本状态信息
#输出信息示例:
Active connections: #当前处于活动状态的客户端连接数
#包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的
连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明
访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active –
(reading+writing)
[root@nginx conf.d]# vim /etc/hosts
#查看自己windows下的ip,允许指定ip可以访问
[root@nginx conf.d]# w -i16:40:47 up 17:51, 3 users, load average: 0.00, 0.11, 0.13
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root seat0 login- Thu18 0.00s 0.00s 0.00s /usr/libexec/
root tty2 tty2 Thu18 22:20m 0.03s 0.03s /usr/libexec/
root pts/1 172.25.254.2 14:56 7.00s 0.24s 0.00s w -i
[root@nginx conf.d]# vim status.conf
server {listen 80;server_name status.timinglee.org;root /data/web/html;index index.html;location /status {stub_status;allow 172.25.254.2;deny all;}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl status.timinglee.org
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>
3.3.10 Nginx 压缩功能
Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。
Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块
[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_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 -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 conf.d]# nginx -s reload\
[root@nginx conf.d]# echo hello timinglee > /data/web/html/small.html
[root@nginx conf.d]# echo hello timinglee > /data/web/html/big.html
[root@nginx conf.d]# du -sh /usr/local/nginx/logs/access.log
32K /usr/local/nginx/logs/access.log
#--head 显示请求报文头部,方便查看
#小文件不压缩,大文件压缩
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 08:19:46 GMT
Content-Type: text/html
Content-Length: 16
Last-Modified: Fri, 16 Aug 2024 08:17:19 GMT
Connection: keep-alive
ETag: "66bf0b0f-10"
Accept-Ranges: bytes
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/big.html
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 08:25:20 GMT
Content-Type: text/html
Last-Modified: Fri, 16 Aug 2024 08:24:47 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"66bf0ccf-7700"
Content-Encoding: gzip
3.4内置变量及自定义变量
#nginx系统内置参数
server {listen 80;server_name var.timinglee.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;}
}
#测试curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
#nginx自定义变量:
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;}
}
#测试:curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
3.5ngx_http_rewrite_module 模块指令
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,变量与表达式之间
使用以下符号链接:
= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)
#注意:
#如果$变量的值为空字符串或0,则if指令认为该条件为false,其他条件为true。
#nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false
3.5.1if 判定指令
测试文件是否存在
示例:
[root@nginx test2]# cat /usr/local/nginx/conf.d/yu.conf
server {listen 80;server_name var.timinglee.org;root /data/web/html;index index.html;location /test2 {if ( !-e $request_filename ) {echo "$request_filename is not exist";}}
}
测试:
#没文件
[root@nginx test2]# nginx -s reload
[root@nginx test2]# curl var.timinglee.org/test2/index.html
/data/web/html/test2/index.html is not exist#有文件
[root@nginx test2]# echo test2 > /data/web/html/test2/index.html
[root@nginx test2]# curl var.timinglee.org/test2/index.html
test2
3.5.2break指令
示例:
location /break {default_type text/html;set $name timinglee;echo $name;if ( $http_user_agent = "curl/7.76.1" ){break;}set $id 666;echo $id;}
测试:
[root@nginx test2]# nginx -s reload
[root@nginx test2]# curl var.timinglee.org/break
timinglee[root@nginx test2]# curl -A "firefox" var.timinglee.org/break
timinglee
666
3.5.3return指令
示例:
location /return {default_type text/html;if ( !-e $request_filename){return 301 http://www.baidu.com;}echo "$request_filename is exist";}
测试:
[root@nginx test2]# nginx -s reload
[root@nginx test2]# curl -I var.timinglee.org/return
HTTP/1.1 301 Moved Permanently
Server: nginx/1.26.2
Date: Sun, 18 Aug 2024 03:55:35 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout=60
Location: http://www.baidu.com[root@nginx test2]# mkdir -p /data/web/html/return
[root@nginx test2]# curl -I var.timinglee.org/return
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sun, 18 Aug 2024 03:56:26 GMT
Content-Type: text/html
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
3.5.4rewrite: 域名永久与临时重定向
域名的临时的调整,后期可能会变,之前的域名或者URL可能还用、或者跳转的目的域名和URL还会跳转,这种情况浏览器不会缓存跳转,临时重定向不会缓存域名解析记录(A记录),但是永久重定向会缓存。
示例: 因业务需要,将访问源域名 www.timinglee.org 的请求永久重定向到 www.timinglee.com
#域名永久
location / {
root /data/nginx/html/pc;
index index.html;
rewrite / http://www.timinglee.com permanent;
#rewrite / http://www.timinglee.com redirect;
}
#重启Nginx并访问域名 http://www.timinglee.org 进行测试#临时重定向
[root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf
server {listen 80;server_name lee.timinglee.org;root /webdata/nginx/timinglee.org/lee;location / {rewrite / http://lee.timinglee.com redirect;#rewrite / http://lee.timinglee.com permanent;}
}
server {listen 80;server_name lee.timinglee.com;root /webdata/nginx/timinglee.com/lee;
}
3.5.6rewrite: 自动跳转 https
案例:基于通信安全考虑公司网站要求全站 https,因此要求将在不影响用户请求的情况下将http请求全 部自动跳转至 https,另外也可以实现部分 location 跳转
[root@nginx ~]# mkdir -p /usr/local/nginx/certs
[root@nginx ~]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/jieyu.org.key -x509 -days 365 -out /usr/local/nginx/certs/jieyu.org.crt
.+.....+....+.....+.+...+........+....+....................+.+.....+...+....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.....+.....+....+........+...+.+...+...............+...+...+..............+.+......+.........+............+..+.+......+.....+...+.........................................................+...+.+..+.......+...+.....+...+.+...+...+..+............+.+.........+........+...+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
...+........+.......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..........+.....+...+.........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*....+.........+...+...+.+...+..........................+.......+..............+....+............+............+...........+.+........+.+...........+.+..+...+...+..........+..+.........+......+....+...+........+...+.+......+.....+....+...+......+...........+.+...+......+......+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanxi
Locality Name (eg, city) [Default City]:xian
Organization Name (eg, company) [Default Company Ltd]:lee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.lee.org
Email Address []:admin@lee.com
3.6Nginx 防盗链
防盗链基于客户端携带的 referer 实现, referer 是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链。
在一个web 站点盗链另一个站点的资源信息,比如:图片、视频等
#新建一个主机172.25.254.20,盗取另一台主机lee.timinglee.org/images/logo.png的图片
[root@client ~]# yum install httpd -y
[root@client html]# vim /var/www/html/index.html
#准备盗链web页面:
<html>
5.3.2 实现防盗链
基于访问安全考虑,nginx支持通过ngx_http_referer_module模块,检查访问请求的referer信息是否有效
实现防盗链功能
官方文档:
示例: 定义防盗链:
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://lee.timinglee.org/images/logo.png" >
<h1 style="color:red">欢迎大家</h1>
<p><a href=http://lee.timinglee.org>狂点老李</a>出门见喜</p>
</body>
</html>
#重启apache并访问http://172.25.254.20 测试
#验证两个域名的日志,是否会在被盗连的web站点的日志中出现以下盗链日志信息:
[root@Nginx ~]# cat /usr/local/nginx/logs/access.log
172.25.254.1 - - [22/Jul/2024:09:50:01 +0800] "GET /images/logo.png HTTP/1.1" 304
0 "http://172.25.254.20/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Edg/126.0.0.0"
172.25.254.1 - - [22/Jul/2024:09:50:18 +0800] "GET / HTTP/1.1" 304 0
"http://172.25.254.20/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Edg/126.0.0.0"
3.7实现 http 反向代理
3.7.1 http 协议反向代理
proxy_pass; # 用来设置将客户端请求转发给的后端服务器的主机# 可以是主机名 ( 将转发至后端服务做为主机头首部 ) 、 IP 地址:端口的方式# 也可以代理到预先设置的主机群组,需要模块 ngx_http_upstream_module 支持
url 后面# 此行为类似于 root#proxy_pass 指定的 uri 不带斜线将访问的 /web# 等于访问后端服务器
uri 内容# 此行为类似于 alias#proxy_pass 指定的 uri 带斜线# 等于访问后端服务器的#http://172.25.254.40:8080/index.html# 内容返回给客户端 } # http://nginx/web/index.html ==>http://1:8080# 重启 Nginx 测试访问效果:#curl -L http://www.lee.org/web# 如果 location 定义其 uri 时使用了正则表达式模式 ( 包括 ~,~*, 但不包括 ^~) ,则 proxy_pass 之后必须不能
使用 uri# 即不能有 / , 用户请求时传递的 uri 将直接附加至后端服务器之后
server {...server_name HOSTNAME;location ~|~* /uri/ {proxy_pass http://host:port; #proxy_pass 后面的 url 不能加 /}...}
http://HOSTNAME/uri/ --> http://host/uri/
proxy_hide_header field; # 用于 nginx 作为反向代理的时候# 在返回给客户端 http 响应时# 隐藏后端服务器相应头部的信息# 可以设置在 http,server 或 location 块
proxy_pass_header field; # 透传# 默认 nginx 在响应报文中不传递后端服务器的首部字段 Date, Server, X-Pad, X-Accel 等参数# 如果要传递的话则要使用 proxy_pass_header field 声明将后端服务器返回的值传递给客户端#field 首部字段大小不敏感
proxy_pass_request_body on | off;# 是否向后端服务器发送 HTTP 实体部分 , 可以设置在 http,server 或 location 块,默认即为开启
proxy_pass_request_headers on | off;# 是否将客户端的请求头部转发给后端服务器,可以设置在 http,server 或 location 块,默认即为开启
proxy_set_header; # 可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实 IP 的时候,就要更改每一个报文的头部
proxy_connect_timeout time;# 配置 nginx 服务器与后端服务器尝试建立连接的超时时间,默认为 60 秒
proxy_read_timeout time;# 配置 nginx 服务器向后端服务器或服务器组发起 read 请求后,等待的超时时间,默认 60s
proxy_send_timeout time;# 配置 nginx 项后端服务器或服务器组发起 write 请求后,等待的超时 时间,默认 60s
proxy_http_version 1.0;# 用于设置 nginx 提供代理服务的 HTTP 协议的版本,默认 http 1.0
proxy_ignore_client_abort off;# 当客户端网络中断请求时, nginx 服务器中断其对后端服务器的请求。即如果此项设置为 on 开启,则服务器会忽略客户端中断并一直等着代理服务执行返回,如果设置为off ,则客户端中断后 Nginx 也会中断客户端请求并立即记录499 日志,默认为 off
3.7.2http 反向代理负载均衡
在上一个节中 Nginx 可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器,而且不能对后端服务器提供相应的服务器状态监测,Nginx 可于 ngx_http_upstream_module 模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能。
原文链接:https://blog.csdn.net/MIKROKSMOS_L/article/details/141247951
[root@web20 ~]# yum install httpd -y
[root@web20 ~]# echo "web2 172.25.254.20" > /var/www/html/index.html
[root@web20 ~]# systemctl enable --now httpd[root@web10 ~]# yum install httpd -y
[root@web10 ~]# echo "web1 172.25.254.10" >> /var/www/html/index.html
[root@web10 ~]# systemctl enable --now http
[root@centos8 ~]# cat /apps/nginx/conf/conf.d/pc.conf
upstream webserver {
#ip_hash;
#hash $request_uri consistent;
#hash $cookie_lee
#least_conn;
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;
server 172.25.254.100:80 backup;
}
server {
listen 80;
server_name www.lee.org;
location ~ / {
proxy_pass http://webserver;
}
}
3.8实现 Nginx 四层负载均衡
Nginx 在 1.9.0 版本开始支持 tcp 模式的负载均衡,在 1.9.13 版本开始支持 udp 协议的负载, udp 主要用于 DNS的域名解析,其配置方式和指令和 http 代理类似,其基于 ngx_stream_proxy_module 模块实现 tcp 负载,另外基于模块ngx_stream_upstream_module 实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。
#在web20中安装mysql
[root@web20 ~]# yum install mariadb-server -y
[root@web20 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=20
[root@web20 ~]# systemctl start mariadb
[root@web20 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"
[root@web10 ~]# mysql -ulee -plee -h172.25.254.20 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
| 20 |
+-------------+
nginx 配置
[root@Nginx ~]# vim /apps/nginx/conf/tcp/tcp.conf
stream {
upstream mysql_server {
server 172.25.254.10:80 max_fails=3 fail_timeout=30s;
server 172.25.254.20:8080 max_fails=3 fail_timeout=30s;
}
server {
listen 172.25.254.100:80;
proxy_pass mysql_server;
proxy_connect_timeout 30s;
proxy_timeout 300s;
}
}
3.9实现 FastCGI
最早的 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 服务器之间传递信息的标准化接口。
3.9.1FastCGI实战案例 : Nginx与php-fpm在同一服务器
[root@nginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel
libpng-devel libcurl-devel [root@nginx ~]#dnf install oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
[root@Nginx ~]# ./configure \
--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
打开php
生成php配置文件
#修改时区
[root@nginx ~]# vim /usr/local/php/etc/php.ini
#生成启动文件
[root@nginx ~]# cd /root/php-8.3.9/
[root@nginx php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/
添加php环境变量
[root@nginx~]#tar zxf memcache-8.2.tgz
[root@nginx~]#cdmemcache-8.2/[root@Nginx ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
6.3.3 php的动态扩展模块(php的缓存模块)
软件下载:http://pecl.php.net/package/memcache
安装memcache模块
复制测试文件到nginx发布目录中
PATH=$PATH:$HOME/bin:/apps/nginx/sbin:/usr/local/php/bin
export PATH
[root@Nginx ~]# source .bash_profile
3.10php的动态扩展模块(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
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-nonzts-20230831/
[root@nginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-20230831/
memcache.so opcache.so
4.1nginx 二次开发版本
4.1.1 openresty
Nginx 是俄罗斯人发明的, Lua 是巴西几个教授发明的,中国人章亦春把 LuaJIT VM 嵌入到 Nginx 中, 实现了 OpenResty 这个高性能服务端解决方案。
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方 模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、 Web 服务和动态网关。
OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx有效地变成一个强大的通用 Web 应用平台。这样, Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty 由于有功能强大且方便的的 API, 可扩展性更强 , 如果需要实现定制功能 ,OpenResty 是个不错的选择。
[root@nginx ~]#tar xf openresty-1.17.8.2.tar.gz
[root@nginx ~]#cd openresty-1.17.8.2/
[root@nginx openresty-1.17.8.2]#./configure --prefix=/usr/local/openresty --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --without-http_memcached_module --with-stream_ssl_module --with-stream --with-stream_realip_module --with-pcre --with-http_ssl_module[root@nginx ~]#gmake && gmake install
[root@Nginx openresty-1.17.8.2]#ln -s /apps/openresty/bin/* /usr/bin/
[root@Nginx openresty-1.17.8.2]#openresty -v
nginx version: openresty/1.17.8.2
[root@Nginx openresty-1.17.8.2]#openresty
[root@Nginx openresty-1.17.8.2]#ps -ef |grep nginx
相关文章:

企业高性能web服务器——Nginx
# 企业高性能web服务器 1、Nginx 编译安装 1.1 编译安装 Nginx 这里下载nginx-1.24.0.tar.gz和nginx-1.26.1.tar.gz可以在官方网站上下载:https://nginx.org/en/download.html 示例:nginx-1.24.0.tar.gz #提前将编译安装出现问题的安装包下载好&…...
FreeSWITCH Java ESL Client Demo
1简介 在开启Java ESL Client编程之前,请先阅读《FreeSWITCH权威指南》学习什么是FreeSWITCH Event Socket。 Java连接FreeSWITCH的ESL可以采用两种模式:inbound和outbound。 Inbound模式:Java应用作为客户端主动连接到FreeSWITCH的内置TCP…...

手摸手系列之Linux下根据自己的jdk包构建docker镜像
项目在本地导出PDF文件正常,部署到Linux服务器docker容器中导出就报错,百撕不得姐,经查,docker依赖的openjdk版本是8u112版本,而我本地是8u421版本,那就升级jdk的小版本试试。 在docker的中央仓库[点我直达…...

tomcat相关
一、部署安装tomcat 在10和20上安装tomcat [root10 ~]# dnf install java-1.8.0-openjdk.x86_64 -y #安装java环境 [root10 ~]# tar zxf apache-tomcat-9.0.93.tar.gz -C /usr/local/ #安装并启动tomcat [root10 ~]# ln -s /usr/local/apache-tomcat-9.0.93/ /usr/local/tomcat…...

20240821给飞凌OK3588-C的核心板刷Rockchip原厂的Buildroot并启动
20240821给飞凌OK3588-C的核心板刷Rockchip原厂的Buildroot并启动 2024/8/21 15:22 viewproviewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ viewproviewpro-ThinkBook-16-G5-IRH:~/repo_RK3588_Buildroot20240508$ ./build.sh lunch 3. rockchip_rk3588_evb7_…...
服务优雅上下线优雅停机
一、目的 服务发生线程锁住,需要下线服务时,保证请求中的接口不受影响,执行完后,再下线服务 二、步骤 *kill -15时执行改钩子函数 优雅停机 1.当线程锁住 2.运维那边监控到,然后进行kill -15 进程ID 3.代码这边监…...

什么是生信分析?深入探讨生物信息学的技术、方法与广泛应用
介绍 生物信息学分析,简称生信分析,是一个结合了生物学、计算机科学、信息学和统计学的多学科领域,旨在处理、分析和解释海量的生物数据。随着现代生物技术的发展,尤其是高通量测序(Next-Generation Sequencing, NGS&…...
JavaScript判断数组是否包含某个值
目录: 1.for循环/while循环2.数组的some方法3.数组的filter方法4.array.indexOf5.array.findIndex6.array.includes7.array.find8.set中的has方法 1.for循环/while循环 for循环: function contains(arr, val) {for (var i 0; i < arr.length; i) {if (arr[i] …...
C# - 如何在Windows系统中通过C#添加新的PATH条目至系统和用户环境变量
编写系统环境变量-->系统变量-->path-->添加新的列 01:直接写 using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace updatesystempath01 {class Program{stati…...

Unity | Shader基础知识(第二十二集:两次渲染)
目录 一、前言 二、“渲染两次” 三、本次成品介绍 四、第一次渲染代码 五、第二次渲染代码 六、截止目前的所有代码 七、调整代码 八、总结 一、前言 之前一直讲的shader文件中,都只写了一次CG代码。 为了大家对这部分的整体理解,我们这次渲…...

鸿蒙Harmony实战开发:Arkts构造函数
构造函数 类声明可以包含用于初始化对象状态的构造函数。 构造函数定义如下: constructor ([parameters]) {// ... } typescript 如果未定义构造函数,则会自动创建具有空参数列表的默认构造函数,例如: class Point {x: numbe…...
@vueuse/core使用useColorMode实现主题颜色切换
useColorMode 是一个在前端开发中常用的自定义钩子(Hook),尤其在需要支持深色模式和浅色模式切换的场景下。这个钩子可以根据用户的选择或系统设置动态调整页面样式。 一、安装和引入 npm install vueuse/core # 或者 yarn add vueuse/…...

生信分析入门:从基础知识到实践操作的全方位指南
随着生物学研究的数字化转型,生物信息学(简称生信)分析已经成为现代生命科学研究中的关键工具。对于刚开始接触生信分析的初学者来说,这个领域可能看起来复杂而陌生。然而,通过系统的学习和实践,生信分析可…...
【STM32 FreeRTOS】内存管理
除了FreeRTOS提供的动态内存管理方法,标准的C库也提供了函数malloc()和函数free()来实现动态的申请和释放内存。 为啥不用标准的C库自带的内存管理算法?因为标准C库的动态管理方法有如下缺点: 占用大量的代码空间,不适合用在资源…...
vue3+vite+cesium配置参考
在vite项目中使用Cesium的配置 关键: 资源目录的复制;CESIUM_BASE_URL的正确配置 //vite.config.js // ... // 安装打包复制资源插件,手动复制不需要 // npm i vite-plugin-static-copy import { viteStaticCopy } from vite-plugin-static-c…...

WEB应用服务器TOMCAT
知识点 一 、WEB技术 1、前端三大核心技术 1.1 HTML HTML ( HyperText Markup Language )超文本标记语言,它不同于一般的编程语言。超文本 即超出纯文本的范畴,例如:描述文本颜色、大小、字体等信息,或使…...
maven打包jar后运行提示“没有主清单属性”问题的几种解决方案
常用的几种maven项目打包后,jar运行提示“没有主清单属性”问题的解决方案,大部分都是要修改pom.xml文件,同时有几种自己常用的配置文件,主要供自用,勿喷。 第一种: <build><plugins><plug…...

计算机毕业设计选题推荐-民宿可视化分析-Python爬虫-随机森林算法
✨作者主页:IT毕设梦工厂✨ 个人简介:曾从事计算机专业培训教学,擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Py…...

WKWebView加载项目中网页的资源图片路径异常
问题原因,将含有html的文件通过如下方式引入到工程中: 这种处理方式,当应用程序变以后,引入的文件会被全部放在Resources目录下,而忽略你原本的文件路径信息。因此导致出问题。 解决方案: 采用如下方式引…...

算法全面剖析
算法 查找算法: 顺序查找: 基本思想: 顺序查找也称为线形查找,属于无序查找算法。从数据结构线形表的一端开始,顺序扫描,依次将扫描到的结点关键字与给定值k相比较,若相等则表示查找成功&am…...

基于FPGA的PID算法学习———实现PID比例控制算法
基于FPGA的PID算法学习 前言一、PID算法分析二、PID仿真分析1. PID代码2.PI代码3.P代码4.顶层5.测试文件6.仿真波形 总结 前言 学习内容:参考网站: PID算法控制 PID即:Proportional(比例)、Integral(积分&…...
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件(System Property Definition File),用于声明和管理 Bluetooth 模块相…...
全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比
目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec? IPsec VPN 5.1 IPsec传输模式(Transport Mode) 5.2 IPsec隧道模式(Tunne…...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...
Java + Spring Boot + Mybatis 实现批量插入
在 Java 中使用 Spring Boot 和 MyBatis 实现批量插入可以通过以下步骤完成。这里提供两种常用方法:使用 MyBatis 的 <foreach> 标签和批处理模式(ExecutorType.BATCH)。 方法一:使用 XML 的 <foreach> 标签ÿ…...

人机融合智能 | “人智交互”跨学科新领域
本文系统地提出基于“以人为中心AI(HCAI)”理念的人-人工智能交互(人智交互)这一跨学科新领域及框架,定义人智交互领域的理念、基本理论和关键问题、方法、开发流程和参与团队等,阐述提出人智交互新领域的意义。然后,提出人智交互研究的三种新范式取向以及它们的意义。最后,总结…...

深入浅出深度学习基础:从感知机到全连接神经网络的核心原理与应用
文章目录 前言一、感知机 (Perceptron)1.1 基础介绍1.1.1 感知机是什么?1.1.2 感知机的工作原理 1.2 感知机的简单应用:基本逻辑门1.2.1 逻辑与 (Logic AND)1.2.2 逻辑或 (Logic OR)1.2.3 逻辑与非 (Logic NAND) 1.3 感知机的实现1.3.1 简单实现 (基于阈…...
省略号和可变参数模板
本文主要介绍如何展开可变参数的参数包 1.C语言的va_list展开可变参数 #include <iostream> #include <cstdarg>void printNumbers(int count, ...) {// 声明va_list类型的变量va_list args;// 使用va_start将可变参数写入变量argsva_start(args, count);for (in…...
Python 高效图像帧提取与视频编码:实战指南
Python 高效图像帧提取与视频编码:实战指南 在音视频处理领域,图像帧提取与视频编码是基础但极具挑战性的任务。Python 结合强大的第三方库(如 OpenCV、FFmpeg、PyAV),可以高效处理视频流,实现快速帧提取、压缩编码等关键功能。本文将深入介绍如何优化这些流程,提高处理…...

【Veristand】Veristand环境安装教程-Linux RT / Windows
首先声明,此教程是针对Simulink编译模型并导入Veristand中编写的,同时需要注意的是老用户编译可能用的是Veristand Model Framework,那个是历史版本,且NI不会再维护,新版本编译支持为VeriStand Model Generation Suppo…...