您的位置:首页 > 娱乐 > 明星 > nginx

nginx

2024/10/5 20:22:31 来源:https://blog.csdn.net/qq_67442238/article/details/141255186  浏览:    关键词:nginx

 一 nginx源码安装

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

[root@node1 ~]# tar zxf nginx-1.24.0.tar.gz

[root@node1 nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[root@node1 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \   #指定nginx运行用户
> --group=nginx \ #指定nginx运行组
> --with-http_ssl_module \#支持https://
> --with-http_v2_module \ #支持http版本2
> --with-http_realip_module \ #支持ip透传
> --with-http_stub_status_module \#支持状态页面
> --with-http_gzip_static_module \#支持压缩
> --with-pcre \#支持正则
> --with-stream \#支持tcp反向代理
> --with-stream_ssl_module \#支持tcp的ssl加密
> --with-stream_realip_module #支持tcp的透传IP
[root@node1 nginx-1.24.0]# make && make install
[root@node1 nginx-1.24.0]# ls /usr/local/nginx/ #有4个主要的目录
conf  html  logs  sbin

 

conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他 的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params 两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀 去掉即可。

html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web 文件是默认的错误页面提示页面。

logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比 如/var/logs/nginx里面。

sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

[root@node1 nginx-1.24.0]# vim ~/.bash_profile

export PATH=$PATH:/usr/local/nginx/sbin
[root@node1 nginx-1.24.0]# source ~/.bash_profile 

[root@node1 nginx-1.24.0]# nginx -V  #查看版本
[root@node1 ~]# nginx -g "daemon off;"

[root@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
 

cd144849877c416abdb56df7b4edd7f9.png

e88a81f616394189aa07afcef4873f03.png

 

 

[root@node1 ~]# tar zxf nginx-1.24.0.tar.gz 

[root@node1 nginx-1.24.0]# ./configure --help
[root@node1 ~]# ls
anaconda-ks.cfg  nginx-1.24.0  nginx-1.24.0.tar.gz

[root@node1 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@node1 nginx-1.24.0]# dnf install gcc -y

[root@node1 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
statically from the source with nginx by using --with-pcre=<path> option.

[root@node1 nginx-1.24.0]# dnf install pcre-devel.x86_64 -y
[root@node1 nginx-1.24.0]# dnf install openssl-devel.x86_64 -y

[root@node1 nginx-1.24.0]# dnf install zlib-devel.x86_64 -y
[root@node1 nginx-1.24.0]# make -j2 #可能会导致文件溢出

[root@node1 nginx-1.24.0]# make install 
make -f objs/Makefile install
make[1]: Entering directory '/root/nginx-1.24.0'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
    || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' \
    || mv '/usr/local/nginx/sbin/nginx' \
        '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' \
    || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/conf/mime.types' \
    || cp conf/mime.types '/usr/local/nginx/conf'
cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
test -f '/usr/local/nginx/conf/fastcgi_params' \
    || cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params \
    '/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/conf/fastcgi.conf' \
    || cp conf/fastcgi.conf '/usr/local/nginx/conf'
cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/nginx/conf/uwsgi_params' \
    || cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params \
    '/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' \
    || cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params \
    '/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
    || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
    || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/root/nginx-1.24.0'
[root@node1 nginx-1.24.0]# cd /usr//local/nginx/
[root@node1 nginx]# ls
conf  html  logs  sbin
[root@node1 nginx]# cd sbin/
[root@node1 sbin]# cd /usr/local/nginx/
[root@node1 nginx]# 
[root@node1 nginx]# ls
conf  html  logs  sbin
[root@node1 nginx]# cd sbin/
[root@node1 sbin]# ls
nginx
[root@node1 sbin]# /usr/local/nginx/
[root@node1 sbin]# useradd -s /sbin/nologin -M nginx
[root@node1 sbin]# id nginx
uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
[root@node1 sbin]# ll
total 5516
-rwxr-xr-x 1 root root 5646216 Aug 16 11:44 nginx
[root@node1 sbin]# ./nginx
然后就能访问了

[root@node1 sbin]# du -sh nginx
5.4M    nginx

[root@node1 ~]# /usr/local/nginx/sbin/nginx -s stop #关掉
[root@node1 ~]# netstat -antlupe | grep nginx
[root@node1 ~]# /usr/local/nginx/sbin/nginx -s restart 

c906d42ee6c1410a8e91222f31af34a9.png

[root@node1 ~]# cd nginx-1.24.0
[root@node1 nginx-1.24.0]# rm -rf /usr/local/nginx/ 删除
[root@node1 nginx-1.24.0]# make clean
 [root@node1 nginx-1.24.0]# vim auto/cc/gcc

a71b6c5611354122a8542465de48040d.png

823f60ffd5334ae8b5fec8cfd97886ef.png

 8333dc672ed94d2eab8bbe871df9cf90.png

 [root@node1 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 

[root@node1 nginx-1.24.0]# make && make install

[root@node1 nginx-1.24.0]# cd /usr/local/nginx/sbin/^C
[root@node1 nginx-1.24.0]# vim ~/.bash_profile #把nginx软件的命令执行添加到环境变量中
export PATH=$PATH:/usr/local/nginx/sbin
[root@node1 nginx-1.24.0]# source ~/.bash_profile 

6d9ad49c17ce4da4a2c20b0c9a6387cc.png

33e8b6d9c7674615a3b56838df35d5f9.png

二、nginx的平滑升级

 https://nginx.org/download/nginx-1.26.2.tar.gz

40272e9b67b2424c9f5508107733f615.png

[root@node1 ~]# tar zxf nginx-1.26.1.tar.gz 

[root@node1 ~]# tar zxf echo-nginx-module-0.63.tar.gz 

[root@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

#开始编译新版本

[root@node1 nginx-1.26.1]# make #只能使用make否则会覆盖
[root@node1 nginx-1.26.1]# ll objs/nginx   /usr/local/nginx/sbin/nginx #查看俩个版本

[root@node1 nginx-1.26.1]# cd /usr/local/nginx/sbin/
[root@node1 sbin]# cp nginx nginx.old
#把之前的旧版的nginx命令备份

[root@node1 sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin/ #把新版本的nginx命令复制过去

[root@node1 sbin]# nginx -t  #检测一下有没有问题
[root@node1 sbin]# du -sh nginx
[root@node1 sbin]# nginx

 

 ff423cafccc346cdab9b5d7ba623b15c.png

cdf46f024b9146be98dcc379a40a0969.png

e955dafecd3840eda0fda84f619da27e.png 

 回收旧的worker 进程,使用新的。

[root@node1 ~]# while true ; do  curl 172.25.250.200;sleep 1; done
#可以在回收前使用死循环检测服务中断没有,发现服务在没有中断的情况下升级了

[root@node1 sbin]# kiil  -WINCH 15166

 

 

 

9cb7091c8249422aa8c0fb5b872dd7c2.png

 三 nginx的回滚

 [root@node1 sbin]# cp nginx nginx.26

[root@node1 sbin]# ls 
[root@node1 sbin]# mv nginx.old  nginx
[root@node1 sbin]# kill -WINCH 8873 #回收旧版本

[root@node1 sbin]# kill -HUP 8873#重新加载配置而不必重启服务
[root@node1 sbin]# kill -WINCH 14751

下图是我重新升级做了一遍所以端口不一样

 

a164cfffc9dc41eebb59d95f1b9d7525.png

 四 nginx 的参数

[root@node1 ~]# 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

[root@node1 sbin]# nginx -s stop 

[root@nginx-node1 sbin]# nginx -s reload #重载

[root@nginx-node1 sbin]# netstat -antlupe | grep 80

五nginx的 脚本编写

 

[root@node1 ~]# nginx
[root@node1 ~]# cd /usr/local/nginx/logs/
[root@node1 logs]# ll
total 12
-rw-r--r-- 1 root root 340 Aug 17 11:13 access.log
-rw-r--r-- 1 root root 663 Aug 17 11:29 error.log
-rw-r--r-- 1 root root   5 Aug 17 11:06 nginx.pid
[root@node1 logs]# vim /lib/systemd/system/nginx.service 
[root@node1 logs]# systemctl daemon-reload
[root@node1 logs]# nginx -s stop
[root@node1 logs]# ps aux| grep nginx
root       14821  0.0  0.1   6408  2176 pts/0    S+   11:31   0:00 grep --color=auto nginx
[root@node1 logs]# 
[root@node1 logs]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@node1 logs]# ps aux| grep nginx
root       14853  0.0  0.1   9924  2052 ?        Ss   11:32   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      14854  0.0  0.2  14200  4868 ?        S    11:32   0:00 nginx: worker process
root       14856  0.0  0.1   6408  2176 pts/0    S+   11:32   0:00 grep --color=auto nginx
 

[Unit]Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -tExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target

 

499e3f393f1146599c848e10ac0c80da.png

15d3d608d9b147d8bfec5006a1319dfe.png

 六nginx全局配置参数优化调整

[root@node1 ~]# cd /usr/local/nginx/conf/

[root@node1 conf]# vim nginx.conf
[root@node1 conf]# nginx -s reload
 

95ca82855d4f41c29c961656ee25c68c.png

 ef1b6496ec2f4e4d9505380716e03cbf.png

 2bfb6bf689c74647aa3823f2a06726c0.png

 e2fd0a283b7442f3932a539ef44fef64.png

 

 最大链接数

[root@node1 conf]# ulimit -a
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 14340
max locked memory           (kbytes, -l) 8192
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 8192
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 14340
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited
[root@node1 conf]# vim /etc/security/limits.conf 
[root@node1 conf]# sudo -u nginx ul
ul               ulimit           ulockmgr_server  
[root@node1 conf]# sudo -u nginx ulimit  -a
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 14340
max locked memory           (kbytes, -l) 8192
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 8192
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 14340
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited
[root@node1 conf]# vim nginx.conf
[root@node1 conf]# vim /etc/se
security/      selinux/       services       sestatus.conf  
[root@node1 conf]# vim /etc/security/limits.conf 
[root@node1 conf]# sudo -u nginx ulimit  -a
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 14340
max locked memory           (kbytes, -l) 8192
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 8192
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 14340
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited
[root@node1 conf]# dnf install httpd-tools -y

 

5f6020c9a84f4252bf12e0b0afe79947.png

6f3c6f3d20ca44a19c1e0451b4d9b1bc.png

55222f58f8d945a6931a3470ced08a73.png 压力测试

[root@node1 conf]#  ab -n 100 -c 50 http://172.25.250.200/index.html
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.250.200 (be patient).....done


Server Software:        nginx/1.24.0
Server Hostname:        172.25.250.200
Server Port:            80

Document Path:          /index.html
Document Length:        615 bytes

Concurrency Level:      50
Time taken for tests:   0.006 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      84800 bytes
HTML transferred:       61500 bytes
Requests per second:    17714.79 [#/sec] (mean)
Time per request:       2.822 [ms] (mean)
Time per request:       0.056 [ms] (mean, across all concurrent requests)
Transfer rate:          14670.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     0    1   1.0      2       4
Waiting:        0    1   0.9      0       4
Total:          1    2   1.0      3       4
WARNING: The median and mean for the waiting time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      3
  80%      3
  90%      3
  95%      3
  98%      4
  99%      4
 100%      4 (longest request)
[root@node1 conf]# cat /usr/local/nginx/logs/access.log  #查看链接是否OK
127.0.0.1 - - [17/Aug/2024:11:11:00 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.76.1"
127.0.0.1 - - [17/Aug/2024:11:11:50 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.76.1"
127.0.0.1 - - [17/Aug/2024:11:13:19 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.76.1"
127.0.0.1 - - [17/Aug/2024:11:13:22 +0800] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.76.1"
172.25.250.200 - - [17/Aug/2024:11:44:10 +0800] "GET /index.html HTTP/1.0" 200 615 "-" "ApacheBench/2.3"
172.25.250.200 - - [17/Aug/2024:11:44:10 +0800] "GET /index.html HTTP/1.0" 200 615 "-" "ApacheBench/2.3"
172.25.250.200 - - [17/Aug/2024:11:44:10 +0800] "GET /index.html HTTP/1.0" 200 615 "-" "ApacheBench/2.3"

[root@node1 conf]# ab -n 1000 -c 500 http://172.25.250.200/index.html
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.250.200 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.24.0
Server Hostname:        172.25.250.200
Server Port:            80

Document Path:          /index.html
Document Length:        615 bytes

Concurrency Level:      500
Time taken for tests:   0.053 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      848000 bytes
HTML transferred:       615000 bytes
Requests per second:    19021.89 [#/sec] (mean)
Time per request:       26.285 [ms] (mean)
Time per request:       0.053 [ms] (mean, across all concurrent requests)
Transfer rate:          15752.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   10   1.0     10      14
Processing:     2   11   2.8     11      19
Waiting:        0    6   3.2      6      14
Total:          9   20   2.5     20      28

Percentage of the requests served within a certain time (ms)
  50%     20
  66%     21
  75%     21
  80%     23
  90%     24
  95%     24
  98%     28
  99%     28
 100%     28 (longest request)
[root@node1 conf]# ab -n 10000 -c 5000 http://172.25.250.200/index.html
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.250.200 (be patient)
socket: Too many open files (24)
[root@node1 conf]# cd 
[root@node1 ~]# vim /etc/security/limits.conf 
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
[root@node1 ~]# nginx -s reload
[root@node1 ~]# ab -n 10000 -c 500 http://172.25.250.200/index.html
This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.250.200 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.24.0
Server Hostname:        172.25.250.200
Server Port:            80

Document Path:          /index.html
Document Length:        615 bytes

Concurrency Level:      500
Time taken for tests:   0.393 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      8480000 bytes
HTML transferred:       6150000 bytes
Requests per second:    25472.58 [#/sec] (mean)
Time per request:       19.629 [ms] (mean)
Time per request:       0.039 [ms] (mean, across all concurrent requests)
Transfer rate:          21094.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    9   2.9      9      16
Processing:     1   10   3.7     10      35
Waiting:        0    7   3.4      7      27
Total:          6   19   4.8     20      42

Percentage of the requests served within a certain time (ms)
  50%     20
  66%     21
  75%     21
  80%     22
  90%     24
  95%     26
  98%     29
  99%     30
 100%     42 (longest request)
 

 9942455adf4941f38f0785aabfe6ac97.png

 七location用法

[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf

699ae325a24f4e0d8a165690d18e84d1.png

dff62cf966bc4725aa1b51287e81d4f9.png

 [root@node1 ~]# mkdir -p /usr/local/nginx/conf.d

[root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
 server {
         listen 80;
        server_name www.bwmis.org;
        root /data/web/html;
        index index.html;
 }

1f4e956cd69d4ee1942e5fef2f03e77c.png

 [root@node1 ~]# mkdir -p /data/web/html

[root@node1 ~]# echo www.bwmis.org > /data/web/html/index.html
[root@node1 ~]# nginx -t
[root@node1 ~]# nginx -s reload

d40d207844ce43d38093d3f4aef1c723.png 301b98390c56497b801a5041e9446ac3.png

 windows上的解析

5dc1d8e88a614f11bd67a61f93b3c91e.png

 [root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf

[root@node1 ~]# mkdir -p /data/web/test1
[root@node1 ~]# echo /data/web/test1/ > /data/web/test1/index.html

[root@node1 ~]# nginx -t
[root@node1 ~]# nginx -s reload
 

1f22ec4c879941e9be7e87c9320d1bd6.png

1eaad315b5fc4f97b37f54755a64d7b3.png

 [root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf 

[root@node1 ~]# nginx -t

[root@node1 ~]# nginx -s reload

ac228c2368f04a12bffefb976c132e1f.png

 5c79c94ac6e34739978c3c501c36fa08.png

location 

 

[root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf

[root@node1 ~]# mkdir -p /data/web/test 
[root@node1 ~]# echo test page > /data/web/test/index.html
[root@node1 ~]# nginx -t
[root@node1 ~]# nginx -s reload

9c42852a22ab4ad2a425436dd1e59f53.png

c53a796bd6444ddb82d8aed5affeba54.png

 [root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@node1 ~]# mkdir /data/web{1,2}
[root@node1 ~]# mkdir /data/web{1,2}/test
[root@node1 ~]# echo web1 test > /data/web1/test/index.html
[root@node1 ~]# echo web2 test > /data/web2/test/index.html

[root@node1 ~]# nginx -s reload

 1cac45e19eb9424d80d9f70283252cc9.png

5156f1db8ab149ddb3e6813d3d34e4c3.png

8bafb1ec92e549039f228787e79c61f8.png

00a2108cc5204adca95504b700a3312c.png

 [root@node1 ~]# mkdir -p /data/web1/{test1,tee}
[root@node1 ~]# echo test1 > /data/web1/test1/index.html
[root@node1 ~]# echo tee > /data/web1/tee/index.html
[root@node1 ~]# mkdir -p /data/web1/lee
[root@node1 ~]# echo lee > /data/web1/lee/index.html

 c6177aebadd04c24bc821fb0909caa96.png

824be3eab8434db3aa8fc5b91b1fb614.png

6e5936479eb744a6ac08106ceb1c2c92.png

8707c598e65447509b43289e60879e49.png

以什么结尾

 a21e6e3a2d844a3e902819e852735a58.pnge7cb367d5b4d472d877b2dfc8ed19c96.png

dcbf8950c03d411883955b5a1eacf428.png

c2f514dbd2054a1481e904b087d8de77.png

bf2b22eaeda74c878cf25a5271f94291.png

 [root@node1 ~]# echo index.lee > /data/web1/lee/index.html

 9c2b3cb9b8a343a18f10ddea3d03e94d.png

28f4cfc49c2c4dd1802e37a4c39d9629.png

e96ad7f1854042ebba40ab7bd322affe.png

fa8e2c946cba49dbb2b7394cf748d36a.png

 b00302d03ebe47c8a8bd23f7bee53fda.png

deb826c7791f414c9808a28400c5a364.png

4c9cf5d84046494b9f7d606a3d9f93d8.png

匹配多个示例 

641a92efd3a444a4b4ff657ddfc2ffac.png

目录测试优先级

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

 3b0aef10abf4433aaf1b62c224540f07.png

27aea636d01e4993a41239bdb8f1afa4.png

ae91d3a0f8484cd3b6867c3433a92741.png

4c97acd664924cf18200340853ab5776.png

7e0e31b12f6743d4b4f40cea90aa05b6.png

54ffd6a34b2f41c88637e3ea87fd473e.png

f040fdd511ca4178bf77747af41aa420.png

463ebdb00c89436b93325ead8688553b.png

ff6b0429b4d546348ad21c5032ad374f.png

c3cf8b68ac2746c1a6c7817d78225b73.png

 7d318704d79943e9ab42936b5eade6ed.png

账户认证 

[root@node1 ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@node1 ~]#  htpasswd -m /usr/local/nginx/.htpasswd lee
New password: 
Re-type new password: 
Adding password for user lee
[root@node1 ~]# cat /usr/local/nginx/.htpasswd
admin:$apr1$JE7UtZza$lw/oDBLPyIfr9IOFioiWZ.
lee:$apr1$D.Gi1MuB$j5F35K/U95smcWK.RQ10N.

[root@node1 ~]# mkdir /data/web/lee
[root@node1 ~]# echo lee > /data/web/lee/index.html
[root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf 

62876f05bd304155ac7f23e474b2c410.png

 13a0424546d249afbe28efdef6aae5e9.png

 82db45ade5f1497299756cfb91c4c6d1.png

dfbf5201816b4de1a4126845eb2ae352.png

 自定义错误页面

bffabab8ff424632993e9c5b65887c20.png

 [root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf 
[root@node1 ~]# nginx -s reload
[root@node1 ~]# mkdir -p /data/web/errorpage
[root@node1 ~]# echo error page > /data/web/errorpage/40x.html

 

[root@node1 ~]# mkdir /var/log/bwmis.org/

072545d77811496db20131fe37af30ac.png

 错误

[root@node1 ~]# mkdir /var/log/bwmis.org/

[root@node1 ~]# nginx -s reload
root@node1 bwmis.org]# cat error.log 
2024/08/17 15:29:09 [error] 2509#0: *12743 open() "/data/web/html/testa" failed (2: No such file or directory), client: 172.25.250.1, server: www.bwmis.org, request: "GET /testa HTTP/1.1", host: "www.bwmis.org"

[root@node1 bwmis.org]# cat error.log 
2024/08/17 15:29:09 [error] 2509#0: *12743 open() "/data/web/html/testa" failed (2: No such file or directory), client: 172.25.250.1, server: www.bwmis.org, request: "GET /testa HTTP/1.1", host: "www.bwmis.org"
[root@node1 bwmis.org]# cat access.log 
172.25.250.1 - - [17/Aug/2024:15:29:09 +0800] "GET /testa HTTP/1.1" 404 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
172.25.250.1 - - [17/Aug/2024:15:31:09 +0800] "GET /aaa HTTP/1.1" 404 11 "-" "curl/8.8.0"
 

 047d5516e68b4ceab2697c574084103c.png

 检测

[root@node1 bwmis.org]# vim /usr/local/nginx/conf.d/vhost.conf 

c296394d5ab14dd28033906c7a771d25.png c6cecfd207ec4c1682b00a58f3adb15e.png

3b9e1709ec9b4846b0fdea4e708eff80.png

 [root@node1 bwmis.org]# rm -rf /data/web/html/index.html 
[root@node1 bwmis.org]# rm -rf /data/web/html/error/

615ec3c7f64c490ba3ab164dbc4ca0d6.png

[root@node1 ~]# mkdir /data/web/html/error
[root@node1 ~]# echo error default > /data/web/html/error/default.html 

8c1c65f086554fa18359c4b43ca9ebd3.pngff4de720c3694efdbd0080cdda1881da.png

 下载检测长链接 的软件

[root@node1 ~]# dnf install telnet -y

[root@node1 ~]# echo www.bwmis.org > /data/web/html/index.html
 

c73225ab49e0406e92fb9a7a688726e7.png

telnet 手动输入检测链接数量,输入俩次后断开 

177aa6bc40c14f58b21e8751c6cc7ec3.png

实际链接保持时间65秒,客户只能看到60秒。

 [root@nginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf

f708d3905b09446d8600662a9c270bec.png

 下载服务器

创建下载目录

[root@node1 ~]# mkdir /data/web/download
[root@node1 ~]#  dd if=/dev/zero of=/data/web/download/leefile bs=1M 
dd: error writing '/data/web/download/leefile': No space left on device
24879+0 records in
24878+0 records out
26086473728 bytes (26 GB, 24 GiB) copied, 14.2441 s, 1.8 GB/s
 

1b16cbafe92c4f3586cc052532050ca1.png

29d66d805af44e64a9d0eeef3d8cb57f.png

2aad52eca21e44fb830230852193d756.png 改格林尼治时间和字节大小。

 dde16c138d0c4e579d73d8f1763a767e.png

 9355cf3d513d41ae88b983edbecaae60.png

 

 73694f0299d143eeb3d330be19c20e08.png

268689dfee1645b1a79e50ac6f1edef1.png

 

 

c916f34e13b946a296c756e717fa76a7.png

限速 

root@node1 ~]#  vim /usr/local/nginx/conf.d/vhost.conf
[root@node1 ~]# nginx -s reload
[root@node1 ~]# wget http://www.bwmis.org/download/leefile
--2024-08-17 16:33:09--  http://www.bwmis.org/download/leefile
Resolving www.bwmis.org (www.bwmis.org)... 172.25.250.200
Connecting to www.bwmis.org (www.bwmis.org)|172.25.250.200|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: ‘leefile.1’

leefile.1                          100%[===============================================================>] 100.00M  1.05MB/s    in 99s     

2024-08-17 16:34:48 (1.01 MB/s) - ‘leefile.1’ saved [104857600/104857600]

[root@node1 ~]# 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
[root@node1 ~]# 
 

e2e45a7800f14e0c824988afdf23ed32.png

ee92e1c5533245e990764808292d4419.png

 错误状态页面

[root@nginx-node1 ~]# cd /usr/local/nginx/conf.d/

dcbe6fc29acf47968375981a77ab96f8.png

下图是windows的页面 

c13e4ea08d4b4f26994686ea0dffb5f7.png e861457973944898acc5cc00884219e7.png

 65e4468b7a8c42419454efe6a8904460.png

76af67439fa648879c34a1d6f43751d7.png

1d8eb138259f416a8538de474ac5d5fd.png

690f550629fd43ed909440589151abeb.png

d2b89fc8acc940dabb4207ae15ab3610.png

6ef2b0f2f54b40d6894f2413b00f0168.png

 

 压缩功能

[root@node1 ~]#  vim /usr/local/nginx/conf/nginx.conf

[root@node1 ~]# nginx -t

[root@nginx-node1 ~]# nginx -t

[root@nginx-node1 ~]# du -sh /usr/local/nginx/logs/access.log
[root@node1 ~]# # echo hello bwmis > /data/web/html/small.html

9d2fb4c5d8eb4e2394cd3e2f1d8c52e0.png

b5319af3542440cfae79ba51955b6294.png

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com