企业高性能web服务器

源码安装

官方源码包下载地址:

 https://nginx.org/en/download.html
 #网页搜索https://nginx.org/en/download.html
 #复制链接https://nginx.org/download/nginx-1.24.0.tar.gz
 [root@nginx ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
 [root@Nginx ~]# yum install gcc pcre-devel zlib-devel openssl-devel -y
 [root@Nginx ~]# useradd -s /sbin/nologin -M nginx
 [root@Nginx ~]# tar -zxvf nginx-1.24.0.tar.gz
 [root@Nginx ~]# cd nginx-1.24.0/
 [root@nginx 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 \
 > 
 checking for OS
  + Linux 5.14.0-70.13.1.el9_0.x86_64 x86_64
 checking for C compiler ... not found
 ​
 ./configure: error: C compiler cc is not found
 [root@nginx nginx-1.24.0]# yum install -y gcc
 [root@nginx 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 \
 > 
 ./configure: error: the HTTP rewrite module requires the PCRE library.
 You can either disable the module by using --without-http_rewrite_module
 option, or install the PCRE library into the system, or build the PCRE library
 statically from the source with nginx by using --with-pcre=<path> option.
 [root@nginx nginx-1.24.0]# yum search pcre
 [root@nginx nginx-1.24.0]# yum install -y pcre-devel.x86_64
 [root@nginx 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 \
 > 
 ./configure: error: SSL modules require the OpenSSL library.
 You can either do not enable the modules, or install the OpenSSL library
 into the system, or build the OpenSSL library statically from the source
 with nginx by using --with-openssl=<path> option.
 ​
 [root@nginx 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 \
 > 
 ./configure: error: the HTTP gzip module requires the zlib library.
 You can either disable the module by using --without-http_gzip_module
 option, or install the zlib library into the system, or build the zlib library
 statically from the source with nginx by using --with-zlib=<path> option.
 ​
 [root@nginx nginx-1.24.0]# yum search zlib
 [root@nginx nginx-1.24.0]# yum install -y zlib-devel.x86_64
 ​
 Configuration summary
   + using system PCRE library
     .....
   nginx http scgi temporary files: "scgi_temp"
 ​
 ###成功
 [root@nginx nginx-1.24.0]# make && make install
 [root@nginx nginx-1.24.0]# ls   #多下面俩文件
 Makefile  objs
 [root@nginx nginx-1.24.0]#  vim ~/.bash_profile
 [root@nginx nginx-1.24.0]# source ~/.bash_profile
 export PATH=$PATH:/usr/local/nginx/sbin
 ​
 [root@nginx nginx-1.24.0]# nginx -V
 nginx version: nginx/1.24.0
 built by gcc 11.2.1 20220127 (Red Hat 11.2.1-9) (GCC) 
 built with OpenSSL 3.0.1 14 Dec 2021
 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
 ​
 [root@nginx ~]# nginx               #开启服务
 nginx: [emerg] getpwnam("nginx") failed
 [root@nginx ~]# id nginx
 id: ‘nginx’: no such user
 [root@nginx ~]# useradd nginx
 [root@nginx ~]# id nginx
 uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
 [root@nginx ~]# nginx
 ​
 #查看大小
 [root@nginx nginx-1.24.0]# cd /usr/local/nginx/sbin/
 [root@nginx sbin]# ls
 nginx
 [root@nginx sbin]# du -sh nginx
 1.2M    nginx
 [root@nginx sbin]# nginx -s stop   #关闭服务
 ​
 ​
 --add-module=/root/echo-nginx-module-0.63
 # \cp -f /root/nginx/nginx-1.26.1/objs/nginx  /usr/local/nginx/sbin

2.6.2 平滑升级和回滚案例

 [root@Nginx ~]#tar -zxvf srcache-nginx-module-0.33.tar.gz 
 [root@Nginx ~]#tar -xvf memc-nginx-module-0.20.tar.gz 
 [root@Nginx ~]# tar zxf nginx-1.26.1.tar.gz
 [root@Nginx ~]# cd nginx-1.26.1/
 ​
  #开始编译新版本:比1.24版本多下面这条
 --add-module=/root/echo-nginx-module-0.63
 --add-module=/root/memc-nginx-module-0.20 
 --add-module=/root/srcache-nginx-module-0.33
  [root@Nginx 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  --add-module=/root/echo-nginx-module-0.63  --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33\
 ​
 #只要make无需要make install
 [root@Nginx nginx-1.26.1]# make
 #查看两个版本
 [root@Nginx nginx-1.26.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
 -rwxr-xr-x 1 root root 1239416 Jul 18 15:08 objs/nginx
 -rwxr-xr-x 1 root root 5671488 Jul 18 11:41 /usr/local/nginx/sbin/nginx
 #把之前的旧版的nginx命令备份
 [root@Nginx ~]# cd /usr/local/nginx/sbin/
 [root@Nginx sbin]# cp nginx nginx.24
 ​
 #把新版本的nginx命令复制过去
 #查看现在版本
 nginx -V
 [root@nginx ~]# nginx -V
 nginx version: nginx/1.24.0
 ​
 [root@Nginx sbin]# \cp -f /root/nginx-1.26.1/objs/nginx  /usr/local/nginx/sbin
 [root@nginx ~]# nginx -V
 nginx version: nginx/1.26.1
 ​
 ######################################################################################
 ###新旧版本切换
 [root@nginx core]# cd /usr/local/nginx/sbin/
 [root@nginx sbin]# ls
 nginx  nginx.24
 ​
 #查看现在版本:现在是1.26
 [root@nginx sbin]# curl -I localhost
 HTTP/1.1 200 OK
 Server: nginx/1.26.0
 ......
 Accept-Ranges: bytes
 ​
 [root@nginx sbin]# ls
 nginx  nginx.24
 [root@nginx sbin]# cp  nginx  nginx.26
 [root@nginx sbin]# mv nginx.24  nginx
 [root@nginx sbin]# ls
 nginx  nginx.26
 #文件改变;重启服务
 [root@nginx sbin]# nginx -s stop
 [root@nginx sbin]# nginx
 #现在版本为1.24
 [root@nginx sbin]# curl -I localhost
 HTTP/1.1 200 OK
 Server: nginx/1.24.1
 ...
 Accept-Ranges: bytes
 ​
 #############################################################################
 #######总结:以上暂时没实现没关闭服务实现平滑切换
 #######平滑切换版本#############################
 ######先换文件再操作
 ###1.低到高
 [root@nginx sbin]# curl -I localhost
 HTTP/1.1 200 OK
 Server: nginx/1.24.0
 [root@nginx sbin]# ls
 nginx  nginx.26
 [root@nginx sbin]# cp nginx nginx.24
 [root@nginx sbin]# ls
 nginx  nginx.24  nginx.26
 [root@nginx sbin]# mv nginx.26 nginx
 mv: overwrite 'nginx'? y
 [root@nginx sbin]# ls
 nginx  nginx.24
 [root@nginx sbin]# ps aux | grep nginx
 root       67040  0.0  0.0   9836   928 ?        Ss   16:13   0:00 nginx: master process nginx
 nginx      67041  0.0  0.1  13696  5288 ?        S    16:13   0:00 nginx: worker process
 root       67060  0.0  0.0 221668  2212 pts/0    S+   16:14   0:00 grep --color=auto nginx
 [root@nginx sbin]#  kill -USR2 67040            #第一个master
 [root@nginx sbin]# ps aux | grep nginx      #上一条命令成功标准:多了一个master和worker
 root       67040  0.0  0.0   9836  2736 ?        Ss   16:13   0:00 nginx: master process nginx
 nginx      67041  0.0  0.1  13696  5288 ?        S    16:13   0:00 nginx: worker process
 root       67061  0.0  0.1   9864  5924 ?        S    16:15   0:00 nginx: master process nginx
 nginx      67062  0.0  0.1  13724  4824 ?        S    16:15   0:00 nginx: worker process
 root       67064  0.0  0.0 221668  2208 pts/0    S+   16:15   0:00 grep --color=auto nginx
 [root@nginx sbin]#  kill -WINCH 67040           #第一个master
 [root@nginx sbin]# curl -I localhost
 HTTP/1.1 200 OK
 Server: nginx/1.26.1
 #############################################################################
 ######高到低
 [root@nginx sbin]# ls
 nginx  nginx.24
 [root@nginx sbin]# cp nginx nginx.26
 [root@nginx sbin]# ls
 nginx  nginx.24  nginx.26
 [root@nginx sbin]# mv nginx.24 nginx
 mv: overwrite 'nginx'? y
 [root@nginx sbin]# ls
 nginx  nginx.26
 [root@nginx sbin]# ps aux | grep nginx
 root       67040  0.0  0.0   9836  2736 ?        Ss   16:13   0:00 nginx: master process nginx
 root       67061  0.0  0.1   9864  5924 ?        S    16:15   0:00 nginx: master process nginx
 nginx      67062  0.0  0.1  13724  5348 ?        S    16:15   0:00 nginx: worker process
 root       67083  0.0  0.0 221668  2252 pts/0    S+   16:19   0:00 grep --color=auto nginx
 [root@nginx sbin]# kill -HUP 67040              #第一个master
 [root@nginx sbin]# ps aux | grep nginx
 root       67040  0.0  0.0   9836  2736 ?        Ss   16:13   0:00 nginx: master process nginx
 root       67061  0.0  0.1   9864  5924 ?        S    16:15   0:00 nginx: master process nginx
 nginx      67062  0.0  0.1  13724  5348 ?        S    16:15   0:00 nginx: worker process
 nginx      67084  0.0  0.1  13696  4744 ?        S    16:20   0:00 nginx: worker process
 root       67086  0.0  0.0 221668  2276 pts/0    S+   16:20   0:00 grep --color=auto nginx
 [root@nginx sbin]#  kill -WINCH 67061            #第二个master
 [root@nginx sbin]# ps aux | grep nginx
 root       67040  0.0  0.0   9836  2736 ?        Ss   16:13   0:00 nginx: master process nginx
 root       67061  0.0  0.1   9864  6376 ?        S    16:15   0:00 nginx: master process nginx
 nginx      67084  0.0  0.1  13696  4744 ?        S    16:20   0:00 nginx: worker process
 root       67089  0.0  0.0 221668  2308 pts/0    S+   16:21   0:00 grep --color=auto nginx
 [root@nginx sbin]# curl -I localhost
 HTTP/1.1 200 OK
 Server: nginx/1.24.0

3.4 核心配置示例

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

ngx_http_core_module实现。

3.4.1 新建一个 PC web 站点

#定义子配置文件路径
[root@nginx ~]#  mkdir /usr/local/nginx/conf.d/ 
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
  keepalive_timeout  65;
  include /usr/local/nginx/conf.d/*.conf;			#
[root@nginx ~]# vim  /usr/local/nginx/conf.d/host.conf 
server {
    listen 80;
    server_name www.a.com;

    location / {
        root /var/www/html;
    }
}
[root@nginx conf.d]# vim /var/www/html/index.html
aa
[root@nginx html]# nginx -s reload
[root@nginx html]# vim /etc/hosts
172.25.254.100 www.a.com
[root@nginx html]# curl www.a.com
aa

3.4.2 root alias、

[root@nginx html]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;

    location / {
        root /var/www/html;
    }
    location /a/ {
        alias /var/www/html/;		#完整路径/var/www/html + location后面的 /
        #同时加/和不加 但是访问时a/
    }
}
[root@nginx html]# nginx -s reload
[root@nginx html]# curl www.a.com/a/
aa

3.4.3 location 的详细使用

#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
#对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号

注意:访问文件名要真实存在

3.4.3.1 匹配案例精确匹配(绝对路径)
server {
    listen 80;
    server_name www.a.com;

    location = /index.html {
        root /var/web2;
    }
}
[root@nginx web2]# cd /var/web2/
[root@nginx web2]# echo var/web2 > index.html
[root@nginx web2]# nginx -s reload
[root@nginx web2]# curl www.a.com/index.html
var/web2
[root@nginx web1]# curl www.a.com/index.HTML
404 40x
[root@nginx web1]# curl www.a.com/index.HTml
404 40x
3.4.3.2匹配案例区分大小写
[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;
    location ~ /index.HTML {
        root /var/web2;
    }
}
[root@nginx web1]# echo web2/index > /var/web2/index.html
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# curl www.a.com/index.html
404 40x
[root@nginx web1]# echo HTML > index.HTML
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# curl www.a.com/index.HTML
HTML
3.4.3.3 匹配案例不区分大小写 ~* 绝对路径
[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;

    location ~* /index.HTML { 
        root /var/web2;
    }
}

[root@nginx web1]# echo web2/index > /var/web2/index.html
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# curl www.a.com/index.html
web2/index
##############################################################
#验证
3.4.3.4 匹配案例-URI开始:^~
[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;
    
    location ^~ /web {
        root /var;
    }
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# curl www.a.com/web1/
e
[root@nginx web1]# curl www.a.com/web2/
web2/index
3.4.3.5 匹配案例文件名后缀  ~*

注意:访问文件名要真实存在

[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;

    location ~* \.html$ {
        root /var/web2/;
    }
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# 
[root@nginx web1]# curl www.a.com/index.html
web2/index
[root@nginx web1]# curl www.a.com/index.HTML
HTML
###

3.4.4 Nginx 账户认证功能

htpasswd -cm /usr/local/nginx/.htpasswd admin
htpasswd -m /usr/local/nginx/.htpasswd lee
[root@nginx web1]# cat /usr/local/nginx/.htpasswd
admin:$apr1$CCtmaIxI$p8xaRy5WPiSNLfx6GpgZ01
lee:$apr1$yfR2zIE8$6FRrzr.KRmiY9cQZLxZBd0

[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;


location /lee {
        root /var/web1;
        auth_basic "login ";
        auth_basic_user_file "/usr/local/nginx/.htpasswd";
    }

}
[root@nginx web1]# vim lee > /var/web1/lee/index.html
[root@nginx web1]# nginx -s reload
[root@nginx web1]# curl www.a.com/lee/ -ulee 
Enter host password for user 'lee':
lee
[root@nginx web1]# curl www.a.com/lee/ -uadmin
Enter host password for user 'admin':
lee

3.4.5 自定义错误页面

[root@nginx web1]# echo 404 40x > /var/web1/40x.index.html
[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;

    error_page 404 /40x.html;
    location /40x.html {
        root /var/web1;
    }
}
[root@nginx web1]# nginx -s reload
[root@nginx web1]# curl www.a.com/le		#没有/le目录;跳转至错误页面
404 40x

3.4.6 自定义错误日志


[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;access.log
    access_log /var/logs/access.log;   #生成检测日志;access.log文件自动生成
    error_log /var/logs/error.log;		#生成错误日志;error.log文件自动生成
    
    error_page 404 /40x.html;			#错误访问页面;访问错误页面时出现错误页面
    location /40x.html {
        root /var/web1;
    }

}
[root@nginx logs]# mkdir /var/logs 
[root@nginx web1]# nginx -s reload
[root@nginx web1]# cat /var/logs/access.log
[root@nginx web1]# cat /var/logs/error.log
[root@nginx web1]# ls /var/logs
access.log  error.log
[root@nginx web1]# curl www.a.com/aa
404 40x

[root@nginx web1]# cat /var/logs/error.log
2024/08/16 14:36:01 [error] 68372#0: *10368 open() "/usr/local/nginx/html/aa" failed (2: No such file or directory), client: 172.25.254.100, server: www.a.com, request: "GET /aa HTTP/1.1", host: "www.a.com"

[root@nginx web1]# cat /var/logs/access.log
172.25.254.100 - - [16/Aug/2024:14:36:01 +0800] "GET /aa HTTP/1.1" 404 8 "-" "curl/7.76.1"

3.4.7 检测文件是否存在

try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如

果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一

个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内

部500错误。

try_files $uri $uri.html $uri/index.html /error/default.html;
[root@nginx logs]# mkdir error
[root@nginx logs]# echo error default  > error/default.html
[root@nginx web1]# vim /usr/local/nginx/conf.d/host.conf
[root@nginx web1]#  nginx -s reload
[root@nginx web1]# curl www.a.com
error default

3.4.8 长连接配置

keepalive_requests 数字; #在一次长连接上所允许请求的资源的最大数量
						#默认为100次,建议适当调大,比如:500
keepalive_timeout 65 60;
#开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,
#第二个数字60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间	
[root@nginx conf]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
	...
	keepalive_timeout  65 60;
    keepalive_requests 500;
    }
##################################
#测试
[root@nginx conf]# yum install -y telnet
[root@nginx conf]# telnet www.a.com 80
Trying 172.25.254.100...
Connected to www.a.com.
Escape character is '^]'.
GET / HTTP/1.1			#手动输入这两行回车俩行
HOST: www.a.com

HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 07:02:36 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Fri, 16 Aug 2024 06:48:59 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66bef65b-e"
Accept-Ranges: bytes


error default		#完成一次
GET / HTTP/1.1		###再输入一次
HOST: www.a.com

HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 07:02:47 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Fri, 16 Aug 2024 06:48:59 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66bef65b-e"
Accept-Ranges: bytes

error default
#完成第二次;
#由于keepalive_requests 500;最大可以来500次

3.4.9 作为下载服务器配置

ngx_http_autoindex_module 模块处理以斜杠字符 "/" 结尾的请求,并生成目录列表,可以做为下载服务

配置使用

[root@nginx logs]# mkdir download
[root@nginx ~]# cp nginx-1.24.0.tar.gz /var/logs/download/
server {
    listen 80;
    server_name www.a.com;

    location /download {
        root /var/logs;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        limit_rate 100k;
    }
}
[root@nginx ~]#  nginx -s reload
[root@nginx ~]# wget http://www.a.com/download/nginx-1.24.0.tar.gz		#下载链接
--2024-08-16 15:43:17--  http://www.a.com/download/nginx-1.24.0.tar.gz
Resolving www.a.com (www.a.com)... 172.25.254.100
Connecting to www.a.com (www.a.com)|172.25.254.100|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: ‘nginx-1.24.0.tar.gz.3’

nginx-1.24.0.tar.gz. 100%[====================>]   1.06M   108KB/s    in 10s     

2024-08-16 15:43:27 (108 KB/s) - ‘nginx-1.24.0.tar.gz.3’ saved [1112471/1112471]

[root@nginx ~]# 

Nginx 高级配置

4.1 Nginx 状态页

基于nginx 模块 ngx_http_stub_status_module 实现,

在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module

否则配置完成之后监测会是提示法错误

[root@nginx ~]# vim /usr/local/nginx/conf.d/host.conf
server {
    listen 80;
    server_name www.a.com;
    root  /var/logs/;

    location /status {
        stub_status;
        allow 172.25.254.1;				#本地win主机IP;浏览器可以访问
        allow 172.25.254.100;			#本机IP:curl可以访问
        deny all;						#拒绝所有访问;处理允许访问的;不添加所有可以访问
    }

[root@nginx ~]#  nginx -s reload
[root@nginx ~]# curl www.a.com/status/
Active connections: 1 
server accepts handled requests
 10398 10398 10165 
Reading: 0 Writing: 1 Waiting: 0 
[root@nginx ~]# 
#浏览器访问:www.a.com/status/

4.2 Nginx 压缩功能

Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文

件大小将比源文件显著变小,样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相

应的CPU资源。

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

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
	...
    #gzip  on;
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_vary on;
    gzip_types application/octet-stream;		#添加类型
	}
################################################################################
#测试
[root@nginx logs]# echo aaaaaaaa > a
[root@nginx logs]# ls
a  access.log  download  error  error.log

[root@nginx logs]# curl --head --compressed www.a.com/a    #不压缩;查看大小和类型
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 08:31:14 GMT
Content-Type: application/octet-stream				#类型;添加类型进去
Content-Length: 9									#小于1k
Last-Modified: Fri, 16 Aug 2024 08:30:57 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66bf0e41-9"
Accept-Ranges: bytes

[root@nginx logs]# cat access.log >> a.text
[root@nginx logs]# du -sh a.text
124K	a.text
[root@nginx logs]# curl --head --compressed 172.25.254.100/a.text
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Fri, 16 Aug 2024 08:41:32 GMT
Content-Type: application/octet-stream
Last-Modified: Fri, 16 Aug 2024 08:34:50 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: W/"66bf0f2a-1e843"
Content-Encoding: gzip							#添加类型后压缩了

4.4 Nginx 变量使用

nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用

变量可以分为内置变量和自定义变量

内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值。

4.4.1 内置变量/自定义
#nginx1.26版本有echo
[root@nginx conf.d]# nginx -V
nginx version: nginx/1.26.1
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;

    location /var {
        default_type text/html;
        set $name timinglee;		#自定义变量
		echo $name;					#输出自定义变量
        echo $remote_addr;
        echo $args;
        echo $is_args;
        echo $document_root;
        echo $document_uri;
        echo $host;
        echo $remote_port;
        echo $remote_user;
        echo $request_method;
        echo $request_filename;
        echo $request_uri;
        echo $scheme;
        echo $server_protocol;
        echo $server_addr;
        echo $server_name;
        echo $server_port;
        echo $http_user_agent;
        echo $http_cookie;
        echo $cookie_key2;
    }
}
[root@nginx var]# vim /etc/hosts
[root@nginx var]# mkdir -pv /data/web/html
mkdir: created directory '/data'
mkdir: created directory '/data/web'
mkdir: created directory '/data/web/html'
[root@nginx conf.d]# curl -b "key1=lee,key2=lee1" -u lee:lee var.timinglee.org/var?name=lee&&id=6666
timinglee    #自定义变量
172.25.254.100
name=lee
?
/data/web/html
/var
var.timinglee.org
50626
lee
GET
/data/web/html/var
/var?name=lee
http
HTTP/1.1
172.25.254.100
var.timinglee.org
80
curl/7.76.1
key1=lee,key2=lee1
lee1

Nginx Rewrite 相关功能

Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求

此功能依靠 PCRE(perl compatible regular expression),因此编译之前要安装PCRE库

rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能

比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的

链接,就可以设置为访问

另外还可以在一定程度上提高网站的安全性。

5.1 ngx_http_rewrite_module 模块指令

官方文档: Module ngx_http_rewrite_module

5.1.1 if 指令

官方文档:

 https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if

可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,

[root@nginx conf.d]# vim /usr/local/nginx/conf.d/host.conf 
server {
    listen 80;
    server_name www.a.com;

    location /var {
        index index.html;
        default_type text/html;
        if ( $scheme = http ){
        echo "if ---------> $scheme";
        }
    }

}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl www.a.com/var
if ---------> http
5.1.2 set 指令
set $name lee;
echo $name;
5.1.3 break 指令

注意: 如果break指令在location块中后续指令还会继续执行,只是不执行 ngx_http_rewrite_module

模块的指令,其它指令还会执行

[root@nginx conf.d]# vim /usr/local/nginx/conf.d/host.conf 
server {
    listen 80;
    server_name www.a.com;
    location /break{
        default_type text/html;
        set $name lee;
        echo $name;
        break;
        set $port $server_port;
        echo $port;
    }

[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl www.a.com/break
lee
5.1.4 return 指令

return用于完成对请求的处理,并直接向客户端返回响应状态码,比如:可以指定重定向URL(对于特殊重

定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配

置都将不被执行,return可以在server、if 和 location块进行配置

[root@nginx conf.d]# vim /usr/local/nginx/conf.d/host.conf 
server {
    listen 80;
    server_name www.a.com;

    location /var {
        index index.html;
        default_type text/html;
        if ( $scheme = http ){
        return 200 http://www.a.com/;
        }
    }

}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl www.a.com/var
http://www.a.com/

5.2 rewrite 指令

通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,

rewrite主要是针对用户请求的URL或者是URI做具体处理

5.2.2 rewrite案例: 域名永久与临时重定向

示例: 因业务需要,将访问源域名 www.aa.com的请求永久重定向到www.a.com

[root@nginx conf.d]# vim a.conf 
server {
    listen 80;
    server_name www.a.com;

    location / {
        root /var/logs/var;
        index index.html;
        rewrite / http://www.timinglee.com permanent;
    }
}
[root@nginx conf.d]# cat /var/logs/var/index.html 
var
[root@nginx conf.d]# cat /var/web1/index.html 
e
#测试
#浏览器访问www.a.com
#显示;e
5.2.4 rewrite案例 自动跳转https
[root@nginx conf.d]# cat  vhost.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    ssl_certificate /usr/local/nginx/certs/timinglee.org.crt;
    ssl_certificate_key /usr/local/nginx/certs/timinglee.org.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    location / {	#针对全站跳转
        if ( $scheme = http ){	#如果没有加条件判断,会导致死循环
            rewrite / https://$host redirect;
        }
        if ( !-e $request_filename ){	#文件不存在时
            rewrite /(.*) https://$host/index.html redirect;
        }

    }
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# vim /etc/hosts
172.25.254.100 www.timinglee.org
#C:\Windows\System32\drivers\etc\hosts
172.25.254.100 www.timinglee.org
#浏览器访问:www.timinglee.org跳转为https协议;www.timinglee.org/htl:htm文件不存在;但是任然能访问
#或者
[root@nginx conf.d]# curl -ikL www.timinglee.org
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Mon, 19 Aug 2024 01:24:34 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: https://www.timinglee.org

HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 19 Aug 2024 01:24:34 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Mon, 19 Aug 2024 01:11:23 GMT
Connection: keep-alive
ETag: "66c29bbb-9"
Accept-Ranges: bytes

案例:基于通信安全考虑公司网站要求全站 https,因此要求将在不影响用户请求的情况下将http请求全

部自动跳转至 https,另外也可以实现部分 location 跳转

5.3 Nginx 防盗链

5.3.1 实现盗链

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

实现防盗链功能

none: 				#请求报文首部没有referer首部,
					 #比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
					 
blocked:		 	#请求报文有referer首部,但无有效值,比如为空。

server_names: 		#referer首部中包含本主机名及即nginx 监听的server_name。

arbitrary_string:	#自定义指定字符串,但可使用*作通配符。示例: *.timinglee.org
www.timinglee.*

regular expression:	 #被指定的正则表达式模式匹配到的字符串,要使用~开头,例如:
~.*\.timinglee\.com
#盗链
#新建一个主机172.25.254.101
yum install -y httpd
vim /var/www/html/index.html
<html>

  <head>
    <meta http-equiv=Content-Type content="text/html;charset=utf-8">
    <title>盗链</title>
</head>

  <body>
    <img src="http://www.timinglee.com/lee.png" >
    <h1 style="color:red">欢迎大家</h1>
    <p><a href=http://www.timinglee.com/>狂点老李</a>出门见喜</p>
  </body>

</html>

#盗取另一台主机www.timinglee.com/images/logo.png的图片
[root@nginx conf.d]# vim lee.conf 
[root@nginx conf.d]# cat lee.conf 
server {
    listen 80;
    server_name www.timinglee.com;

    location = /lee.png {
        root /var/logs/images;
    }
    location  / {
        root /var/web1;
    }
}
[root@nginx conf.d]# cat /var/web1/index.html 
e
[root@nginx conf.d]# ll /var/logs/images/lee.png
-rw-r--r-- 1 root root 22174 Aug  3 14:01 /var/logs/images/lee.png
   
#测试:浏览器访问:172.25.254.101:出现
#再点击:“狂点老李”:出现:e

 

5.3.2 实现防盗链

基于访问安全考虑,nginx支持通过ngx_http_referer_module模块,检查访问请求的referer信息是否有效

Nginx 反向代理功能

6.1 实现 http 反向代理

6.1.1.2 实战案例 反向代理单台 web 服务器
[root@nginx conf.d]# vim a.conf 
server {
    listen 80;
    server_name www.aa.com;
    location / {	#反向代理单台 **web** **服务器**
        proxy_pass http://172.25.254.102;
    }
    location ~ /static {	#指定 location 实现反向代理
        proxy_pass http://172.25.254.101:80;
    }
}
###########3反向代理单台web服务器**相关配置###########
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl 172.25.254.102
web20
[root@nginx conf.d]# curl www.aa.com
web20

########指定 location 实现反向代理################
##后端web服务器必须要有相对于的访问URL
[root@web20 ~]# mkdir /var/www/html/static
[root@web20 ~]# echo static 172.25.254.20 > /var/www/html/static/index.html
[root@web20 ~]# systemctl restart httpd
#本机上访问
[root@localhost ~]# curl 172.25.254.102/static/
static 172.25.254.20
[root@nginx conf.d]# curl www.aa.com/static/
web10
6.1.1.4 反向代理示例缓存功能

6.1.1.4.2 准备缓存配置

#测试服务器:IP:172.25.254.100
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.aaa.com;

    location ~ /static {
        proxy_pass http://172.25.254.101:80;
        proxy_cache proxycache;
        proxy_cache_key $request_uri;
        proxy_cache_valid 200 302 301 10m;
        proxy_cache_valid any 1m;
        }
}
#后端服务器IP:172.25.254.101
yum install -y httpd
cd /var/www/local/html
mkdir static 
[root@localhost html]# cat /var/log/messages > log.html
[root@localhost html]# ll
total 52
-rw-r--r--. 1 root root     6 Aug  6 11:51 index.html
-rw-r--r--. 1 root root 47490 Aug 18 18:46 log.html
drwxr-xr-x. 2 root root    24 Aug 18 18:18 static

systemctl restart httpd
#:非缓存场景压测
[root@localhost html]# ab -n1000 -c100 http://www.aaa.com/static/log.html
Requests per second:    1971.38 [#/sec] (mean)

[root@localhost html]# ab -n1000 -c100 http://www.aaa.com/static/index.html
Requests per second:    2186.20 [#/sec] (mean)
nginx -s reload
#测试服务器
[root@nginx conf.d]# tree /usr/local/nginx/proxy_cache/
/usr/local/nginx/proxy_cache/
├── d
│   └── eb
│       └── 74
│           └── a971fce2cfaae636d54b5121d7a74ebd
└── e
    └── 50
        └── 99
            └── 319432ef3663735a9d3cb4e0c1d9950e

6.2 实现 Nginx 四层负载均衡

官方文档:

Module ngx_stream_proxy_module

6.2.2 负载均衡实例: MySQL

后端服务器安装 MySQL

#在apache101中安装mysql
[root@apache101 ~]# yum install mariadb-server -y
[root@apache101 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=20
[root@apache101 ~]# systemctl start mariadb
[root@apache101 ~]# mysql
create user lee@'%' identified by 'lee';
[root@apache101 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"
[root@apache101 ~]# mysql -ulee -plee -h172.25.254.101 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
| 10 |
+-------------+
#在apache30重复以上步骤并在apache102上测试
[root@apache102 ~]# yum install mariadb-server -y
[root@apache102 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=20
[root@apache102 ~]# systemctl start mariadb
[root@apache102 ~]# mysql
create user lee@'%' identified by 'lee';
[root@apache102 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"
[root@apache102 ~]# mysql -ulee -plee -h172.25.254.102 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
| 20 |
+-------------+

nginx配置  

[root@nginx nginx]# mkdir tcp
[root@nginx nginx]# cd tcp/			#tcp是四层;http是七层
[root@nginx tcp]# vim tcp.cof		#注意tcp.conf文件不能与其他http的文件在同一个目录中
stream {
    upstream mysql_server {
        server 172.25.254.101:3306 max_fails=3 fail_timeout=30s;
        server 172.25.254.102:3306 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 172.25.254.100:3306;
        proxy_pass mysql_server;
        proxy_connect_timeout 30s;
        proxy_timeout 300s;
    }
}
[root@nginx tcp]# vim /usr/local/nginx/conf/nginx.conf
include /usr/local/nginx/tcp/*.conf;			#添加这一行;在http外

http {
    include       mime.types;
    default_type  application/octet-stream;
[root@nginx tcp]# yum install -y mariadb   #使有mysql命令

#后端101上测试
[root@web10 conf]# mysql -ulee -plee -h172.25.254.100 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
[root@web10 conf]# mysql -ulee -plee -h172.25.254.100 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
6.2.3 udp 负载均衡实例: DNS

配置后端服务器dns

#后端服务器:172.25.254.101
yum install -y bind
vim /etc/named.conf
listen-on port 53 { any; };
allow-query     { any; };

vim /etc/named.rfc1912.zones
zone "timinglee.org" IN {
        type master;
        file "timinglee.org.zone";
        allow-update { none; };
};
cd /var/named
cp -p named.localhost timinglee.org.zone
vim timinglee.org.zone
$TTL 1D
@       IN SOA  ns.timinglee.org. root.timinglee.org. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      ns.timinglee.org.
ns      A       172.25.254.101    #本机101;另一台写102;其他所有配置一样
www     A       172.25.254.102		#另一台102;另一台写101

[root@web10 named]# systemctl start named
[root@web10 named]# dig ns.timinglee.org @172.25.254.101
。。。
[root@web10 named]# ns.timinglee.org.	86400	IN	A	172.25.254.101
。。。
[root@web10 named]#  dig www.timinglee.org @172.25.254.101
www.timinglee.org.	86400	IN	A	172.25.254.102

#把tcp配置文件清理再实验

[root@nginx nginx]# mkdir udp
[root@nginx nginx]# cd udp
[root@nginx udp]# vim udp.conf 
stream {
    upstream dns_server {
        server 172.25.254.101:53 max_fails=3 fail_timeout=30s;
        server 172.25.254.102:53 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 172.25.254.100:53 udp;
        proxy_pass dns_server;
        proxy_timeout 1s;
        proxy_responses 1;
    }
}
[root@nginx udp]# vim /usr/local/nginx/conf/nginx.conf
include /usr/local/nginx/udp/*.conf; 	#添加这一行;在http外

http {
    include       mime.types;

[root@web10 named]# dig ns.timinglee.org @172.25.254.100
ns.timinglee.org.	86400	IN	A	172.25.254.101				#轮询
[root@web10 named]# dig ns.timinglee.org @172.25.254.100
ns.timinglee.org.	86400	IN	A	172.25.254.102

6.3 实现 FastCGI

6.3.2: Nginx与php-fpm在同一服务器
[root@nginx ~]# yum install -y bzip2 systemd-devel libxml2-devel sqlite-devel libpng-devel libcurl-devel oniguruma-devel
[root@nginx ~]# wget https://mirrors.aliyun.com/rockylinux/9.4/devel/x86_64/kickstart/Packages/o/oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
[root@nginx ~]# yum install -y oniguruma-devel
[root@nginx ~]# cd php-8.3.9/
./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@nginx php-8.3.9]# make && make install 		#大概13分钟

1、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/
[root@Nginx php-8.3.9]# 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@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

2、准备php测试页面   

[root@Nginx ~]# mkdir /data/php -p
[root@centos8 ~]# cat /data/php/index.php #php测试页面
<?php
	phpinfo();
?>

[root@nginx conf.d]# vim  /usr/local/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;			#netstat -antlupe | grep php看到的
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}
#win里面hosts:
172.25.254.100 php.timinglee.org
#重启Nginx并浏览器访问测试
[root@Nginx ~]# nginx -s reload
php.timinglee.org/index.php		#出php页面

4、添加php环境变量

[root@nginx ~]# vim .bash_profile
export PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin:/usr/local/php/bin

[root@nginx ~]# source .bash_profile

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

软件下载:PECL :: 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-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]# 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.3.4 php高速缓存

[root@nginx conf.d]#vim /usr/local/nginx/conf.d/php.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@nginx conf.d]# cat /etc/hosts
172.25.254.100 php.timinglee.org				#一行写一个;多个会出错
172.25.254.100 php.timinglee.com
[root@nginx conf.d]# nginx -s reload
#测试
[root@nginx conf.d]#  ab -n500 -c10 http://php.timinglee.com/example.php
  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值