企业高性能Web服务器——Nginx

        Nginx是由 1994 年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯 rambler.ru 公司开发的,开发 工作最早从2002 年开始,第一次公开发布时间是 2004 10 4 日,版本号是 0.1.0。
        Nginx历经十几年的迭代更新, 目前功能已经非常完善且运行稳 定,另外 Nginx 的版本分为开发版、稳定版和过期版, nginx 以功能丰富著称,它即可以作为 http 服务 器,也可以作为反向代理服务器或者邮件服务器能够快速的响应静态网页的请求。
功能介绍
  • 静态的web资源服务器html,图片,jscsstxt等静态资源
  • http/https协议的反向代理
  • 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
  • tcp/udp协议的请求转发(反向代理)
  • imap4/pop3协议的反向代理

基础特性

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

Web 服务相关的功能

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

Nginx安装、平滑升级和回滚

主机配置
nmcli c modify ens160 ipv4.address 172.25.254.100/24 ipv4.gateway 172.25.254.2 ipv4.dns 114.114.114.114 ipv4.method manual autoconnect yes
nmcli c up ens160
systemctl disable --now firewalld
cat /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted
reboot
dnf install gcc pcre-devel zlib-devel openssl-devel -y    #做源码安装需要的软件
源码安装Nginx
在https://nginx.org/en/download.html网页中下载需要的版本的Nginx压缩包,此次实验过程中使用的是1.24.0和1.26.1。
将压缩包复制到虚拟机中,并解压进行安装
[root@localhost ~]# tar zxf nginx-1.24.0.tar.gz 
[root@localhost ~]# cd nginx-1.24.0/
[root@localhost nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@localhost nginx-1.24.0]# vim auto/cc/gcc    #将#debug部分的代码注释
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@localhost nginx-1.24.0]# make && make install
[root@localhost nginx-1.24.0]# vim ~/.bash_profile     #把nginx软件的命令执行路陷阱添加到环境变量中
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost nginx-1.24.0]# source ~/.bash_profile    #读入Shell文件并依次执行文件中的所有语句
[root@localhost nginx-1.24.0]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC) 
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

至此Nginx安装完成,如果想要使用systemctl命令控制Nginx服务,可以写以下文件。

[root@localhost ~]# 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@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl start nginx
Nginx平滑升级和回滚

需提前将echo-nginx-module-0.63.tar.gz复制到虚拟机中。

平滑升级过程
[root@localhost ~]# tar zxf echo-nginx-module-0.63.tar.gz 
[root@localhost ~]# tar zxf nginx-1.26.1.tar.gz
[root@localhost ~]# cd nginx-1.26.1/
[root@localhost nginx-1.26.1]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --add-module=/root/echo-nginx-module-0.63 \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module
[root@localhost nginx-1.26.1]# make
[root@localhost nginx-1.26.1]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# mv nginx nginx.24
[root@localhost sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/
[root@localhost sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2052 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       8763  0.0  0.1  14200  4868 ?        S    11:55   0:00 nginx: worker process
root       12204  0.0  0.0 221664  2176 pts/0    S+   12:12   0:00 grep --color=auto nginx
[root@localhost sbin]# kill -USR2 8762
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2180 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       8763  0.0  0.1  14200  4868 ?        S    11:55   0:00 nginx: worker process
root       12205  0.0  0.1   9896  6528 ?        S    12:12   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12206  0.0  0.1  14228  5008 ?        S    12:12   0:00 nginx: worker process
root       12208  0.0  0.0 221664  2176 pts/0    S+   12:12   0:00 grep --color=auto nginx
[root@localhost sbin]# kill -WINCH 8762
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2180 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       12205  0.0  0.1   9896  6528 ?        S    12:12   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12206  0.0  0.1  14228  5008 ?        S    12:12   0:00 nginx: worker process
root       12211  0.0  0.0 221664  2176 pts/0    S+   12:14   0:00 grep --color=auto nginx
[root@localhost sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sun, 18 Aug 2024 16:14:38 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sun, 18 Aug 2024 15:39:44 GMT
Connection: keep-alive
ETag: "66c215c0-267"
Accept-Ranges: bytes
平滑回滚过程
[root@localhost sbin]# mv nginx nginx.26
[root@localhost sbin]# mv nginx.24 nginx
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2180 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       12205  0.0  0.1   9896  6528 ?        S    12:12   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12206  0.0  0.1  14228  5264 ?        S    12:12   0:00 nginx: worker process
root       12228  0.0  0.0 221664  2176 pts/0    S+   12:16   0:00 grep --color=auto nginx
[root@localhost sbin]# kill -HUP 8762
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2180 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       12205  0.0  0.1   9896  6528 ?        S    12:12   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12206  0.0  0.1  14228  5264 ?        S    12:12   0:00 nginx: worker process
nginx      12229  0.0  0.1  14200  4996 ?        S    12:16   0:00 nginx: worker process
root       12231  0.0  0.0 221664  2176 pts/0    S+   12:16   0:00 grep --color=auto nginx
[root@localhost sbin]# kill -WINCH 12205
[root@localhost sbin]# ps aux | grep nginx
root        8762  0.0  0.0   9868  2180 ?        Ss   11:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
root       12205  0.0  0.1   9896  6528 ?        S    12:12   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      12229  0.0  0.1  14200  4996 ?        S    12:16   0:00 nginx: worker process
root       12233  0.0  0.0 221664  2176 pts/0    S+   12:17   0:00 grep --color=auto nginx
[root@localhost sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sun, 18 Aug 2024 16:17:58 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Sun, 18 Aug 2024 15:39:44 GMT
Connection: keep-alive
ETag: "66c215c0-267"
Accept-Ranges: bytes

Nginx高并发配置及子配置文件的使用

修改配置

[root@localhost ~]# echo * - nproc 100000 > /etc/security/limits.conf
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# mkdir -p /usr/local/nginx/conf.d
[root@localhost ~]# nginx -s reload

Nginx服务器的基础使用

编写Nginx子配置文件
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
	listen 80;
	server_name www.qdx.com;
	root /data/web/html;
	index index.html;
	location /test1/ {
		root /data/web;
	}

	location /test2 {
		alias /data/web/test1;
	}
}

创建对应的目录和文件

[root@localhost ~]# mkdir -p /data/web/html
[root@localhost ~]# echo WebHtml > /data/web/html/index.html
[root@localhost ~]# mkdir -p /data/web/test1
[root@localhost ~]# echo This is test1 > /data/web/test1/index.html
[root@localhost ~]# nginx -s reload

测试效果

location匹配符

编写Nginx子配置文件
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;
    index index.html;

    location = /test/index.html {
	root /data/web1;
    }	
 
    location /test {
	root /data/web2;
    }

    location ^~ /t {
	root /data/web3;
    }

   location ~ .(html)$ {
   	root /data/web4;
    }

    location ~* .HTML$ {
	root /data/web5;
    }
}

创建对应的目录和文件

[root@localhost ~]# mkdir -p /data/web{1..5}
[root@localhost ~]# mkdir -p /data/web{1..5}/test
[root@localhost ~]# echo web1 > /data/web1/test/index.html
[root@localhost ~]# echo web2 > /data/web2/test/index.html
[root@localhost ~]# echo web3 > /data/web3/test/index.html
[root@localhost ~]# echo web4 > /data/web4/test/index.html
[root@localhost ~]# echo web5 > /data/web5/test/index.html
[root@localhost ~]# nginx -s reload

一边注释一边进行测试

PS:“=”优先级 > “~”优先级 > “~*”优先级 > 不带符号优先级 > “^~”优先级

Nginx用户认证功能

创建账号密码

[root@localhost ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin qdx
Adding password for user admin
[root@localhost ~]# htpasswd -mb /usr/local/nginx/conf/.htpasswd zsyx qdx
Adding password for user zsyx
[root@localhost ~]# cat /usr/local/nginx/conf/.htpasswd 
admin:$apr1$vImOsiPM$jZOOGgqEg73PLChc4XH7i/
zsyx:$apr1$aoQ3gvHe$VC8FLWp0FbhA4Bjk0YYH80
编写Nginx子配置文件
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;
    index index.html;	
 
    location /qdx {
	root /data/web;
    index index.html;
 	auth_basic "login password !!";
	auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
    } 
}

PS:“auth_basic_user_file”路径必须是存放账号密码的位置,否则访问页面时会报403错误

创建对应的目录和文件

[root@localhost ~]# mkdir /data/web/qdx
[root@localhost ~]# echo qdx > /data/web/qdx/index.html
[root@localhost ~]# nginx -s reload

测试

自定义错误页面和错误日志、检测文件是否存在

配置Nginx子配置文件
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;
    index index.html;	
    error_page 403 /40x.html;
    error_log /var/log/qdx.com/error.log;
    access_log /var/log/qdx.com/access.log;
    try_files $uri $uri.html $uri/index.html /error/default.html;

    location = /40x.html {
	root /data/web/errorpage;
    }
     
}

创建对应的目录和文件

[root@localhost ~]# mkdir -p /data/web/errorpage
[root@localhost ~]# echo error page > /data/web/errorpage/40x.html
[root@localhost ~]# mkdir /var/log/qdx.com
[root@localhost ~]# mkdir /data/web/html/error
[root@localhost ~]# echo error default > /data/web/html/error/default.html
[root@localhost ~]# nginx -s reload

测试

[root@localhost ~]# rm -rf /data/web/html/index.html 
[root@localhost ~]# curl www.qdx.com    #测试本次错误页面时,需要将文件检测先注销
error page
[root@localhost ~]# rm -rf /data/web/errorpage/40x.html 
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl www.qdx.com
error default
[root@localhost ~]# tail -5 /var/log/qdx.com/error.log 
2024/08/20 11:04:42 [error] 14189#0: *96 directory index of "/data/web/html/" is forbidden, client: 172.25.254.100, server: www.qdx.com, request: "GET / HTTP/1.1", host: "www.qdx.com"
2024/08/20 11:04:59 [error] 14194#0: *97 directory index of "/data/web/html/" is forbidden, client: 172.25.254.100, server: www.qdx.com, request: "GET / HTTP/1.1", host: "www.qdx.com"
2024/08/20 11:05:24 [error] 14204#0: *99 directory index of "/data/web/html/" is forbidden, client: 172.25.254.100, server: www.qdx.com, request: "GET / HTTP/1.1", host: "www.qdx.com"
2024/08/20 11:10:51 [error] 14252#0: *100 directory index of "/data/web/html/" is forbidden, client: 172.25.254.100, server: www.qdx.com, request: "GET / HTTP/1.1", host: "www.qdx.com"
2024/08/20 11:11:20 [error] 14252#0: *100 directory index of "/data/web/html/" is forbidden, client: 172.25.254.100, server: www.qdx.com, request: "GET / HTTP/1.1", host: "www.qdx.com"

长连接配置

配置Nginx主配置文件
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf

下载测试软件
[root@localhost ~]# dnf install telnet -y

测试

作为下载服务器配置

生成下载文件,并完成Nginx子配置文件
[root@localhost ~]# mkdir /data/web/download
[root@localhost ~]# dd if=/dev/zero of=/data/web/download/qdxfile bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0369143 s, 2.8 GB/s
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;
    index index.html;	
    
    location /download {
	root /data/web;
	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate 1024k;
    }
     
}

测试

Nginx压缩功能

修改Nginx主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
创建对应的目录和文件
[root@localhost ~]# echo hello qdx > /data/web/html/small.html
[root@localhost ~]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html
[root@localhost ~]# nginx -s reload

测试

[root@localhost ~]# curl --head --compressed 172.25.254.100/big.html
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Tue, 20 Aug 2024 15:37:47 GMT
Content-Type: text/html
Last-Modified: Tue, 20 Aug 2024 15:32:19 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: W/"66c4b703-3fe8"
Content-Encoding: gzip
[root@localhost ~]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Tue, 20 Aug 2024 15:37:59 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 20 Aug 2024 15:31:51 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66c4b6e7-a"
Accept-Ranges: bytes

Nginx变量

更改配置文件
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;	
    
    location /var {
	default_type text/html;
	echo $remote_addr;
	echo $args;
	echo $document_root;
	echo $document_uri;
	echo $host;
	echo $http_user_agent;
	echo $request_filename;
	echo $scheme;
	echo $scheme://$host$document_uri?$args;
	echo $http_cookie;
	echo $cookie_key2;
	echo $http_Accept;
    }
     
}

测试

if判断语句

编辑配置文件

[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.qdx.com;
    root /data/web/html;	
    
    location /test {
	index index.html;
 	default_type text/html;
	if ( $scheme = http ){
	    echo "if ---------> $scheme";
	}
	if ( $scheme = https ){
	    echo "if ---------> $scheme";
	}
    }
}

测试

自动跳转https

编辑配置文件

[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 443 ssl;
    listen 80;
    ssl_certificate /data/certs/ca.crt;
    ssl_certificate_key /data/certs/server.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
    server_name www.qdx.com;
    location / {
	root /data/web;
	index index.html;
	if ( $scheme = http ){
	    rewrite / https://$host redirect;
	}
    }
  
    location /login {
	if ( $scheme = http ){
	    rewrite / https://$host/login redirect;
	}
    }
}

测试

后端多台web服务器

环境准备

一台Nginx服务器,两台Web服务器

部署后端web服务器

Web1
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# echo "Web1 172.25.254.110" > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
[root@localhost ~]# curl http://172.25.254.110
Web1 172.25.254.110
Web2
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# echo "Web2 172.25.254.120" > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
[root@localhost ~]# curl 172.25.254.120
Web2 172.25.254.120
配置Nginx服务器
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf 
upstream webserver {
    server 172.25.254.110:80 weight=1 fail_timeout=15s max_fails=3;
    server 172.25.254.120:80 weight=1 fail_timeout=15s max_fails=3;
    server 172.25.254.100:80 backup;
}
server {
    listen 80;
    server_name www.qdx.com;

    location ~ / {
	proxy_pass http://webserver;
    }
}
[root@localhost ~]# systemctl restart nginx.service 

测试

Nginx与PHP

源码编译安装PHP并优化配置
[root@localhost ~]# yum install bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel
[root@localhost ~]# ./configure \
> --prefix=/usr/local/php \
> --with-config-file-path=/usr/local/php/etc \
> --enable-fpm \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --with-curl \
> --with-iconv \
> --with-mhash \
> --with-zlib \
> --with-openssl \
> --enable-mysqlnd \
> --with-mysqli \
> --with-pdo-mysql \
> --disable-debug \
> --enable-sockets \
> --enable-soap \
> --enable-xml \
> --enable-ftp \
> --enable-gd \
> --enable-exif \
> --enable-mbstring \
> --enable-bcmath \
> --with-fpm-systemd
[root@localhost ~]# cd /usr/local/php/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cat php-fpm.conf
pid = run/php-fpm.pid
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# cd /root/php-8.3.9/
[root@localhost php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@localhost ~]# 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@localhost ~]# cd /root/php-8.3.9/
[root@localhost php-8.3.9]# cp  sapi/fpm/php-fpm.service /lib/systemd/system/
[root@localhost ~]# vim /lib/systemd/system/php-fpm.service
# Mounts the /usr, /boot, and /etc directories read-only for processes invoked by this unit
#ProtectSystem=full
[root@localhost php-8.3.9]# systemctl start php-fpm.service

准备PHP测试页面
[root@localhost ~]# mkdir /data/php -p
[root@localhost ~]# cat /data/php/index.php
<?
phpinfo();
?>
Nginx配置
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.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;
    }
}
[root@localhost ~]# nginx -s reload

测试

PHP动态扩展模块

安装mencache模块
[root@localhost ~]# tar zxf memcache-8.2.tgz
[root@localhost ~]# cd memcache-8.2/
[root@localhost memcache-8.2]# yum install autoconf
[root@localhost memcache-8.2]# phpize
[root@localhost memcache-8.2]# ./configure && make && make install
[root@localhost memcache-8.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
memcache.so opcache.so
复制测试文件到Nginx发布目录中
[root@localhost ~]# cd memcache-8.2/
[root@localhost memcache-8.2]# cp example.php memcache.php /data/php/
[root@localhost ~]# vim /data/php/memcache.php
define('ADMIN_USERNAME','admin'); 
define('ADMIN_PASSWORD','lee'); 
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);
$MEMCACHE_SERVERS[] = 'localhost:11211';
配置PHP加载memcache模块
[root@localhost ~]# vim /usr/local/php/etc/php.ini
;extension=zip
extension=memcache
;zend_extension=opcache
[root@localhost ~]# systemctl reload php-fpm
部署memcached
[root@localhost ~]# yum install memcached -y
[root@localhost ~]# systemctl enable --now memcached.service
[root@localhost ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"
测试

PHP高速缓存

[root@localhost ~]# rm -rf /usr/local/nginx/*
[root@localhost ~]# tar zxf srcache-nginx-module-0.33.tar.gz
[root@localhost ~]# tar zxf memc-nginx-module-0.20.tar.gz
[root@localhost ~]# cd nginx-1.26.1/
[root@localhost nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33
[root@localhost nginx-1.26.1]# make && make install
[root@localhost ~]# cat /usr/local/nginx/conf.d/vhost.conf
upstream memcache {
    server 127.0.0.1:11211;
    keepalive 512;
}
server {
    listen 80;
    server_name php.timinglee.org;
    root /data/php;
    location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string; 
        set $memc_exptime 300; 
        memc_pass memcache;
    }
    location ~ \.php$ {
        set $key $uri$args;
        srcache_fetch GET /memc $key; 
        srcache_store PUT /memc $key; 
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
[root@localhost ~]# systemctl start nginx.service

测试

[root@localhost ~]# ab -n500 -c10 http://php.timinglee.org/index.php
Concurrency Level: 10
Time taken for tests: 0.255 seconds
Complete requests: 500
Failed requests: 0
  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值