一 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
[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
[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
[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
二、nginx的平滑升级
https://nginx.org/download/nginx-1.26.2.tar.gz
[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
回收旧的worker 进程,使用新的。
[root@node1 ~]# while true ; do curl 172.25.250.200;sleep 1; done
#可以在回收前使用死循环检测服务中断没有,发现服务在没有中断的情况下升级了[root@node1 sbin]# kiil -WINCH 15166
三 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下图是我重新升级做了一遍所以端口不一样
四 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
六nginx全局配置参数优化调整
[root@node1 ~]# cd /usr/local/nginx/conf/
[root@node1 conf]# vim nginx.conf
[root@node1 conf]# nginx -s reload
最大链接数
[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
压力测试
[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: 80Document Path: /index.html
Document Length: 615 bytesConcurrency 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] receivedConnection 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: 80Document Path: /index.html
Document Length: 615 bytesConcurrency 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] receivedConnection 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 28Percentage 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: 80Document Path: /index.html
Document Length: 615 bytesConcurrency 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] receivedConnection 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 42Percentage 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)
七location用法
[root@node1 ~]# vim /usr/local/nginx/conf/nginx.conf
[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;
}
[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
windows上的解析
[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
[root@node1 ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@node1 ~]# nginx -t
[root@node1 ~]# nginx -s reload
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
[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
[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
以什么结尾
[root@node1 ~]# echo index.lee > /data/web1/lee/index.html
匹配多个示例
目录测试优先级
[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
账户认证
[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
自定义错误页面
[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/
错误
[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"
检测
[root@node1 bwmis.org]# vim /usr/local/nginx/conf.d/vhost.conf
[root@node1 bwmis.org]# rm -rf /data/web/html/index.html
[root@node1 bwmis.org]# rm -rf /data/web/html/error/
[root@node1 ~]# mkdir /data/web/html/error
[root@node1 ~]# echo error default > /data/web/html/error/default.html
下载检测长链接 的软件
[root@node1 ~]# dnf install telnet -y
[root@node1 ~]# echo www.bwmis.org > /data/web/html/index.html
telnet 手动输入检测链接数量,输入俩次后断开
实际链接保持时间65秒,客户只能看到60秒。
[root@nginx-node1 ~]# vim /usr/local/nginx/conf/nginx.conf
下载服务器
创建下载目录
[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
改格林尼治时间和字节大小。
限速
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 ~]#
错误状态页面
[root@nginx-node1 ~]# cd /usr/local/nginx/conf.d/
下图是windows的页面
压缩功能
[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