企业级高性能web服务器-------nginx

nginx源码编译

[root@Nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y
[root@rhel9 ~]# ls
echo-nginx-module-0.63.tar.gz  nginx-1.24.0.tar.gz  vmset.sh
[root@rhel9 ~]# tar zxf echo-nginx-module-0.63.tar.gz 
[root@rhel9 ~]# tar zxf nginx-1.24.0.tar.gz 
[root@rhel9 ~]# cd nginx-1.24.0/
​
[root@rhel9 nginx-1.24.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
​
[root@rhel9 nginx-1.24.0]# vim auto/cc/gcc 
# debug,关闭debug功能
#CFLAGS="$CFLAGS -g"
​
#添加用户
[root@rhel9 nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@rhel9 nginx-1.24.0]#  dnf install gcc pcre-devel zlib-devel openssl-devel -y
​
#进行编译
[root@rhel9 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_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
​
 nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
​
[root@rhel9 nginx-1.24.0]#  make && make install
​
#可以清空之前的编译
[root@rhel9 nginx-1.24.0]# make clean
​
#安装编译完以后
[root@rhel9 ~]# nginx -V#可以看到之前的 ./configure策略
​
[root@rhel9 ~]# du -sh  /usr/local/nginx/sbin/nginx 
1.3M    /usr/local/nginx/sbin/nginx
​
#编译参数
[root@rhel9 ~]# vim ~/.bash_profile
 9 export PATH=$PATH:/usr/local/nginx/sbin
[root@rhel9 ~]# source ~/.bash_profile
[root@rhel9 ~]# nginx 
​

nginx的平滑升级

[root@rhel9 ~]# tar zxf nginx-1.26.1.tar.gz 
[root@rhel9 ~]# cd nginx-1.26.1/
[root@rhel9 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
​
[root@rhel9 nginx-1.26.1]# make
​
#看一下之前的版本
[root@rhel9 sbin]# ps aux | grep nginx 
root       47786  0.0  0.0   9900  2068 ?        Ss   21:26   0:00 nginx: master process nginx
nginx      47787  0.0  0.1  14244  5012 ?        S    21:26   0:00 nginx: worker process
root       47789  0.0  0.0 221680  2304 pts/0    S+   21:26   0:00 grep --color=auto nginx
[root@rhel9 sbin]# curl -I 172.25.254.2
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 19 Aug 2024 13:26:49 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 19 Aug 2024 01:36:22 GMT
Connection: keep-alive
ETag: "66c2a196-267"
Accept-Ranges: bytes
​
[root@rhel9 ~]# cp /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.24
#备份原来的nginx的策略
[root@rhel9 ~]# \cp  -f /root/nginx-1.26.1/objs/nginx  /usr/local/nginx/sbin/
#将新版本的nginx的策略复制
​
#检查一下有没有问题
[root@rhel9 ~]# cd /usr/local/nginx/sbin/
[root@rhel9 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@rhel9 sbin]# kill -USR2  47786
#之前master的id
[root@rhel9 sbin]# ps aux | grep nginx 
root       47786  0.0  0.0   9900  2452 ?        Ss   21:26   0:00 nginx: master process nginx
nginx      47798  0.0  0.1  14244  5268 ?        S    21:29   0:00 nginx: worker process
root       47808  0.0  0.1   9776  6528 ?        S    21:32   0:00 nginx: master process nginx
nginx      47809  0.0  0.1  14240  5008 ?        S    21:32   0:00 nginx: worker process
root       47811  0.0  0.0 221680  2304 pts/0    S+   21:33   0:00 grep --color=auto nginx
[root@rhel9 sbin]# kill -WINCH  47786
#进程回收
[root@rhel9 sbin]# ps aux | grep nginx 
root       47786  0.0  0.0   9900  2452 ?        Ss   21:26   0:00 nginx: master process nginx
root       47808  0.0  0.1   9776  6528 ?        S    21:32   0:00 nginx: master process nginx
nginx      47809  0.0  0.1  14240  5264 ?        S    21:32   0:00 nginx: worker process
root       47817  0.0  0.0 221680  2304 pts/0    S+   21:37   0:00 grep --color=auto nginx
​
[root@rhel9 sbin]# curl -I 172.25.254.2
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 19 Aug 2024 13:35:31 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 19 Aug 2024 01:36:22 GMT
Connection: keep-alive
ETag: "66c2a196-267"
Accept-Ranges: bytes
#版本已经升级欧克
​

版本回退

[root@rhel9 sbin]# ls
nginx.24.old  nginx.26.old
[root@rhel9 sbin]# mv nginx.24.old nginx 
[root@rhel9 sbin]# ls
nginx  nginx.26.old
​
[root@rhel9 sbin]# ps aux | grep nginx 
root       47786  0.0  0.0   9900  2452 ?        Ss   21:26   0:00 nginx: master process nginx
root       47808  0.0  0.1   9776  6528 ?        S    21:32   0:00 nginx: master process nginx
nginx      47809  0.0  0.1  14240  5264 ?        S    21:32   0:00 nginx: worker process
root       47831  0.0  0.0 221680  2304 pts/0    S+   21:40   0:00 grep --color=auto nginx
[root@rhel9 sbin]# curl -I 172.25.254.2
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Mon, 19 Aug 2024 13:40:25 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 19 Aug 2024 01:36:22 GMT
Connection: keep-alive
ETag: "66c2a196-267"
Accept-Ranges: bytes
​
[root@rhel9 sbin]#kill -HUP 47786 
#激活旧master的worker
[root@rhel9 sbin]#kill -WINCH 47808
#回收新master的worker
​
[root@rhel9 sbin]# curl -I 172.25.254.2
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 19 Aug 2024 13:45:00 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 19 Aug 2024 01:36:22 GMT
Connection: keep-alive
ETag: "66c2a196-267"
Accept-Ranges: bytes
​

4.nginx的io调用及优化原理

6.nginx的运行原理

nginx命令的常用参数

nginx  -V #查看详细的版本信息
nginx  -s reload/reopen/stop
nginx  -t #检察语法
​
[root@rhel9 sbin]# vim /usr/local/nginx/conf/nginx.conf
#user  nobody;
#worker_processes  1;
[root@rhel9 sbin]# nginx -s stop 
[root@rhel9 sbin]# nginx -g "worker_processes 3;"
#指定工作的worker数目,前提要注释掉配置文件中的worker,以及停止nginx服务
[root@rhel9 sbin]# ps aux | grep nginx 
root       47905  0.0  0.0   9900  2068 ?        Ss   22:08   0:00 nginx: master process nginx -g worker_processes 3;
nginx      47906  0.0  0.1  14244  5012 ?        S    22:08   0:00 nginx: worker process
nginx      47907  0.0  0.1  14244  5012 ?        S    22:08   0:00 nginx: worker process
nginx      47908  0.0  0.1  14244  5012 ?        S    22:08   0:00 nginx: worker process
root       47910  0.0  0.0 221680  2304 pts/0    S+   22:08   0:00 grep --color=auto nginx

nginx的启动脚本服务编写(使用systemctl)

[root@rhel9 ~]# 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@rhel9 ~]# systemctl daemon-reload
[root@rhel9 ~]# systemctl status nginx 
○ nginx.service - The NGINX HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
     Active: inactive (dead)
​

nginx的高并发设置

[root@rhel9 ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  auto;
worker_cpu_affinity 0001 0010 0100 1000;
​
events {
    worker_connections  100000;#设置单个工作进程的最大并发连接数
    use epoll;#使用epoll事件驱动,
}
[root@rhel9 ~]# nginx -s reload 
​
#重启服务以后,由于我们设定的worker生效,会有很多个worker进程
[root@rhel9 ~]# ps aux | grep nginx 
root       48244  0.0  0.0  10028  3224 ?        Ss   13:45   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      48255  0.0  0.1  14368  5140 ?        S    13:45   0:00 nginx: worker process
nginx      48256  0.0  0.1  14368  4884 ?        S    13:45   0:00 nginx: worker process
nginx      48257  0.0  0.1  14368  5012 ?        S    13:45   0:00 nginx: worker process
nginx      48258  0.0  0.1  14368  5012 ?        S    13:45   0:00 nginx: worker process
root       48260  0.0  0.0 221680  2304 pts/2    S+   13:45   0:00 grep --color=auto nginx
​
#并不支持最大并发链接
[root@rhel9 ~]# ulimit -a
open files                          (-n) 1024
​
#修改pam限制
[root@rhel9 ~]# vim /etc/security/limits.conf 
61 # End of file
62 nginx           -       nofile           10000
[root@rhel9 ~]# sudo -u nginx ulimit -n
10000
​
#压力测试
[root@rhel9 ~]# install httpd-tools -y
[root@rhel9 ~]# ab -n 100  -c 50  http://172.25.254.2/index.html
Concurrency Level:      50
Time taken for tests:   0.009 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      84800 bytes
HTML transferred:       61500 bytes
Requests per second:    11163.21 [#/sec] (mean)
Time per request:       4.479 [ms] (mean)
Time per request:       0.090 [ms] (mean, across all concurrent requests)
Transfer rate:          9244.53 [Kbytes/sec] received

nginx配置中的root和alias

[root@rhel9 ~]# mkdir /usr/local/nginx/conf.d/
[root@rhel9 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
​
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
​
    #access_log  logs/access.log  main;
​
    sendfile        on;
    #tcp_nopush     on;
​
    #keepalive_timeout  0;
    keepalive_timeout  65;
​
    #gzip  on;
    include "/usr/local/nginx/conf.d/*.conf";
    #添加子配置文件的目录路径
    
[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    location   /test1/ {
        root /data/web/;
        }
    location   /test2 {
        alias  /data/web/test1;
        #使用相对路径的时候,location和alias末尾要加/一起加或者都不加
        }
}
​
​
[root@rhel9 ~]# mkdir /data/web/html  -p
[root@rhel9 ~]# echo www.timinglee.org > /data/web/html/index.html
[root@rhel9 ~]# nginx -s reload 
注意做本地解析
​
[root@rhel9 ~]# curl www.timinglee.org
www.timinglee.org
[root@rhel9 ~]# curl www.timinglee.org/test1/
test1
[root@rhel9 ~]# curl www.timinglee.org/test2/
test1
​
​

nginx-location用法详解

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;

ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,

而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最

高的一个uri

uri是用户请求的字符串,即域名后面的web文件路径

然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理

此请求。

优先级顺序: ~* 和~ 大于 / 大于 ^~ / 大于 =

location中的"="

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location  =  wangye {
        root  /data/web1;
        }
​
    location  /wangye {
        root    /data/web2;
        }
​
}
[root@rhel9 data]# echo web1 wangye > /data/web1/wangye/index.html
[root@rhel9 data]# echo web2 wangye > /data/web2/wangye/index.html
[root@rhel9 ~]# curl www.timinglee.org/wangye/
web2 wangye
​
说明=的优先级大于/
​

location中的^~

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    
    location ^~ /w {
        root    /data/web1;
        }
}
[root@rhel9 conf.d]# nginx -s reload
​
[root@rhel9 web1]# tree /data/web1/
/data/web1/
├── w3
│   └── index.html
├── wangye
│   └── index.html
├── wangye1
│   └── index.html
└── wangye2
    └── index.html
​
[root@rhel9 ~]# curl www.timinglee.org/wangye1/
wangye1
[root@rhel9 ~]# curl www.timinglee.org/wangye2/
wangye2
[root@rhel9 ~]# curl www.timinglee.org/wangye/
web1 wangye
[root@rhel9 ~]# curl www.timinglee.org/w3/
w3
​

location中的~*$和~$

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    
    #区分大小写,以html结尾,可以修改为location ~* \.HTML$ ,不区分大小写
    location ~ \.html$ {
        root    /data/web;
        }
}
[root@rhel9 conf.d]# nginx -s reload
​
[root@rhel9 ~]# tree /data/web/
/data/web/
├── html
│   └── index.html
└── test1
    └── index.html
[root@rhel9 ~]# curl www.timinglee.org/test1/index.html
test1
[root@rhel9 ~]# curl www.timinglee.org/html/index.html
www.timinglee.org
​
​
​
​

nginx下的用户认证

[root@rhel9 ~]# htpasswd -cm   /usr/local/nginx/.htpasswd  admin  
htpasswd:用于创建和更新存储用户名和密码的文件的工具。
-c:创建新的 .htpasswd 文件。
-m:使用 MD5 算法加密密码
[root@rhel9 ~]# htpasswd -m   /usr/local/nginx/.htpasswd  admin1
[root@rhel9 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$6jHLMBZV$SSROQ8IyB/Li/dsevRA58/
admin1:$apr1$4t3CHNk4$3dJIzieetDfhHN9ymchM21
​
[root@rhel9 ~]# mkdir /data/web/lee  -p
[root@rhel9 ~]# echo leee > /data/web/lee/index.html
​
[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.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@rhel9 ~]#  -s reload

image-20240820154650048

nginx自定义错误页面

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    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@rhel9 ~]# nginx -s reload 
​
[root@rhel9 ~]# mkdir -p  /data/web/errorpage
[root@rhel9 ~]# echo sorry error  >  /data/web/errorpage/40x.html
​
[root@rhel9 ~]# curl www.timinglee.org/ssh
sorry error

nginx自定义错误日志

#默认日志目录
[root@rhel9 ~]# ll /usr/local/nginx/logs/
总用量 36
-rw-r--r-- 1 root root 17602  8月 20 16:01 access.log
-rw-r--r-- 1 root root  9145  8月 20 16:01 error.log
​
[root@rhel9 ~]# mkdir -p /var/log/timinglee.org/  
[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
​
    error_page  404     /40x.html;
​
    error_log  /var/log/timinglee.org/error.log;
    access_log  /var/log/timinglee.org/access.log;
    #自定义日志目录
​
    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@rhel9 ~]# 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@rhel9 ~]# nginx -s reload 
​
#日志测试
[root@rhel9 ~]# curl www.timinglee.org
sorry error
[root@rhel9 ~]# cat /var/log/timinglee.org/access.log 
172.25.254.1 - - [20/Aug/2024:16:10:19 +0800] "GET /lee HTTP/1.1" 401 179 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
172.25.254.1 - admin [20/Aug/2024:16:10:22 +0800] "GET /lee HTTP/1.1" 401 179 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
172.25.254.1 - admin [20/Aug/2024:16:10:27 +0800] "GET /lee HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
172.25.254.1 - admin [20/Aug/2024:16:10:27 +0800] "GET /lee/ HTTP/1.1" 200 5 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0"
172.25.254.2 - - [20/Aug/2024:16:12:36 +0800] "GET / HTTP/1.1" 404 12 "-" "curl/7.76.1"
​

nginx的文件检测

[root@rhel9 ~]# tree /data
/data
└── web
    ├── errorpage
    │   └── 40x.html
    ├── html
    │   └── error
    │       └── default.html
    └── lee
        └── index.html
​
​
[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
​
    error_page  404     /40x.html;
​
    error_log  /var/log/timinglee.org/error.log;
    access_log  /var/log/timinglee.org/access.log;
​
    try_files $uri $uri.html $uri/index.html /error/default.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@rhel9 ~]# mkdir /data/web/html/error/ -p
[root@rhel9 ~]# echo error default > /data/web/html/error/default.html
​
[root@rhel9 ~]# nginx -s reload 
[root@rhel9 ~]# curl www.timinglee.org
error default
#/data/web/html/index.html被删了所以会自动转去default.html
​
​

nginx的长链接管理

​
[root@rhel9 ~]# vim /usr/local/nginx/conf/nginx.conf
 33     keepalive_timeout  5 3;
        #客户端与服务器之间连接的保持活动(即保持连接打开)的超时时间
 34     keepalive_requests 2;
        #用于设置在一个持久连接上可以发送的最大请求数。当达到这个数量后,Nginx将关闭连接。这个设置有助于防止单个连接占用服务器资源过长时间。
        
[root@rhel9 ~]# nginx -s reload 
[root@rhel9 ~]# dnf install telnet  -y
​
[root@rhel9 ~]# telnet  www.timinglee.org 80
Trying 172.25.254.2...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
​
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 20 Aug 2024 11:57:38 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 20 Aug 2024 11:46:10 GMT
Connection: keep-alive
Keep-Alive: timeout=3
ETag: "66c48202-a"
Accept-Ranges: bytes
​
timinglee
Connection closed by foreign host.
[root@rhel9 ~]# 
​
​

nginx下载服务器的设定和优化

[root@rhel9 ~]# mkdir /data/web/download
[root@rhel9 ~]# dd if=/dev/zero of=/data/web/download/leefile   bs=1M  count=100
#####
dd:是命令本身。
if=/dev/zero:指定输入文件(input file)为 /dev/zero。
of=/data/web/download/leefile:指定输出文件(output file)的路径和文件名
bs=1M: dd 在每次读写操作中会处理 1MB 的数据。
count=100:指定复制的块数量。由于块大小被设置为 1MB,因此总共会复制 100MB 的数据到输出文件。
######
​
[root@rhel9 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
server {
    listen 80;
    server_name www.timinglee.org;
    root /data/web/html;
    index index.html;
    
    location /download {
        root /data/web;
        autoindex on;
        #自动索引功能
        autoindex_exact_size on;
        #计算文件确切大小(单位bytes)
        autoindex_localtime on;
        #on表示显示本机时间而非GMT(格林威治)时间
        limit_rate 9000k;
        #限速,默认不限速
    }
}
[root@rhel9 ~]# nginx -s reload
​
[root@rhel9 ~]# wget   www.timinglee.org/download/leefile
--2024-08-20 20:11:19--  http://www.timinglee.org/download/leefile
正在解析主机 www.timinglee.org (www.timinglee.org)... 172.25.254.2
正在连接 www.timinglee.org (www.timinglee.org)|172.25.254.2|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:104857600 (100M) [application/octet-stream]
正在保存至: “leefile”
​
leefile                100%[============================>] 100.00M  9.16MB/s  用时 11s     
​
2024-08-20 20:11:30 (8.90 MB/s) - 已保存 “leefile” [104857600/104857600])
​

nginx的状态页面

[root@rhel9 ~]# vim   /usr/local/nginx/conf.d/status.conf 
server {
    listen 80;
    server_name status.timinglee.org;
    root /data/web/html;
    index index.html;
​
location /status {
    stub_status;
    # 开启 Nginx 的 stub_status 模块
    auth_basic   "login password";
    auth_basic_user_file "/usr/local/nginx/.htpasswd";
    # 认证文件路径  
    allow 172.25.254.1;
    # 允许特定 IP 访问  
    deny all;
    # 拒绝其他所有 IP 访问  
    }
}
[root@rhel9 ~]# nginx -s reload 
​

image-20240820202753422

nginx的压缩功能

[root@rhel9 ~]# vim /usr/local/nginx/conf/nginx.conf
​
http {
............
...........
​
    gzip  on;
    #启用或禁用gzip压缩,
    gzip_comp_level 5;
    #压缩比
    gzip_min_length 1k;
    #gzip压缩的最小文件,小于设置值的文件将不会压缩
    gzip_http_version 1.1;
    gzip_vary on;
    #启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
    gzip_types text/plain application/javascript application/x-javascript text/css
application/xml text/javascript application/x-httpd-php image/gif image/png;
[root@rhel9 ~]# nginx -s reload 
​
[root@rhel9 ~]# cat /usr/local/nginx/logs/error.log > /data/web/html/big.html
[root@rhel9 ~]# echo hi > /data/web/html/small.html
​
测试
[root@rhel9 ~]# curl --head  --compressed 172.25.254.2/big.html
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 20 Aug 2024 14:16:16 GMT
Content-Type: text/html
Last-Modified: Tue, 20 Aug 2024 14:14:49 GMT
Connection: keep-alive
Keep-Alive: timeout=3
Vary: Accept-Encoding
ETag: W/"66c4a4d9-289a"
Content-Encoding: gzip
#文件被压缩
​
[root@rhel9 ~]# curl --head  --compressed 172.25.254.2/small.html
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Tue, 20 Aug 2024 14:16:24 GMT
Content-Type: text/html
Content-Length: 3
Last-Modified: Tue, 20 Aug 2024 14:15:09 GMT
Connection: keep-alive
Keep-Alive: timeout=3
ETag: "66c4a4ed-3"
Accept-Ranges: bytes
#文件没有被压缩

nginx的变量详解

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/var.conf
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location /var {
    default_type text/html;
    echo $remote_addr;
    #客户端ip
    
    echo $args;
    #例如:https://search.jd.com/Search?keyword=手机&enc=utf-8#返回结果为: keyword=手机&enc=utf-8
   
   echo $is_args;
    #如果有参数为? 否则为空
    
    echo $document_root;
    #访问的文件根目录,/data/web/html
    
    echo $document_uri;
    #location /var,返回/var
    
    echo $host;
    #curl var.timinglee.org/var,返回var.timinglee.org/var
    
    echo $remote_port;
    #客户端请求nginx打开的随机端口
    
    echo $remote_user;
    #curl -u admin:admin,返回admin
    
    echo $request_method;
    #请求资源的方式,GET/PUT/DELETE等
    
    echo $request_filename;
    #当前请求的资源文件的磁盘路径,/data/web/html/var
    
    echo $request_uri;
     #curl var.timinglee.org/var?name=lee&&id=6666,返回/var?name=lee&&id=6666
   
    echo $scheme;
    #请求的协议,例如:http,https,ftp等
    
    echo $server_protocol;
    #保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
    
    echo $server_addr;
    #保存了服务器的IP地址
    
    echo $server_name;
    #服务器的域名
    
    echo $server_port;
    #虚拟主机的端口号
    
    echo $http_user_agent;
    #虚拟主机的端口号
    
    echo $http_cookie;
    #客户端的所有cookie信息
    
    echo $cookie_key2;
    #curl -b "key1=lee,key2=lee1",返回key2的值
    
    set $timinglee lee;
    echo $timinglee;
    #自定义变量
}
}
​
[root@rhel9 ~]# nginx -s reload
#注意域名解析
[root@rhel9 ~]# curl -b "key1=lee,key2=lee1" -u admin:admin var.timinglee.org/var?name=lee&&id=6666
172.25.254.2
name=lee
?
/data/web/html
/var
var.timinglee.org
52416
admin
GET
/data/web/html/var
/var?name=lee
http
HTTP/1.1
172.25.254.2
var.timinglee.org
80
curl/7.76.1
key1=lee,key2=lee1
lee1
lee
​

nginx的Rewrite相关功能

if指令

= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)

[root@rhel9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
​
location /test2 {
if ( !-e $request_filename ){
    echo "$request_filename is not exist";
}
}
}
[root@rhel9 conf.d]# nginx -s reload 
​
#####测试
[root@rhel9 conf.d]# curl var.timinglee.org/test2
/data/web/html/test2 is not exist
​
[root@rhel9 conf.d]# mkdir /data/web/html/test2/ -p
[root@rhel9 conf.d]# echo test2 > /data/web/html/test2/index.html
[root@rhel9 conf.d]# curl var.timinglee.org/test2/
test2
​

break和set指令

[root@rhel9 conf.d]# vim var.conf
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
 location /break {
        default_type text/html;
        if ( $http_user_agent = "firefox" ){
            set $name1 浏览器是firefox中断;
            echo $name1;
            break;
        }
        set $id 浏览器不是firefox;
        echo $id;
    }
}
[root@rhel9 conf.d]# nginx -s reload 
​
[root@rhel9 conf.d]# curl  -A "firefox" var.timinglee.org/break/
浏览器是firefox中断
​
[root@rhel9 conf.d]# curl var.timinglee.org/break/
开始
浏览器不是firefox
​

return指令

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

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

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

[root@rhel9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
​
​
 location /return {
        default_type text/html;
        if ( !-e $request_filename){
        #当前请求的资源文件的磁盘路径,/data/web/html/return
            return 301 http://www.baidu.com;
        }
        echo "$request_filename is exist";
    }
}
​
[root@rhel9 conf.d]# nginx -s reload 
​
​
​
####测试,当当前请求的资源文件的磁盘路径,/data/web/html/return不存在
[root@rhel9 conf.d]# curl -I var.timinglee.org/return
HTTP/1.1 301 Moved Permanently
Server: nginx/1.24.0
Date: Wed, 21 Aug 2024 13:14:46 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout=3
Location: http://www.baidu.com
​
####测试,当前请求的资源文件的磁盘路径,/data/web/html/return存在
[root@rhel9 conf.d]# mkdir  -p /data/web/html/return
[root@rhel9 conf.d]# curl -I var.timinglee.org/return
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Wed, 21 Aug 2024 13:16:00 GMT
Content-Type: text/html
Connection: keep-alive
Keep-Alive: timeout=3
Vary: Accept-Encoding
​

rewrite指令

临时重定向

redirect;

#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端

#由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302

[root@rhel9 ~]# vim /usr/local/nginx/conf.d/var.conf
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
        
  location / {
        root /data/web/var;
        index index.html;
        rewrite / http://www.timinglee.com permanent;
    }
    
}
[root@rhel9 conf.d]# vim var.conf 
​
[root@rhel9 conf.d]# curl -I var.timinglee.org
HTTP/1.1 301 Moved Permanently
#301表示临时重定向
Server: nginx/1.24.0
Date: Wed, 21 Aug 2024 13:27:10 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Keep-Alive: timeout=3
Location: http://www.timinglee.com
#重定向到http://www.timinglee.com网站
​

永久重定向

permanent;

#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端

#由客户端重新发起请求,状态码:30

[root@rhel9 conf.d]# vim var.conf 
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
  location / {
        root /data/web/var;
        index index.html;
        rewrite / http://www.timinglee.com redirect;
    }
​
}
[root@rhel9 conf.d]# nginx -s reload 
​
[root@rhel9 conf.d]# curl -I var.timinglee.org
HTTP/1.1 302 Moved Temporarily
#302表示临时重定向
Server: nginx/1.24.0
Date: Wed, 21 Aug 2024 13:28:54 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Keep-Alive: timeout=3
Location: http://www.timinglee.com
​

break和last

[root@rhel9 conf.d]# mkdir  /data/web/html/{test1,test2,break,last} -p
[root@rhel9 conf.d]# echo test1 > /data/web/html/test1/index.html
[root@rhel9 conf.d]# echo test2 > /data/web/html/test2/index.html
[root@rhel9 conf.d]# echo last > /data/web/html/last/index.html
[root@rhel9 conf.d]# echo break > /data/web/html/break/index.html
​
[root@rhel9 conf.d]# vim  var.conf 
server {
    listen 80;
    server_name var.timinglee.org;
    root /data/web/html;
    index index.html;
​
    location /break {
        root /data/web/html;
        rewrite ^/break/(.*) /test1/$1 break;
        #break就是他会去找/data/web/html/test1/index.html
        rewrite ^/test1/(.*) /test2/$1;
                }
​
    location /last {
        root /data/web/html;
        rewrite ^/last/(.*) /test1/$1 last;
        #last会跳出location,找到下面设定的/test1的location
        rewrite ^/test1/(.*) /test2/$1;
                }
​
    location /test1 {
        default_type text/html;
        return 666 "hahahahahhahhahaahahahahhaahhahah";
                }
​
    location /test2 {
        root /data/web/html;
                }   
}
[root@rhel9 conf.d]# nginx -s reload
​
​
[root@rhel9 conf.d]# curl var.timinglee.org/break/index.html
test1
[root@rhel9 conf.d]# curl var.timinglee.org/last/
hahahahahhahhahaahahahahhaahhahah

nginx-php的源码编译

#下载需要用到的软件,略
#nginx源码编译
[root@nginx ~]# tar zxf memc-nginx-module-0.20.tar.gz
[root@nginx ~]# tar zxf  srcache-nginx-module-0.33.tar.gz
[root@nginx ~]# tar zxf  nginx-1.24.0.tar.gz
[root@nginx ~]# cd nginx-1.24.0/ 
[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33 --user=nginx --group=nginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-pcre
[root@nginx nginx-1.24.0]# make && make install
​
​
#php编译
[root@nginx ~]yum  install -y bzip2 systemd-devel libxml2-devel sqlite-devel -y
[root@nginx ~]yum  install libcurl-devel  -y
[root@nginx ~]dnf install libpng-devel.x86_64 -y
[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 oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm -y
​
[root@nginx ~]# tar zxf php-8.3.9.tar.gz
[root@nginx ~]# cd php-8.3.9/
[root@nginx 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
  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值