Apache 起初由美国的伊利诺伊大学香槟分校的国家超级计算机应用中心开发目前经历了两大版本分别是 1.X 和 2.X其可以通过编译安装实现特定的功能
预派生模式,有一个主控制进程,然后生成多个子进程,使用 select 模型,最大并发 1024每个子进程有一个独立的线程响应用户请求相对比较占用内存,但是比较稳定,可以设置最大和最小进程数是最古老的一种模式 , 也是最稳定的模式,适用于访问量不是很大的场景优点:稳定缺点:每个用户请求需要对应开启一个进程 , 占用资源较多,并发性差 , 不适用于高并发场景
一种多进程和多线程混合的模型有一个控制进程,启动多个子进程每个子进程里面包含固定的线程使用线程程来处理请求当线程不够使用的时候会再启动一个新的子进程 , 然后在进程里面再启动线程处理请求,由于其使用了线程处理请求,因此可以承受更高的并发优点:相比 prefork 占用的内存较少,可以同时处理更多的请求缺点:使用 keepalive 的长连接方式,某个线程会一直被占据,即使没有传输数据,也需要一直等待到超时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用(该问题在 prefork模式下,同样会发生)
Apache 中最新的模式, 2012 年发布的 apache 2.4.X 系列正式支持 event 模型 , 属于事件驱动模型 (epoll)每个进程响应多个请求,在现在版本里的已经是稳定可用的模式它和 worker 模式很像,最大的区别在于,它解决了 keepalive 场景下长期被占用的线程的资源浪费问题(某些线程因为被 keepalive ,空挂在哪里等待,中间几乎没有请求过来,甚至等到超时) event MPM 中,会有一个专门的线程来管理这些 keepalive 类型的线程当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放。这样增强了高并发场 景下的请求处理能力优点:单线程响应多请求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理 keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放缺点:没有线程安全控制
Nginx : engine X , 2002 年开发,分为社区版和商业版 (nginx plus )2019 年 3 月 11 日 F5 Networks 6.7 亿美元的价格收购Nginx 是免费的、开源的、高性能的 HTTP 和反向代理服务器、邮件代理服务器、以及 TCP/UDP 代理服务器解决 C10K 问题( 10K Connections )Nginx 官网: http://nginx.orgnginx 的其它的二次发行版:Tengine :由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。 Tengine 的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的 Web 平台。从 2011 年 12 月开始,Tengine成为一个开源项目官网 : http://tengine.taobao.org/OpenResty :基于 Nginx 与 Lua 语言的高性能 Web 平台, 章亦春团队开发,官网: http://openresty.org/cn/
静态的 web 资源服务器 html ,图片, js , css , txt 等静态资源http/https 协议的反向代理结合 FastCGI/uWSGI/SCGI 等协议反向代理动态资源请求tcp/udp 协议的请求转发(反向代理)imap4/pop3 协议的反向代理
模块化设计,较好的扩展性高可靠性支持热部署:不停机更新配置文件,升级版本,更换日志文件低内存消耗: 10000 个 keep-alive 连接模式下的非活动连接,仅需 2.5M 内存event-driven,aio,mmap , sendfile
虚拟主机( server )支持 keep-alive 和管道连接 ( 利用一个连接做多次请求 )访问日志(支持基于日志缓冲提高其性能) url rewirte路径别名基于 IP 及用户的访问控制支持速率限制及并发数限制重新配置和在线升级而无须中断客户的工作进程
Nginx 版本Mainline version 主要开发版本 , 一般为奇数版本号 , 比如 1.19Stable version 当前最新稳定版 , 一般为偶数版本 , 如 :1.20Legacy versions 旧的稳定版 , 一般为偶数版本 , 如 :1.18Nginx 安装可以使用 yum 或源码安装,但是推荐使用源码编译安装yum 的版本比较旧编译安装可以更方便自定义相关路径使用源码编译可以自定义相关功能,更方便业务的上的使用
安装:
[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 sbinconf :保存 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]# nginxroot@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_namemain block :主配置段,即全局配置段,对 http,mail 都有效# 事件驱动相关的配置event {...}#http/https 协议相关配置段http { ...}# 默认配置文件不包括下面两个块#mail 协议相关配置段mail {...}#stream 服务器相关配置段stream {...}
# 全局配置端,对全局生效,主要设置 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 ( 快速拷贝到 kernelsocketbuffer) >> 协议栈。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/
[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 leeAdding password for user nginx[root@Nginx ~]# cat /usr/local/nginx/conf/.htpasswdadmin:$apr1$haGCKgCT$myogggALmqNecTyNupsWQ/lee:$apr1$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.confserver {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:leelogin[root@node100 ~]# curl lee.nginx.org/login/ -u admin:leelogin
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.confserver {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; -------- 限速,默认不限速 }
}
}
[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;
}
[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/
test2break和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,继续寻找路径
[root@centos8 ~]#vim /apps/nginx/conf.d/pc.confserver {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 进行跳转 httpsif ($scheme = http ){ # 如果没有加条件判断,会导致死循环rewrite / https://$host/login redirect;}}}# 重启 Nginx 并访问测试[root@centos7 ~]#curl -ikL www.timinglee.orgHTTP/1.1 302 Moved TemporarilyServer: nginx/1.18.0Date: Thu, 08 Oct 2020 15:23:48 GMTContent-Type: text/htmlContent-Length: 145Connection: keep-aliveLocation: https://www.magedu.orgHTTP/1.1 200 OKServer: nginx/1.18.0Date: Thu, 08 Oct 2020 15:23:48 GMTContent-Type: text/htmlContent-Length: 7Last-Modified: Sat, 26 Sep 2020 01:18:32 GMTConnection: keep-aliveETag: "5f6e96e8-7"Accept-Ranges: bytespc web
[root@centos8 ~]#vim /apps/nginx/conf.d/pc.conflocation / {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 并访问测试
防盗链基于客户端携带的 referer 实现, referer 是记录打开一个页面之前记录是从哪个页面跳转过来的标 记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗 链,referer就是之前的那个网站域名,正常的 referer 信息有以下几种:none : # 请求报文首部没有 referer 首部,# 比如用户直接在浏览器输入域名访问 web 网站,就没有 referer 信息。blocked : # 请求报文有 referer 首部,但无有效值,比如为空。server_names : #referer 首部中包含本主机名及即 nginx 监听的 server_name 。arbitrary_string : # 自定义指定字符串,但可使用 * 作通配符。示例 : *.timinglee.orgwww.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.html172.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 reload172.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.zoneroot@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
[root@Nginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devellibpng-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 管理 cgiphp 相关配置优化[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.timezonedate.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 bythis unit.#ProtectSystem=full # 注释该内容[root@Nginx php-8.3.9]# systemctl start php-fpm.service[root@Nginx php-8.3.9]# netstat -antlupe | grep phptcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 0820758 176202/php-fpm: mas准备 php 测试页面[root@Nginx ~]# mkdir /data/php -p[root@centos8 ~]# cat /data/php/index.php #php 测试页面<?phpphpinfo();?>Nginx 配置转发Nginx 安装完成之后默认生成了与 fastcgi 的相关配置文件,一般保存在 nginx 的安装路径的 conf 目录当中,比如 /apps/nginx/conf/fastcgi.conf 、 /apps/nginx/conf/fastcgi_params 。[root@Nginx ~]# vim /apps/nginx/conf.d/php.confserver {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 functionsif [ -f ~/.bashrc ]; then. ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/bin:/apps/nginx/sbin:/usr/local/php/binexport 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 installInstalling 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-zts-20230831/memcache.so opcache.so
复制测试文件到nginx发布目录中
[root@Nginx ~]# cd memcache-8.2/[root@Nginx memcache-8.2]# lsautom4te.cache config.log configure.ac example.php Makefile.fragmentsREADMEbuild config.m4 config.w32 include Makefile.objects runtests.phpconfig9.m4 config.nice CREDITS libtool memcache.la srcconfig.h config.status docker LICENSE memcache.phptestsconfig.h.in configure Dockerfile Makefile modules[root@Nginx memcache-8.2]# cp example.php memcache.php /data/php/[root@Nginx ~]# vim /data/php/memcache.phpdefine('ADMIN_USERNAME','admin'); // Admin Usernamedefine('ADMIN_PASSWORD','lee'); // Admin Passworddefine('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=zipextension=memcache;zend_extension=opcache[root@Nginx ~]# systemctl reload php-fpm[root@Nginx no-debug-non-zts-20230831]# php -m | grep memmemcache
[root@Nginx ~]# yum install memcached -y[root@Nginx ~]# systemctl enable --now memcached.service[root@Nginx ~]# netstat -antlupe | grep memcachetcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN976 1037243 186762/memcached[root@Nginx ~]# cat /etc/sysconfig/memcachedPORT="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 --withhttp_realip_module --with-http_stub_status_module --with-http_gzip_static_module--with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33[root@Nginx nginx-1.26.1]# make && make install[root@Nginx ~]# vim /apps/nginx/conf.d/php.confupstream 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 来作为 keyset $memc_exptime 300; # 缓存失效时间 300 秒memc_pass memcache;}location ~ \.php$ {set $key $uri$args; # 设定 key 的值srcache_fetch GET /memc $key; # 检测 mem 中是否有要访问的 phpsrcache_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
[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 -vnginx 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 服务器是一个免费的开放源代码的 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
[root@tomcat-node1 ~]# cp test.jsp /usr/local/tomcat/webapps/ROOT/
172.25.254.100:
[root@tomcat ~]# vim /usr/local/nginx/conf.d/php.confupstream 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