企业高性能Web服务器

目录

一、Web服务的基础介绍

1.1 Web 服务介绍

1.2 Apache 经典的 web 服务端

1.2.1 Apache prefork 模型

1.2.2 Apache worker 模型

1.2.3 Apache event模型

1.3 Nginx-高性能的 web 服务端

 二、Nginx 架构

2.1 Nginx 功能介绍

2.2 Web 服务相关的功能

2.3 Nginx 基础特性

2.4 Nginx 模块介绍

三、Nginx 安装 

3.1 Nginx 版本和安装方式

3.2 Nginx 编译安装

四、平滑升级和回滚 

4.1 平滑升级

 4.2  平滑回滚

五、Nginx核心配置 

5.1 新建一个 PC web 站点

5.2  Nginx账户认证功能

5.3 Nginx自定义错误页面

六、Nginx 高级配置 

6.1 Nginx 状态页

6.3 Nginx 压缩功能 

七、Nginx Rewrite 相关功能 

 7.1 rewrite 指令

7.2 全站加密 

7.3 防盗链 

八、Nginx 反向代理功能 

  实现 http 反向代理

九、实现 FastCGI

十、php的动态扩展模块(php的缓存模块) 


一、Web服务的基础介绍

1.1 Web 服务介绍

1.2 Apache 经典的 web 服务端

1.2.1 Apache prefork 模型

  • 预派生模式,有一个主控制进程,然后生成多个子进程,使用select模型,最大并发1024
  • 每个子进程有一个独立的线程响应用户请求
  • 相对比较占用内存,但是比较稳定,可以设置最大和最小进程数
  • 是最古老的一种模式,也是最稳定的模式,适用于访问量不是很大的场景

优缺点:

  • 优点:稳定
  • 缺点:每个用户请求需要对应开启一个进程,占用资源较多,并发性差,不适用于高并发场景

1.2.2 Apache worker 模型

  • 一种多进程和多线程混合的模型
  • 有一个控制进程,启动多个子进程
  • 每个子进程里面包含固定的线程
  • 使用线程程来处理请求
  • 当线程不够使用的时候会再启动一个新的子进程,然后在进程里面再启动线程处理请求
  • 由于其使用了线程处理请求,因此可以承受更高的并发

优缺点:

  • 优点:相比prefork 占用的内存较少,可以同时处理更多的请求
  • 缺点:使用keepalive的长连接方式,某个线程会一直被占据,即使没有传输数据,也需要一直等待到超 时才会被释放。如果过多的线程,被这样占据,也会导致在高并发场景下的无服务线程可用(该问题在 prefork模式下,同样会发生)

1.2.3 Apache event模型

Apache中最新的模式,2012年发布的apache 2.4.X系列正式支持event 模型,属于事件驱动模型(epoll)

  • 每个进程响应多个请求,在现在版本里的已经是稳定可用的模式
  • 它和worker模式很像,最大的区别在于,它解决了keepalive场景下长期被占用的线程的资源浪费问题 (某些线程因为被keepalive,空挂在哪里等待,中间几乎没有请求过来,甚至等到超时)
  • event MPM中,会有一个专门的线程来管理这些keepalive类型的线程
  • 当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放。这样增强了高并发场 景下的请求处理能力

优缺点:

  • 优点:单线程响应多请求,占据更少的内存,高并发下表现更优秀,会有一个专门的线程来管理keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务线程,执行完毕后,又允许它释放
  • 缺点:没有线程安全控制

1.3 Nginx-高性能的 web 服务端

Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发 工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.0

2019年3月11日F5 与 NGINX达成协议,F5 将收购 NGINX 的所有已发行股票,总价值约为 6.7 亿美元。 6.7亿美金约合44.97亿人民币,nginx核心模块代码长度198430(包括空格、注释),所以一行代码约为 2.2万人民币

官网地址 www.nginx.org

Nginx历经十几年的迭代更新(https://nginx.org/en/CHANGES), 目前功能已经非常完善且运行稳定,另外Nginx的版本分为开发版、稳定版和过期版,nginx以功能丰富著称,它即可以作为http服务器,也可以作为反向代理服务器或者邮件服务器能够快速的响应静态网页的请求

支持FastCGI/SSL/Virtual Host/URL Rwrite /Gzip / HTTP Basic Auth/http或者TCP的负载均衡(1.9版本以 上且开启stream模块)等功能,并且支持第三方的功能扩展。

天猫 淘宝 京东 小米 163 新浪等一线互联网公司都在用Nginx或者进行二次开发

 二、Nginx 架构

2.1 Nginx 功能介绍

  • 静态的web资源服务器html,图片,js,css,txt等静态资源
  • http/https协议的反向代理
  • 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
  • tcp/udp协议的请求转发(反向代理)
  • imap4/pop3协议的反向代理

2.2 Web 服务相关的功能

  • 虚拟主机(server)
  • 支持 keep-alive 和管道连接(利用一个连接做多次请求)
  • 访问日志(支持基于日志缓冲提高其性能)
  • url rewirte
  • 路径别名
  • 基于IP及用户的访问控制
  • 支持速率限制及并发数限制
  • 重新配置和在线升级而无须中断客户的工作进程

2.3 Nginx 基础特性

  • 模块化设计,较好的扩展性 高可靠性
  • 支持热部署:不停机更新配置文件,升级版本,更换日志文件
  • 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
  • event-driven,aio,mmap,sendfile

2.4 Nginx 模块介绍

nginx 有多种模块

  • 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件 驱动机制 、进程管理等核心功能
  • 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置等等
  • 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如: Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
  • 邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持 Stream服务模块: 实现反向代理功能,包括TCP协议代理
  • 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支 持等

nginx高度模块化,但其模块早期不支持DSO机制;1.9.11 版本支持动态装载和卸载

三、Nginx 安装 

3.1 Nginx 版本和安装方式

nginx版本

  • Nginx版本 Mainline version 主要开发版本,一般为奇数版本号,比如1.19
  • Stable version 当前最新稳定版,一般为偶数版本,如:1.20
  • Legacy versions 旧的稳定版,一般为偶数版本,如:1.18

nginx安装可以使用yum或源码安装,但是推荐使用源码编译安装

  • yum的版本比较旧
  • 编译安装可以更方便自定义相关路径
  • 使用源码编译可以自定义相关功能,更方便业务的上的使用

3.2 Nginx 编译安装

编译器介绍

源码安装需要提前准备标准的编译器,GCC的全称是(GNU Compiler collection),其有GNU开发,并以 GPL即LGPL许可,是自由的类UNIX即苹果电脑Mac OS X操作系统的标准编译器,因为GCC原本只能处理C语 言,所以原名为GNU C语言编译器,后来得到快速发展,可以处理C++,Fortran,pascal,objective C, java以及Ada等其他语言,此外还需要Automake工具,以完成自动创建Makefile的工作,Nginx的一些模块 需要依赖第三方库,比如: pcre(支持rewrite),zlib(支持gzip模块)和openssl(支持ssl模块) 等。

编译安装 Nginx

官方源码包下载地址:

https://nginx.org/en/download.html

[root@nginx-node1 ~]# ls
anaconda-ks.cfg  Documents  echo-nginx-module-0.63.tar.gz  nginx-1.24.0.tar.gz  Public     Videos
Desktop          Downloads  Music                          Pictures             Templates
[root@nginx-node1 ~]# tar zxf nginx-1.24.0.tar.gz 
[root@nginx-node1 ~]# tar zxf echo-nginx-module-0.63.tar.gz
[root@nginx-node1 ~]# cd nginx-1.24.0/
[root@nginx-node1 nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

[root@nginx-node1 nginx-1.24.0]#  dnf install gcc pcre-devel zlib-devel openssl-devel -y

[root@nginx-node1 nginx-1.24.0]# ./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
  • --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 

 

[root@nginx-node1 nginx-1.24.0]# make && 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 sbin]# useradd -s /sbin/nologin -M nginx
[root@nginx-node1 sbin]# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
[root@nginx-node1 sbin]# ll
total 5956
-rwxr-xr-x 1 root root 6098136 Aug 21 00:13 nginx
[root@nginx-node1 sbin]# ./nginx
[root@nginx-node1 sbin]# ps aux | grep nginx
root       42047  0.0  0.0   9868   940 ?        Ss   00:38   0:00 nginx: master process ./nginx
nginx      42048  0.0  0.2  13740  4852 ?        S    00:38   0:00 nginx: worker process
root       42054  0.0  0.1 221668  2216 pts/1    S+   00:39   0:00 grep --color=auto nginx

[root@nginx-node1 sbin]# netstat -antlupe | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          79628      42047/nginx: master 

 将nginx软件的命令执行路径添加到环境变量中

验证版本 

[root@nginx-node1 sbin]# nginx -V

设置启动文件

[root@nginx-node1 ~]#  vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@nginx-node1 ~]#  systemctl daemon-reload
[root@nginx-node1 ~]#  systemctl start nginx

 

四、平滑升级和回滚 

4.1 平滑升级

平滑升级客户感受不到任何变化

[root@nginx-node1 ~]#  tar zxf echo-nginx-module-0.63.tar.gz 
[root@nginx-node1 ~]# tar zxf nginx-1.26.1.tar.gz
[root@nginx-node1 ~]# cd nginx-1.26.1/
 
#编译目录的时候,把niginx模块也加进去,让其内部可以运行
[root@nginx-node1 nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --add-module=/root/echo-nginx-module-0.63 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module  --add-module=/root/srcache-nginx-module-0.33  --add-module=/root/memc-nginx-module-0.20 
#不要install
[root@nginx-node1 nginx-1.26.1]# make
 
#编译好后生成obj文件夹,里面有nginx
[root@nginx-node1 nginx-1.26.1]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@nginx-node1 nginx-1.26.1]# cd objs/
[root@nginx-node1 objs]# ls
addon         nginx              ngx_auto_headers.h  src
autoconf.err  nginx.8            ngx_modules.c
Makefile      ngx_auto_config.h  ngx_modules.o
 
#进入运行环境
[root@nginx-node1 objs]# cd /usr/local/nginx/sbin/
[root@nginx-node1 sbin]# ll
total 1216
-rwxr-xr-x 1 root root 1242720 Aug 15 21:47 nginx

备份nginx24
[root@nginx-node1 sbin]# cp nginx nginx.24
 
#用26覆盖24
[root@nginx-node1 sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/nginx
[root@nginx-node1 sbin]# ls
nginx  nginx.24
#旧的进程
[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi-daemon: running 
root        9155  0.0  0.0   9860   932 ?        Ss   22:06   0:00 nginx: master process 
nginx       9156  0.0  0.1  13756  5408 ?        S    22:06   0:00 nginx: worker process
root        9193  0.0  0.0 221796  2360 pts/1    S+   22:14   0:00 grep --color=auto nginx
 
 
#回收旧版本
root@nginx-node1 sbin]# kill -USR2 9155
[root@nginx-node1 sbin]# kill -WINCH 9155

 

 4.2  平滑回滚

[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi-daemon: running [nginx-node1.local]
root        9155  0.0  0.0   9860  2568 ?        Ss   22:06   0:00 nginx: master process nginx
root        9195  0.0  0.1   9896  5924 ?        S    22:15   0:00 nginx: master process nginx
nginx       9196  0.0  0.1  13792  5300 ?        S    22:15   0:00 nginx: worker process
root        9219  0.0  0.0 221796  2372 pts/1    S+   22:37   0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# kill -HUP 9155
[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi
root        9155  0.0  0.0   9860  2568 ?        Ss   22:06   0:00 nginx
root        9195  0.0  0.1   9896  5924 ?        S    22:15   0:00 nginx
nginx       9196  0.0  0.1  13792  5300 ?        S    22:15   0:00 nginx
nginx       9225  0.0  0.1  13756  4868 ?        S    22:40   0:00 nginx
root        9227  0.0  0.0 221796  2376 pts/1    S+   22:41   0:00 grep 
[root@nginx-node1 sbin]# kill -WINCH 9195
[root@nginx-node1 sbin]# curl -I 172.25.254.100

把名字修改回去,再删除

[root@nginx-node1 sbin]# ls
nginx  nginx.old
[root@nginx-node1 sbin]# cp nginx nginx.new
[root@nginx-node1 sbin]# ls
nginx  nginx.new  nginx.old
[root@nginx-node1 sbin]# \cp -f nginx.old nginx
[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi-daemon: running [nginx-node1.local]
root        9155  0.0  0.0   9860  2568 ?        Ss   22:06   0:00 nginx: master process nginx
root        9195  0.0  0.1   9896  6424 ?        S    22:15   0:00 nginx: master process nginx
nginx       9225  0.0  0.1  13756  5408 ?        S    22:40   0:00 nginx: worker process
root        9258  0.0  0.0 221796  2372 pts/1    S+   22:45   0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi-daemon: running [nginx-node1.local]
root        9155  0.0  0.0   9860  2568 ?        Ss   22:06   0:00 nginx: master process nginx
root        9195  0.0  0.1   9896  6424 ?        S    22:15   0:00 nginx: master process nginx
nginx       9225  0.0  0.1  13756  5408 ?        S    22:40   0:00 nginx: worker process
root        9260  0.0  0.0 221796  2376 pts/1    S+   22:46   0:00 grep --color=auto nginx
[root@nginx-node1 sbin]# kill -9 9195
[root@nginx-node1 sbin]# ps aux | grep nginx
avahi        882  0.0  0.1  15528  6224 ?        Ss   21:41   0:00 avahi-daemon: running [nginx-node1.local]
root        9155  0.0  0.0   9860  2568 ?        Ss   22:06   0:00 nginx: master process nginx
nginx       9225  0.0  0.1  13756  5408 ?        S    22:40   0:00 nginx: worker process
root        9262  0.0  0.0 221796  2352 pts/1    S+   22:47   0:00 grep --color=auto nginx

五、Nginx核心配置 

基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块 ngx_http_core_module实现。

5.1 新建一个 PC web 站点

[root@nginx-node1 nginx]# vim  /usr/local/nginx/conf/nginx.conf
events {
    worker_connections  10000;
    use epoll;
}
    #gzip  on;  子配置文件路径,写在后面不生效
    include "/usr/local/nginx/conf.d/*.conf";
 
[root@nginx-node1 nginx]# mkdir -p /usr/local/nginx/conf.d
[root@nginx-node1 nginx]# mkdir -p /data/web/html
[root@nginx-node1 nginx]# echo www.example.org > /data/web/html/index.html
 
[root@nginx-node1 nginx]# vim /usr/local/nginx/conf.d/vhost.conf
server{
    listen 80;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
}
 
#重新加载nginx
[root@nginx-node1 nginx]# nginx -s reload
 
#在windows里面做域名解析
172.25.254.100  www.example.org

root 

[root@nginx-node1 nginx]# mkdir /data/web/test1 -p
[root@nginx-node1 nginx]# echo /data/web/test1 > /data/web/test1/index.html
[root@nginx-node1 nginx]# vim /usr/local/nginx/conf.d/vhost.conf
server{
    listen 80;
    server_name www.example.org;
    root /data/web/html;
    index index.html;

    location /test1/ {
        root /data/web;
    }
}
[root@nginx-node1 nginx]# nginx -s reload
#查看日志
[root@nginx-node1 nginx]# tail /usr/local/nginx/logs/error.log

 

alias

输入test2实际访问的是alias里面的地址

server{
    listen 80;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
    location /test1/ {
        root /data/web;
    }
    location /test2 {
        alias /data/web/test1;
    }
}    

 

5.2  Nginx账户认证功能

#第一步:创建认证文件
[root@nginx-node1 ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
#htpasswd隐藏文件,安全  记得结尾加用户
 
#如果只是增加用户,要把-c去掉,否则会覆盖
[root@nginx-node1 ~]# htpasswd -m /usr/local/nginx/.htpasswd lee
New password: 
Re-type new password: 
Adding password for user lee
 
#查看用户
[root@nginx-node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$OWjOtehT$SvrN5paUET5pg.IOyOK030
lee:$apr1$3D6uKBrp$BmJGKK1uWzloILWxnZ8Cq0
 
 
#创建默认配置文件
[root@nginx-node1 ~]# mkdir -p /data/web
[root@nginx-node1 ~]# mkdir /data/web/lee
[root@nginx-node1 ~]# echo lee > /data/web/lee/index.html
 

[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server{
    listen 80;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
 
    location /lee {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file "/usr/local/nginx/.htpasswd";
    }
}
[root@nginx-node1 ~]# nginx -s reload

 

5.3 Nginx自定义错误页面

[root@nginx-node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server{
    listen 80;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
    #404状态码重定向
    error_page 404 /40x.html;
 
    location /lee {
        root /data/web;
        auth_basic "login password";
        auth_basic_user_file "/usr/local/nginx/.htpasswd";
    }
    location = /40x.html {
        root /data/web/errorpage;
    }
}
[root@nginx-node1 ~]# mkdir -p /data/web/errorpage
[root@nginx-node1 ~]# echo errorpage > /data/web/errorpage/40x.html

 

六、Nginx 高级配置 

6.1 Nginx 状态页

  • 基于nginx 模块 ngx_http_stub_status_module 实现
  • 在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module
  • 否则配置完成之后监测会是提示法错误
[root@nginx-node1  ~]# cd /usr/local/nginx/conf.d/
[root@nginx-node1  conf.d]# vim vhost.conf 
[root@nginx-node1  conf.d]# vim status.conf
[root@nginx-node1 conf.d]# nginx -s relod
server {
    listen 80;
    server_name status.example.org;
    root /data/web/html;
    index index.html;
    location /status {
        stub_status;
        auth_basic "login";
        auth_basic_user_file "/usr/local/nginx/.htpasswd";
 
    }
}

6.3 Nginx 压缩功能 

Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相 应的CPU资源。

Nginx对文件的压缩功能是依赖于模块 ngx_http_gzip_module,默认是内置模块

[root@nginx-node1 conf.d]# vim /usr/local/nginx/conf/nginx.conf
	gzip  on;
    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_http_version 1.1;
    gzip_buffers number size;
    gzip_vary on;
	gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
[root@nginx-node1 conf.d]# nginx -s reload
[root@nginx-node1 conf.d]# nginx -t
 
[root@nginx-node1 conf.d]# echo hello example xixi > /data/web/html/small.html
[root@nginx-node1 conf.d]# cat /var/log/example.org/access.log > /data/web/html/big.html
root@nginx-node1 conf.d]# curl --head --compressed 172.25.254.100/small.html

[root@nginx conf.d]# curl --head --compressed 172.25.254.100/big.html

 

七、Nginx Rewrite 相关功能 

  • Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求
  • 此功能依靠 PCRE(perl compatible regular expression),因此编译之前要安装PCRE库
  • rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能
  • 比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的 链接,就可以设置为访问
  • 另外还可以在一定程度上提高网站的安全性

 7.1 rewrite 指令

rewrite 指令,通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配, rewrite主要是针对用户请求的URL或者是URI做具体处理

官方文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

  • rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI
  • 注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成 后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的 标志位用于控制此循环机制
  • 如果替换后的URL是以http://或https://开头,则替换结果会直接以重定向返回给客户端, 即永久重定向 301
[root@nginx-node1 conf.d]# vim vhost.conf 
server {
    listen 80;
    server_name var.example.org;
    root /data/web/html;
    index index.html;
 
    location / {
        root /data/web/var;
        index index.html;
        rewrite / http://www.example.com permanent;     
        #rewrite / http://www.example.com redirect;
    }
}
[root@nginx-node1 conf.d]# mkdir /data/web/var -p
[root@nginx nginx]# curl var.example.org
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

7.2 全站加密 

#创建加密目录
[root@nginx-node1 ~]# cd /usr/local/nginx/
[root@nginx-node1 nginx]# mkdir certs
 
#加密
[root@nginx-node1 ~]# openssl req  -newkey  rsa:2048 -nodes -sha256 -keyout /usr/local/nginx/certs/example.org.key  -x509  -days 365 -out /usr/local/nginx/certs/example.org.crt
 
[root@nginx-node1 certs]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    listen 80;
    listen 443 ssl;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
    #证书目录
    ssl_certificate /usr/local/nginx/certs/example.org.crt;
    #钥匙证书
    ssl_certificate_key /usr/local/nginx/certs/example.org.key;
    #session会话
    ssl_session_cache    shared:SSL:1m;
    #连接时间
    ssl_session_timeout  5m;
 
    location / {
    	#如果协议不是http,就重定向https
        if ( $scheme = http ){
            rewrite / https://$host redirect;
        }
        #如果访问的页面不存在,转到主页面
        if ( !-e $request_filename ){
            rewrite /(.*) https://$host/index.html redirect;
        }
    }
}

7.3 防盗链 

防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标 记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗 链,referer就是之前的那个网站域名

实现盗链

 在一个web 站点盗链另一个站点的资源信息,比如:图片、视频等

[root@nginx-node1 ~]# cd /data/web/html/images
[root@nginx-node1 images]# ls
xixi.jpg

盗链网页 172.25.254.10

[root@webserver1 ~]# cd /var/www/html/
[root@webserver1 html]# vim index.html
<html>
  <head>
    <meta http-equiv=Content-Type content="text/html;charset=utf-8">
    <title>盗链</title>
</head>
  <body>
    <img src="http://www.example.org/images/xixi.jpg" >
    <h1 style="color:red">欢迎大家</h1>
    <p><a href=http://www.example.org>狂点老李</a>出门见喜</p>
  </body>
</html>
[root@webserver1 html]# systemctl restart httpd

防盗链 

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.org;
    root /data/web/html;
    index index.html;
    ssl_certificate /usr/local/nginx/certs/example.org.crt;
    ssl_certificate_key /usr/local/nginx/certs/example.org.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    #允许访问的
    location /images  {
        valid_referers none blocked server_names *.example.org ~/.baidu/.;
        #如果是非法的,就重定向
        if ( $invalid_referer ){
                rewrite ^/   http://www.example.org/haha.jpg;
        }
     }
}

八、Nginx 反向代理功能 

  实现 http 反向代理

  • 将web2的http配置文件中的监听80改成8080.
  • 然后nginx子配置文件添加反向代理

重启后测试: 

 

九、实现 FastCGI

CGI的由来:

最早的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服务器之间传递信息的标准化接口。

 为什么会有FastCGI?

  • CGI协议虽然解决了语言解析器和 Web Server 之间通讯的问题,但是它的效率很低,因为 Web Server 每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候 再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性 能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请 求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率。 

什么是PHP-FPM?

PHP-FPM(FastCGI Process Manager:

  • FastCGI进程管理器)是一个实现了Fastcgi的程序,并且提供进程管理的功能。
  • 进程包括master进程和worker进程。master进程只有一个,负责监听端口,接受来自web server 的请求
  • worker进程一般会有多个,每个进程中会嵌入一个PHP解析器,进行PHP代码的处理。 

编译安装更方便自定义参数或选项,所以推荐大家使用源码编译

官方网站:www.php.net 

源码编译php

#利用yum解决php依赖
[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 \
php相关配置优化
准备php测试页面
--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.timinglee.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测试页面

添加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=$PATH:$HOME/bin:/apps/nginx/sbin:/usr/local/php/bin
export PATH
[root@Nginx ~]# source .bash_profile

十、php的动态扩展模块(php的缓存模块) 

软件下载:http://pecl.php.net/package/memcache

安装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 runtests.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 查看命中效果

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值