一 .Web 服务基础介绍 Web 服务介绍 1.1Apache 经典的 Web 服务端 Apache 起初由美国的伊利诺伊大学香槟分校的国家超级计算机应用中心开发 目前经历了两大版天职别是 1.X 和 2.X 其可以通过编译安装实现特定的功能 1.1.1Apache prefork 模型 预派生模式,有一个主控制历程,然后天生多个子历程,使用 select 模型,最大并发 1024 每个子历程有一个独立的线程响应用户哀求 相对比力占用内存,但是比力稳固,可以设置最大和最小历程数 是最古老的一种模式 , 也是最稳固的模式,适用于访问量不是很大的场景 优点:稳固 缺点:每个用户哀求需要对应开启一个历程 , 占用资源较多,并发性差 , 不适用于高并发场景 1.1.2Apache worker 模型 一种多历程和多线程混合的模型 有一个控制历程,启动多个子历程 每个子历程里面包罗固定的线程 使用线程程来处理哀求 当线程不敷使用的时间会再启动一个新的子历程 , 然后在历程里面再启动线程处理哀求, 由于其使用了线程处理哀求,因此可以承受更高的并发 优点:相比 prefork 占用的内存较少,可以同时处理更多的哀求 缺点:使用 keepalive 的长毗连方式,某个线程会不停被占据,纵然没有传输数据,也需要不停等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用(该问题在 prefork模式下,同样会发生) 1.1.3 Apache event 模型 Apache 中最新的模式, 2012 年发布的 apache 2.4.X 系列正式支持 event 模型 , 属于事件驱动模型 (epoll) 每个历程响应多个哀求,在如今版本里的已经是稳固可用的模式 它和 worker 模式很像,最大的区别在于,它解决了 keepalive 场景下长期被占用的线程的资源浪费问题 (某些线程由于被 keepalive ,空挂在哪里等待,中间几乎没有哀求过来,甚至比及超时) event MPM 中,会有一个专门的线程来管理这些 keepalive 类型的线程 当有真实哀求过来的时间,将哀求通报给服务线程,实行完毕后,又答应它释放。这样加强了高并发场 景下的哀求处理本领 优点:单线程响应多哀求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理 keep-alive类型的线程,当有真实哀求过来的时间,将哀求通报给服务线程,实行完毕后,又答应它释放 缺点:没有线程安全控制 二 .Nginx 架构和安装 Nginx 介绍 Nginx : engine X , 2002 年开发,分为社区版和商业版 (nginx plus ) 2019 年 3 月 11 日 F5 Networks 6.7 亿美元的价格收购 Nginx 是免费的、开源的、高性能的 HTTP 和反向代理服务器、邮件代理服务器、以及 TCP/UDP 代理服务器 解决 C10K 问题( 10K Connections ) Nginx 官网: http://nginx.org nginx 的别的的二次发行版: Tengine :由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加 了很多高级功能和特性。 Tengine 的性能和稳固性已经在大型的网站如淘宝网,天猫商城等得到了 很好的检验。它的终极目标是打造一个高效、稳固、安全、易用的 Web 平台。从 2011 年 12 月开始,Tengine成为一个开源项目官网 : http://tengine.taobao.org/ OpenResty :基于 Nginx 与 Lua 语言的高性能 Web 平台, 章亦春团队开发,官网: http://openr esty.org/cn/ 2.1 Nginx 功能介绍 静态的 web 资源服务器 html ,图片, js , css , txt 等静态资源 http/https 协议的反向代理 结合 FastCGI/uWSGI/SCGI 等协议反向代理动态资源哀求 tcp/udp 协议的哀求转发(反向代理) imap4/pop3 协议的反向代理 2.2 基础特性 模块化设计,较好的扩展性 高可靠性 支持热部署:不停机更新配置文件,升级版本,更换日志文件 低内存斲丧: 10000 个 keep-alive 毗连模式下的非运动毗连,仅需 2.5M 内存 event-driven,aio,mmap , sendfile 2.2Web 服务相关的功能 假造主机( server ) 支持 keep-alive 和管道毗连 ( 利用一个毗连做多次哀求 ) 访问日志(支持基于日志缓冲提高其性能) url rewirte 路径别名 基于 IP 及用户的访问控制 支持速率限制及并发数限制 重新配置和在线升级而无须中断客户的工作历程 Nginx 版本和安装方式 Nginx 版本 Mainline version 重要开发版本 , 一般为奇数版本号 , 比如 1.19 Stable version 当前最新稳固版 , 一般为偶数版本 , 如 :1.20 Legacy versions 旧的稳固版 , 一般为偶数版本 , 如 :1.18 Nginx 安装可以使用 yum 或源码安装,但是保举使用源码编译安装 yum 的版本比力旧 编译安装可以更方便自界说相关路径 使用源码编译可以自界说相关功能,更方便业务的上的使用 编译安装 Nginx 安装:
[root@nginx-node1 ~]# tar zxf nginx-1.24.0.tar.gz ---解压
进入目次:
[root@nginx-node1 ~]# cd nginx-1.24.0/
[root@nginx-node1 nginx-1.24.0]#
安装依赖性:
[root@nginx-node1 nginx-1.24.0]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@nginx-node1 nginx-1.24.0]# useradd -s /sbin/nologin -M nginx ---- 添加使用用户
编译模块
[root@nginx-node1 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 过了之后会天生Makefile文件,make规则
make clean ---- 可以让之前做的编译模块还原
实行make install
[root@nginx-node1 nginx-1.24.0]# make && make install
把nginx软件的下令实行添加到环境变量中
[root@nginx-node1 ~]# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=$PATH:/usr/local/nginx/sbin ---- 添加这个
[root@nginx-node1 ~]# source ~/.bash_profile
[root@nginx-node1 ~]# du -sh /usr/local/nginx/sbin/nginx
进到这个目次下开启nginx:
[root@nginx-node1 ~]# cd /usr/local/nginx/sbin/
[root@nginx-node1 sbin]# ls
nginx
[root@nginx-node1 sbin]#
[root@nginx-node1 sbin]# ./nginx
如何关闭nginx:
[root@nginx-node1 ~]# /usr/local/nginx/sbin/nginx -s stop
关闭debug模式:
[root@nginx-node1 nginx-1.24.0]# vim auto/cc/gcc
启动nginx:
[root@nginx-node1 nginx-1.24.0]# systemctl daemon-reload [root@nginx-node1 nginx-1.24.0]# systemctl start nginx nginx完成安装以后,有四个重要的目次(/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 二进制启动脚本,可以接受不同的参数以实现不同的功能。 平滑升级和回滚案例 解压软件包:
[root@nginx-node1 ~]# tar zxf echo-nginx-module-0.63.tar.gz
[root@nginx-node1 ~]# tar zxf nginx-1.26.2.tar.gz 进入目次,编译:
[root@nginx-node1 nginx-1.26.2]# ./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
然后只要make无需要make install
[root@nginx-node1 nginx-1.26.2]# make
平滑升级:
对旧版本的下令举行备份:
旧版本:
[root@nginx-node1 sbin]# nginx -s stop
[root@nginx-node1 ~]# rm -rf /usr/local/nginx/
[root@nginx-node1 ~]# cd /root/nginx-1.24.0/
[root@nginx-node1 nginx-1.24.0]# make install
[root@nginx-node1 nginx-1.24.0]# cd /usr/local/nginx/
[root@nginx-node1 nginx]# ls
conf html logs sbin
[root@nginx-node1 nginx]# cd sbin/
[root@nginx-node1 sbin]# ls
nginx
[root@nginx-node1 sbin]# nginx
root@nginx-node1 objs]# cd /usr/local/nginx/sbin/
[root@nginx-node1 sbin]# ls
nginx
[root@nginx-node1 sbin]# cp nginx nginx.old
[root@nginx-node1 sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/
[root@nginx-node1 sbin]# ps aux | grep nginx
root 14254 0.0 0.0 9868 1928 ? Ss 09:54 0:00 nginx: master process nginx
nginx 14255 0.0 0.1 14200 5128 ? S 09:54 0:00 nginx: worker process
root 14275 0.0 0.0 221664 2304 pts/2 S+ 09:58 0:00 grep --color=auto nginx
[root@nginx-node1 ~]# cd nginx-1.24.0/
[root@nginx-node1 nginx-1.24.0]# cd src/
[root@nginx-node1 src]# cd core/
[root@nginx-node1 core]# curl -I 172.25.254.200
HTTP/1.1 200 OK
Server: nginx/1.26.2 ----- 平滑升级成功
Date: Thu, 15 Aug 2024 14:10:49 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 13:53:46 GMT
Connection: keep-alive
ETag: "66be086a-267"
Accept-Ranges: bytes
版本回滚:
把旧版本激活,新版本接纳:
激活:
[root@nginx-node1 sbin]# kill -HUP 14254
[root@nginx-node1 sbin]# ps aux | grep nginx
root 14254 0.0 0.0 9868 2184 ? Ss 09:54 0:00 nginx: master process nginx
root 14276 0.0 0.1 9896 6656 ? S 09:59 0:00 nginx: master process nginx
nginx 14277 0.0 0.1 14228 5392 ? S 09:59 0:00 nginx: worker process
nginx 14339 0.0 0.1 14200 5144 ? S 10:12 0:00 nginx: worker process --- 新的
root 14341 0.0 0.0 221664 2304 pts/2 S+ 10:12 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]#
接纳:
[root@nginx-node1 sbin]# kill -WINCH 14276
[root@nginx-node1 sbin]# ps aux | grep nginx
root 14254 0.0 0.0 9868 2184 ? Ss 09:54 0:00 nginx: master process nginx
root 14276 0.0 0.1 9896 6656 ? S 09:59 0:00 nginx: master process nginx
nginx 14339 0.0 0.1 14200 5144 ? S 10:12 0:00 nginx: worker process
root 14343 0.0 0.0 221664 2304 pts/2 S+ 10:14 0:00 grep --color=auto nginx
回到旧版本了:
[root@nginx-node1 core]# curl -I 172.25.254.200
HTTP/1.1 200 OK
Server: nginx/1.0
Date: Thu, 23 Aug 2024 14:15:36 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 13:53:46 GMT
Connection: keep-alive
ETag: "66be086a-267"
Accept-Ranges: bytes
把新的历程杀掉:
[root@nginx-node1 sbin]# cp nginx nginx.new
[root@nginx-node1 sbin]# \cp -f nginx.old nginx
[root@nginx-node1 sbin]# ls
nginx nginx.new nginx.old
[root@nginx-node1 sbin]# ps aux | grep nginx
root 14254 0.0 0.0 9868 2184 ? Ss 09:54 0:00 nginx: master process nginx
root 14276 0.0 0.1 9896 6656 ? S 09:59 0:00 nginx: master process nginx
nginx 14339 0.0 0.1 14200 5400 ? S 10:12 0:00 nginx: worker process
root 14362 0.0 0.0 221664 2304 pts/2 S+ 10:17 0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# kill -9 14276
[root@nginx-node1 sbin]# ps aux | grep nginx
root 14254 0.0 0.0 9868 2184 ? Ss 09:54 0:00 nginx: master process nginx
nginx 14339 0.0 0.1 14200 5400 ? S 10:12 0:00 nginx: worker process
root 14364 0.0 0.0 221664 2304 pts/2 S+ 10:18 0:00 grep --color=auto nginx
三 Nginx 核心配置详解
nginx 配置文件格式说明 Nginx 主配置文件的配置指令方式: 主配置文件结构:四部分 配置文件由指令与指令块构成 每条指令以 ; 分号结尾,指令与值之间以空格符号分隔 可以将多条指令放在同一行 , 用分号分隔即可 , 但可读性差 , 不保举 指令块以 { } 大括号将多条指令组织在一起 , 且可以嵌套指令块 include 语句答应组合多个配置文件以提升可维护性 使用 # 符号添加解释,提高可读性 使用 $ 符号使用变量 部分指令的参数支持正则表达式 directive value [value2 ...]; 注意 (1) 指令必须以分号结尾 (2) 支持使用配置变量 内建变量:由 Nginx 模块引入,可直接引用 自界说变量:由用户使用 set 下令界说 , 格式 : set variable_name value; 引用变量: $variable_name main block :主配置段,即全局配置段,对 http,mail 都有效 # 事件驱动相关的配置 event { ... } #http/https 协议相关配置段 http { ... } # 默认配置文件不包括下面两个块 #mail 协议相关配置段 mail { ... } #stream 服务器相关配置段 stream { ... } 默认的 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 监听的端口 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 } nginx配置中的root和alias
新建一个web站点:
[root@nginx-node1 core]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
use epoll; ---- 加上这个,运用epoll
}
#gzip on;
include "/usr/local/nginx/conf.d/*.conf"; ------ 再加上子配置文件
创建子配置文件目次:
[root@nginx-node1 core]# mkdir /usr/local/nginx/conf.d -p
[root@nginx-node1 core]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
}
[root@nginx-node1 core]# mkdir -p /data/web/html
[root@nginx-node1 core]# echo www.nginx.org > /data/web/html/index.html
[root@nginx-node1 core]# nginx -t ---- 校验语法是否有问题
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx-node1 core]# nginx -s reload ---- 刷新
访问172.25.254.200
去访问另外的路径,root和alias
[root@nginx-node1 core]# vim /usr/local/nginx/conf.d/vhost.conf
root:
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
location /test1/ {
root /data/web;
}
}
[root@nginx-node1 core]# mkdir /data/web/test1 -p
[root@nginx-node1 core]# echo /data/web/test1 > /data/web/test1/index.html
[root@nginx-node1 core]# nginx -s reload --- 刷新一下
访问172.25.254.100/test1
alias:
[root@nginx-node1 core]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
location /test1/ {
root /data/web;
}
location /test2 {
alias /data/web/test1;
}
}
[root@nginx-node1 core]# nginx -s reload --- 刷新一下
访问172.25.254.200/test2/
Nginx 账户认证功能 [root@Nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee #-b 表 示非交互建立用户认证 Adding password for user admin [root@Nginx ~]# htpasswd -mb /usr/local/nginx/conf/.htpasswd lee lee Adding password for user nginx [root@Nginx ~]# cat /usr/local/nginx/conf/.htpasswd adminapr1$haGCKgCT$myogggALmqNecTyNupsWQ/ leeapr1$H97AyQPF$kGU.Tc4zn1E4Zkp/M4R6G. [root@Nginx ~]# mkdir /webdata/nginx/timinglee.org/lee/login [root@Nginx ~]# echo login > /webdata/nginx/nginx.org/lee/login/index.html [root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf server { listen 80; server_name lee.nginx.org; location /login { root /webdata/nginx/nginx.org/lee; index index.html; auth_basic "login password"; auth_basic_user_file "/usr/local/nginx/conf/.htpasswd"; } } # 重启 Nginx 并访问测试 [root@node100 ~]# curl lee.nginx.org/login/ -u lee:lee login [root@node100 ~]# curl lee.nginx.org/login/ -u admin:lee login nginx的自界说错误页面
创建文件目次:
[root@nginx ~]# mkdir /data/web/errorpage -p
[root@nginx ~]# echo error page > /data/web/erroepage/40x.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
#error_log /var/log/nginx.org/error.log;
#access_log /var/log/nginx.org/access.log;
location /ding {
root /data/web;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location = /40x.html {
root /data/web/errorpage;
}
}
[root@nginx ~]# systemctl restart nginx.service
网页访问172.25.254.100/ppd
自界说错误日志 [root@Nginx ~]# mkdir "/var/log/nginx" -p [root@Nginx ~]# vim /usr/local/nginx/conf.d/vhosts.conf server { listen 80; server_name lee.nginx.org; error_page 404 /40x.html; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location = /40x.html { root /webdata/nginx/nginx/lee/errors; } } # 重启 nginx 并访问不存在的页面举行测试并验证是在指定目次天生新的日志文件 nginx中的文件检测
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/nginx.org/error.log;
access_log /var/log/nginx.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /ding {
root /data/web;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location /40x.html {
root /data/web/errorpage;
}
}
[root@nginx ~]# systemctl restart nginx.service
[root@nginx ~]# rm -rf /data/web/html/index.html
[root@nginx ~]# rm -rf /data/web/html/error
[root@nginx ~]# curl www.nginx.org ---- 如今检测到文件都没有,报500的错
<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.nginx.org
error default
还原返来
[root@nginx ~]# echo www.nginx.org > /data/web/html/index.html
nginx-下载服务器的设定及优化
创建下载文件的目次:
[root@nginx ~]# mkdir /data/web/download -p
[root@nginx ~]# dd if=/dev/zero of=/data/web/download/dingfile bs=1M count=100 ---- 做一个巨细为100M的文件,并放到刚才创建的目次。创建的一个数据。
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0394146 s, 2.7 GB/s
[root@nginx ~]#
写一个location,访问路径
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/nginx.org/error.log;
access_log /var/log/nginx.org/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html;
location /ding {
root /data/web;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
location /40x.html {
root /data/web/errorpage;
}
location /download {
root /data/web;
autoindex on; ----- 让文件可以长列表显示
autoindex_localtime on; -----on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间
autoindex_exact_size off; ---- 计算文件确切巨细(单位bytes),此为默认值,off只显示大概巨细(单位kb、mb、gb)
limit_rate 1024k; -------- 限速,默认不限速 }
}
}
rewrite 案例 : 域名永久与临时重定向 [root@nginx conf.d]# vim /usr/local/nginx/conf.d/vars.conf
location / {
root /data/web/var;
index index.html;
rewrite / http://www.nginx.com permanent; --- 永久重定向 301 两个不能同时启用
#rewrite / http://www.nginx.com redirext; --- 临时重定向 302
}
[root@nginx conf.d]# mkdir -p /data/web/var
[root@nginx conf.d]# echo var page > /data/web/var/index.html
[root@nginx conf.d]# nginx -s reload
测试:
curl 不支持重定向
永久的301:
[root@nginx conf.d]# curl -I var.nginx.org
HTTP/1.1 301 Moved Permanently
Server: xiaoding/1.1
Date: Sun, 18 Aug 2024 11:40:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout=60
Location: http://www.nginx.com
换成临时的302:
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vars.conf
location / {
root /data/web/var;
index index.html;
#rewrite / http://www.nginx.com permanent;
rewrite / http://www.nginx.com redirext;
}
break 和 last 区别案例 [root@nginx conf.d]# mkdir /data/web/html/{test1,test2,break,last} -p
[root@nginx conf.d]# echo test1 > /data/web/html/test1/index.html
[root@nginx conf.d]# echo test2 > /data/web/html/test2/index.html
[root@nginx conf.d]# echo break > /data/web/html/break/index.html
[root@nginx conf.d]# echo last > /data/web/html/last/index.html
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vars.conf
server {
listen 80;
server_name var.nginx.org;
root /data/web/html;
index index.html;
location /break {
rewrite ^/break/(.*) /test1/$1;
rewrite ^/test1/(.*) /test2/$1;
}
location /last {
rewrite ^/last/(.*) /test1/$1;
rewrite ^/test1/(.*) /test2/$1;
}
location /test1 {
default_type text/html;
return 203 " hahahahaha";
}
location /test2 {
root /data/web/html;
}
}
访问:
[root@nginx conf.d]# curl var.nginx.org/break/
test2
[root@nginx conf.d]# curl var.nginx.org/last/
test2
[root@nginx conf.d]# curl var.nginx.org/test1/
xiaoding hahahahaha
[root@nginx conf.d]# curl var.nginx.org/test2/
test2
break和last效果示例:
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vars.conf
server {
listen 80;
server_name var.nginx.org;
root /data/web/html;
index index.html;
location /break {
root /data/web/html;
rewrite ^/break/(.*) /test1/$1 break; ----- 加上break,实行到这里就不访问下面的了,看的是test1里面的内容
rewrite ^/test1/(.*) /test2/$1;
}
location /last {
root /data/web/html;
rewrite ^/last/(.*) /test1/$1 last;
rewrite ^/test1/(.*) /test2/$1;
}
location /test1 {
default_type text/html;
return 203 " hahahahaha";
}
location /test2 {
root /data/web/html;
}
}
测试
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl var.nginx.org/break/index.html #访问break时,会终止,但不会跳出当前的location
test1
[root@nginx ~]#
[root@nginx~]# curl var.nginx.org/last/index.html #访问last时,也会终止,但是会跳出当前的location,继承探求路径
rewrite 案例 : 自动跳转 https [root@centos8 ~]#vim /apps/nginx/conf.d/pc.conf server { listen 443 ssl; listen 80; ssl_certificate /apps/nginx/certs/www.timinglee.org.crt; ssl_certificate_key /apps/nginx/certs/www.timinglee.org.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; server_name www.timniglee.org; location / { # 针对全站跳转 root /data/nginx/html/pc; index index.html; if ($scheme = http ){ # 如果没有加条件判断,会导致死循环 rewrite / https://$host redirect; } } location /login { # 针对特定的 URL 举行跳转 https if ($scheme = http ){ # 如果没有加条件判断,会导致死循环 rewrite / https://$host/login redirect; } } } # 重启 Nginx 并访问测试 [root@centos7 ~]#curl -ikL www.timinglee.org HTTP/1.1 302 Moved Temporarily Server: nginx/1.18.0 Date: Thu, 08 Oct 2020 15:23:48 GMT Content-Type: text/html Content-Length: 145 Connection: keep-alive Location: https://www.magedu.org HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Thu, 08 Oct 2020 15:23:48 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Sat, 26 Sep 2020 01:18:32 GMT Connection: keep-alive ETag: "5f6e96e8-7" Accept-Ranges: bytes pc web rewrite 案例 : 判断文件是否存在 [root@centos8 ~]#vim /apps/nginx/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; if (!-e $request_filename) { rewrite .* http://www.timinglee.org/index.html; # 实现客户端浏览器的 302 跳转 #rewrite .* /index.html; #web 服务器内部跳转 } } # 重启 Nginx 并访问测试 Nginx 防盗链 防盗链基于客户端携带的 referer 实现, referer 是记载打开一个页面之前记载是从哪个页面跳转过来的标 记信息,如果别人只链接了本身网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗 链,referer就是之前的谁人网站域名,正常的 referer 信息有以下几种: none : # 哀求报文首部没有 referer 首部, # 比如用户直接在浏览器输入域名访问 web 网站,就没有 referer 信息。 blocked : # 哀求报文有 referer 首部,但无有效值,比如为空。 server_names : #referer 首部中包罗本主机名及即 nginx 监听的 server_name 。 arbitrary_string : # 自界说指定字符串,但可使用 * 作通配符。示例 : *.timinglee.org www.timinglee.* regular expression : # 被指定的正则表达式模式匹配到的字符串 , 要使用 ~ 开头,例如: ~.*\.timinglee\.com 实现盗链 [root@nginx conf.d]# mkdir -p /data/web/html/images
[root@nginx conf.d]# cd /data/web/html/images/
[root@nginx images]# ls
1.jpg
网页访问172.25.254.100/images/1.jpg
另一台主机托入盗链:
[root@nginx-node1 ~]# dnf install httpd -y
[root@nginx-node1 ~]# cd /var/www/html/
[root@nginx-node1 html]# ls
2.png
[root@nginx-node1 html]# mv 2.png /var/www/html/index.html
[root@nginx-node1 html]# ls
index.html
[root@nginx-node1 html]# cat index.html
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title>盗链</title>
</head>
<body>
<img src="http://www.nginx.org/images/1.jpg" >
<h1 style="color:red">hahaha</h1>
<p><a href=http://www.nginx.org>xixi</a>hehe</p>
</body>
</html>
[root@nginx-node1 html]# systemctl start httpd
去网页访问172.25.254.10
防止盗链发生
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
listen 443 ssl;
server_name www.nginx.org;
root /data/web/html;
index index.html;
ssl_certificate /usr/local/nginx/certs/nginx.org.crt;
ssl_certificate_key /usr/local/nginx/certs/nginx.org.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
location /images {
valid_referers none blocked server_names *.nginx.org ~/.baidu/.;
if ( $invalid_referer ){
return 404;
}
}
}
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
listen 443 ssl;
server_name www.nginx.org;
root /data/web/html;
index index.html;
ssl_certificate /usr/local/nginx/certs/nginx.org.crt;
ssl_certificate_key /usr/local/nginx/certs/nginx.org.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#location / {
# valid_referers none blocked server_names *.nginx.org ~/.baidu/.;
# if ( $invalid_referer ){
# return 404;
#}
#}
location /images {
valid_referers none blocked server_names *.nginx.org ~/.baidu/.;
if ( $invalid_referer ){
rewrite ^/ http://www.nginx.org/tupi.jpg;
}
}
}
nginx 反向代理及动静分离的实现
两台主机:
172.25.254.10:
[root@nginx-node1 ~]# echo 172.25.254.10 > /var/www/html/index.html
172.25.254.20:
[root@nginx-node2 ~]# echo 172.25.254.20 > /var/www/html/index.html
[root@nginx-node2 ~]# systemctl restart httpd
[root@nginx-node2 ~]# mkdir -p /var/www/html/static
[root@nginx-node2 ~]# echo static 172.25.254.20 > /var/www/html/static/index.html
172.25.254.100:
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
location / {
proxy_pass http://172.25.254.10:80;
}
location /static {
proxy_pass http://172.25.254.20:8080;
}
}
172.25.254.10:
[root@nginx-node1 ~]# dnf install php -y
[root@nginx-node1 ~]# systemctl restart httpd
[root@nginx-node1 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.nginx.org;
location ~ \.php$ {
proxy_pass http://172.25.254.10:80;
}
location /static {
proxy_pass http://172.25.254.20:8080;
}
}
[root@nginx ~]# nginx -s reload
网页访问www.nginx.org/index.php = 172.25.254.10
nginx 反向代理的负载均衡
七层:
配置nginx反向代理
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
server 172.25.254.10:80 fail_timeout=15s max_fails=3;
server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
server 172.25.254.100:80 backup;
}
server {
listen 80;
server_name www.nginx.org;
location / {
proxy_pass http://webcluster;
}
}
[root@nginx nginx]# nginx -s reload
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
ip_hash; ---- 算法,让同一个IP哀求,访问到同一个主机上面
server 172.25.254.10:80 fail_timeout=15s max_fails=3;
server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
#server 172.25.254.100:80 backup; --- hash写完不能加backup 防止调度到backup上面去
}
server {
listen 80;
server_name www.nginxorg;
location / {
proxy_pass http://webcluster;
}
}
[root@nginx nginx]# nginx -s reload
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
#ip_hash;
hash $request_uri consistent; ---- 动态算法,hash一致性
server 172.25.254.10:80 fail_timeout=15s max_fails=3;
server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
#server 172.25.254.100:80 backup;
}
server {
listen 80;
server_name www.nginx.org;
location / {
proxy_pass http://webcluster;
}
}
[root@nginx nginx]# nginx -s reload
172.25.254.10:
[root@nginx-node1 ~]# mkdir -p /var/www/html/static
[root@nginx-node1 ~]# echo 172.25.254.10 static > /var/www/html/static/index.html
对cookie举行hash
[root@nginx nginx]# vim /usr/local/nginx/conf.d/vhost.conf
upstream webcluster {
#ip_hash;
#hash $request_uri consistent;
hash $cookie_ding; ------- 对cookie举行hash
server 172.25.254.10:80 fail_timeout=15s max_fails=3;
server 172.25.254.20:8080 fail_timeout=15s max_fails=3;
#server 172.25.254.100:80 backup;
}
server {
listen 80;
server_name www.timingding.org;
location / {
proxy_pass http://webcluster;
}
}
[root@nginx nginx]# nginx -s reload
四层负载:
172.25.254.10和20 安装bind
配置DNS
[root@nginx-node1 ~]# dnf install bind -y
[root@nginx-node2 ~]# dnf install bind -y
[root@nginx-node1 ~]# vim /etc/named.conf
// 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; --- 改为no
[root@nginx-node1 ~]# vim /etc/named.rfc1912.zones
zone "timingding.org" IN {
type master;
file "nginx.org.zone";
allow-update { none; };
};
[root@nginx-node1 ~]# cd /var/named/
[root@nginx-node1 named]#
[root@nginx-node1 named]# cp named.localhost nginx.org.zone -p
[root@nginx-node1 named]# vim nginx.org.zone
$TTL 1D
@ IN SOA ns.nginx.org. root.nginx.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.nginx.org.
ns A 172.25.254.10
www A 172.25.254.10
[root@nginx-node1 named]# systemctl start named
[root@nginx-node1 named]# systemctl start named
[root@nginx-node1 named]# dig www.nginx.org @172.25.254.10
; <<>> DiG 9.16.23-RH <<>> www.nginx.org @172.25.254.10
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27004
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 783e7ecaf1c29da10100000066c22dd4285bd72eb63c17c1 (good)
;; QUESTION SECTION:
;www.nginx.org. IN A
;; ANSWER SECTION:
www.nginx.org. 86400 IN A 172.25.254.10
;; Query time: 0 msec
;; SERVER: 172.25.254.10#53(172.25.254.10)
;; WHEN: Sun Aug 23 13:22:28 EDT 2024
;; MSG SIZE rcvd: 91
[root@nginx-node1 named]# scp -p /etc/named.{conf,rfc1912.zones} root@172.25.254.20:/etc/
The authenticity of host '172.25.254.20 (172.25.254.20)' can't be established.
ED25519 key fingerprint is SHA256:JAc5p6OZrNZsG8UQHYDL8RDEOeKmzy1IWQlXlmvsuSw.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.254.20' (ED25519) to the list of known hosts.
root@172.25.254.20's password:
named.conf 100% 1727 3.0MB/s 00:00
named.rfc1912.zones 100% 1129 2.8MB/s 00:00
[root@nginx-node1 named]#
[root@nginx-node1 named]#
scp -p /var/named/nginx.org.zone root@172.25.254.20:/var/named/nginx.org.zone
root@172.25.254.20's password:
nginx.org.zone 100% 210 394.6KB/s 00:00
[root@nginx-node1 named]#
[root@nginx-node2 ~]# vim /var/named/nginx.org.zone
$TTL 1D
@ IN SOA ns.nginx.org. root.nginx.org. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.nginx.org.
ns A 172.25.254.20
www A 172.25.254.20
[root@nginx-node2 named]# chgrp named timingding.org.zone
[root@nginx-node2 ~]# systemctl start named
[root@nginx-node2 named]# dig www.nginx.org @172.25.254.20
; <<>> DiG 9.16.23-RH <<>> www.nginx.org @172.25.254.20
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48200
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 3da0f57e7816a0bb0100000066c22fd3ea997c832e0fcd37 (good)
;; QUESTION SECTION:
;www.nginx.org. IN A
;; ANSWER SECTION:
www.nginx.org. 86400 IN A 172.25.254.20
;; Query time: 0 msec
;; SERVER: 172.25.254.20#53(172.25.254.20)
;; WHEN: Sun Aug 23 13:30:59 EDT 2024
;; MSG SIZE rcvd: 91
FastCGI 实战案例 : Nginx 与 php-fpm 在同一服务器 [root@Nginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel # 解压源码并安装 [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 相关配置优化 [root@Nginx ~]# cd /usr/local/php/etc [root@Nginx etc]# cp php-fpm.conf.default php-fpm.conf [root@Nginx etc]# vim php-fpm.conf 去掉解释 pid = run/php-fpm.pid # 指定 pid 文件存放位置 [root@Nginx etc]# cd php-fpm.d/ [root@Nginx php-fpm.d]# cp www.conf.default www.conf # 天生主配置文件 [root@Nginx php-fpm.d]# cd /root/php-8.3.9/ [root@Nginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini [root@Nginx ~]# vim /usr/local/php/etc/php.ini [Date] ; Defines the default timezone used by the date functions ; https://php.net/date.timezone date.timezone = Asia/Shanghai # 修改时区 # 天生启动文件 [root@Nginx ~]# cd /root/php-8.3.9/ [root@Nginx php-8.3.9]# cp sapi/fpm/php-fpm.service /lib/systemd/system/ # Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit. #ProtectSystem=full # 解释该内容 [root@Nginx php-8.3.9]# systemctl start php-fpm.service [root@Nginx php-8.3.9]# netstat -antlupe | grep php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0 820758 176202/php-fpm: mas
准备 php 测试页面 [root@Nginx ~]# mkdir /data/php -p [root@centos8 ~]# cat /data/php/index.php #php 测试页面 <?php phpinfo(); ?>
Nginx 配置转发 Nginx 安装完成之后默认天生了与 fastcgi 的相关配置文件,一般保存在 nginx 的安装路径的 conf 目次当 中,比如 /apps/nginx/conf/fastcgi.conf 、 /apps/nginx/conf/fastcgi_params 。 [root@Nginx ~]# vim /apps/nginx/conf.d/php.conf server { listen 80; server_name php.nginx.org; root /data/php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } # 重启 Nginx 并访问 web 测试 [root@Nginx ~]# nginx -s reload
添加 php 环境变量 [root@Nginx ~]# vim .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATHHOME/bin:/apps/nginx/sbin:/usr/local/php/bin export PATH [root@Nginx ~]# source .bash_profile php的动态扩展模块(php的缓存模块):安装memcache模块
[root@Nginx ~]# tar zxf memcache-8.2.tgz [root@Nginx ~]# cd memcache-8.2/ [root@Nginx memcache-8.2]# yum install autoconf [root@Nginx memcache-8.2]# phpize [root@Nginx memcache-8.2]# ./configure && make && make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non zts-20230831/ [root@Nginx memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts- 20230831/ memcache.so opcache.so 复制测试文件到nginx发布目次中
[root@Nginx ~]# cd memcache-8.2/ [root@Nginx memcache-8.2]# ls autom4te.cache config.log configure.ac example.php Makefile.fragments README build config.m4 config.w32 include Makefile.objects run tests.php config9.m4 config.nice CREDITS libtool memcache.la src config.h config.status docker LICENSE memcache.php tests config.h.in configure Dockerfile Makefile modules [root@Nginx memcache-8.2]# cp example.php memcache.php /data/php/ [root@Nginx ~]# vim /data/php/memcache.php define('ADMIN_USERNAME','admin'); // Admin Username define('ADMIN_PASSWORD','lee'); // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s'); define('GRAPH_SIZE',200); define('MAX_ITEM_DUMP',50); $MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array 配置php加载memcache模块
[root@Nginx ~]# vim /usr/local/php/etc/php.ini ;extension=zip extension=memcache ;zend_extension=opcache [root@Nginx ~]# systemctl reload php-fpm [root@Nginx no-debug-non-zts-20230831]# php -m | grep mem memcache 部署 memcached [root@Nginx ~]# yum install memcached -y [root@Nginx ~]# systemctl enable --now memcached.service [root@Nginx ~]# netstat -antlupe | grep memcache tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 976 1037243 186762/memcached [root@Nginx ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="-l 127.0.0.1,::1" 测试: 访问 http://php.timinglee.org/example.php 不停刷新 访问 http://php.timinglee.org/memcache.php 查看掷中效果 php高速缓存 :
[root@Nginx ~]# rm -fr /apps/nginx/
[root@Nginx ~]# tar zxf srcache-nginx-module-0.33.tar.gz [root@Nginx ~]# tar zxf memc-nginx-module-0.20.tar.gz [root@Nginx ~]# cd nginx-1.26.1/ [root@Nginx nginx-1.26.1]# ./configure --prefix=/apps/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 -- add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module- 0.33 [root@Nginx nginx-1.26.1]# make && make install [root@Nginx ~]# vim /apps/nginx/conf.d/php.conf upstream memcache { server 127.0.0.1:11211; keepalive 512; } server { listen 80; server_name php.nginx.org; root /data/php; location /memc { internal; memc_connect_timeout 100ms; memc_send_timeout 100ms; memc_read_timeout 100ms; set $memc_key $query_string; # 使用内置变量 $query_string 来作为 key set $memc_exptime 300; # 缓存失效时间 300 秒 memc_pass memcache; } location ~ \.php$ { set $key $uri$args; # 设定 key 的值 srcache_fetch GET /memc $key; # 检测 mem 中是否有要访问的 php srcache_store PUT /memc $key; # 缓存为加载的 php 数据 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } [root@Nginx ~]# systemctl start nginx.service nginx 二次开发版本------编译安装 openresty [root@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 src]#wget https://openresty.org/download/openresty-1.17.8.2.tar.gz [root@Nginx src]#tar xf openresty-1.17.8.2.tar.gz [root@Nginx src]#cd openresty-1.17.8.2/ [root@Nginx openresty-1.17.8.2]#./configure \ --prefix=/apps/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 openresty-1.17.8.2]#make && make 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 tomcat 的功能介绍 : Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器,属于轻量级应用服务器,在中小型体系和 并发访问用户不是很多的场合下被普遍使用, Tomcat 具有处理 HTML 页面的功能,它还是一个 Servlet 和 JSP 容器 起始于 SUN 公司的一个 Servlet 的参考实现项目 Java Web Server ,开发者是 James Duncan Davidson , 在 1999 年,将项目贡献给了 apache 软件基金会( ASF ),和 ASF 现有的项目 JServ 合并,并开源成为顶级 项目 Tomcat 仅仅实现了 Java EE 规范中与 Servlet 、 JSP 相关的类库,是 JavaEE 不完备实现。 1999 年发布初始版本是 Tomcat 3.0 ,实现了 Servlet 2.2 和 JSP 1.1 规范。 Tomcat 4.x 发布时,内建了 Catalina ( Servlet 容器)和 Jasper ( JSP engine )等 当前 Tomcat 的正式版本已经更新到 10.0.x 版本,但当前企业中主流版本为 8.x 和 7.x 官网 : http://tomcat.apache.org/ 官网文档 : https://tomcat.apache.org/tomcat-8.5-doc/index.html 资助文档 : https://cwiki.apache.org/confluence/display/tomcat/ https://cwiki.apache.org/confluence/display/tomcat/FAQ 应用TOMCAT服务器
安装Java环境
[root@tomcat-node1 ~]# yum install java-1.8.0-openjdk.x86_64 -y
安装并启动TOMCAT
[root@tomcat-node1 ~]# tar zxf apache-tomcat-9.0.93.tar.gz -C /usr/local/
[root@tomcat-node1 ~]# ln -s /usr/local/apache-tomcat-9.0.93/ /usr/local/tomcat
[root@tomcat-node1 ~]# cd /usr/local/tomcat/
[root@tomcat-node1 ~]# /usr/local/tomcat/bin/startup.sh
[root@tomcat-node1 ~]# vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/etc/alternatives/jre_openjdk ---添加
[root@tomcat-node1 ~]# useradd -s tomcat.tomcat /usr/local/tomcat
[root@tomcat-node1 ~]# chown -R tomcat.tomcat /usr/local/tomcat/
[root@tomcat-node1 ~]# vim /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
[root@tomcat-node1 ~]# systemctl daemon-reload
[root@tomcat-node1 ~]# systemctl enable --now tomcat
tomcat 负载均衡实现 [root@tomcat-node1 ~]# cp test.jsp /usr/local/tomcat/webapps/ROOT/
172.25.254.100:
[root@tomcat ~]# vim /usr/local/nginx/conf.d/php.conf
upstream tomcat {
ip_hash;
#hash $cookie_JSESSIONID;
server 172.25.254.10:8080;
server 172.25.254.20:8080;
}
server {
listen 80;
server_name www.tomcat.org;
root /usr/local/tomcat/webapps/ROOT/test.jsp;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
}
[root@tomcat ~]# nginx -s reload
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |