编译安装 Nginx
准备rhel9环境
下载安装包nginx-1.24.0(xftp)/复制下载链接 (nginx.org——>download)
解压
[root@nginx nginx-1.24.0]# tar zxf nginx-1.24.0.tar.gz [root@nginx nginx-1.24.0]#tar zxf nginx-1.24.0.tar.gz [root@nginx nginx-1.24.0]# dnf install gcc pcre-devel zlib-devel openssl-devel -y[root@nginx nginx-1.24.0]# useradd -s /sbin/nologin -M nginx
[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 \--with-stream_realip_module
会生成一个Makefile
make clean 可以删掉Makefile 不写!!!!!
重新执行[root@nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx ......才有Makefile
[root@nginx nginx-1.24.0]# make -j2(内核有2个就写2)会导致内核溢出!!!!不写
[root@nginx nginx-1.24.0]# vim auto/cc/gcc
/debug,注释
重新启动:
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 --with-stream_realip_module
[root@nginx ~]# make && make install
[root@nginx ~]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@nginx ~]# source ~/.bash_profile
[root@nginx ~]# du -sh /usr/local/nginx/sbin/nginx
平滑升级和回滚案例
下载最新的安装包
[root@nginx ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz
[root@nginx nginx-1.26.2]# tar zxf echo-nginx-module-0.63.tar.gz
[root@nginx nginx-1.26.2]# tar zxf nginx-1.26.2.tar.gz
[root@nginx nginx-1.26.2]# cd nginx-1.26.2/
多写一句:
[root@nginx nginx-1.26.2]# ./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@nginx nginx-1.26.2]# make
[root@nginx nginx-1.26.2]# cd objs/ [root@nginx objs]# ls
[root@nginx objs]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ls nginx
[root@nginx sbin]# nginx [root@nginx sbin]# curl -I 172.25.254.100
出现这个问题:curl: (7) Failed to connect to 172.25.254.100 port 80: 拒绝连接
防火墙没关;
[root@nginx sbin]# cp nginx nginx.old [root@nginx sbin]# ls nginx nginx.old
[root@nginx sbin]# \cp -f /root/nginx-1.26.2/objs/nginx /usr/local/nginx/sbin/ [root@nginx sbin]# ll
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 936 ? Ss 22:46 0:00 nginx: master process nginx
nginx 14510 0.0 0.1 13724 5516 ? S 22:46 0:00 nginx: worker process
root 14547 0.0 0.0 221680 2392 pts/1 S+ 22:50 0:00 grep --color=auto nginx
[root@nginx sbin]# pidof nginx 14510 14509
[root@nginx sbin]# kill -USR2 14509
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
nginx 14510 0.0 0.1 13724 5516 ? S 22:46 0:00 nginx: worker process
root 14549 0.0 0.1 9872 5888 ? S 22:52 0:00 nginx: master process nginx
nginx 14550 0.0 0.1 13760 4812 ? S 22:52 0:00 nginx: worker process
root 14552 0.0 0.0 221680 2308 pts/1 S+ 22:52 0:00 grep --color=auto nginx
[root@nginx sbin]# kill -WINCH 14509(回收旧版本)
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
root 14549 0.0 0.1 9872 5888 ? S 22:52 0:00 nginx: master process nginx
nginx 14550 0.0 0.1 13760 4812 ? S 22:52 0:00 nginx: worker process
root 14555 0.0 0.0 221680 2384 pts/1 S+ 22:54 0:00 grep --color=auto nginx
[root@nginx sbin]# kill -HUP 14509(激活旧版本)
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
root 14549 0.0 0.1 9872 5888 ? S 22:52 0:00 nginx: master process nginx
nginx 14550 0.0 0.1 13760 4812 ? S 22:52 0:00 nginx: worker process
nginx 14570 0.0 0.1 13724 4868 ? S 22:56 0:00 nginx: worker process
root 14572 0.0 0.0 221680 2436 pts/1 S+ 22:56 0:00 grep --color=auto nginx
[root@nginx sbin]# kill -WINCH 14549(回收旧版本)
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
root 14549 0.0 0.1 9872 6360 ? S 22:52 0:00 nginx: master process nginx
nginx 14570 0.0 0.1 13724 4868 ? S 22:56 0:00 nginx: worker process
root 14574 0.0 0.0 221680 2312 pts/1 S+ 22:59 0:00 grep --color=auto nginx
[root@nginx sbin]# ls nginx nginx.old
[root@nginx sbin]# cp nginx nginx.new
[root@nginx sbin]# ls nginx nginx.new nginx.old
[root@nginx sbin]# \cp -f nginx.old nginx
[root@nginx sbin]# ls nginx nginx.new nginx.old
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
root 14549 0.0 0.1 9872 6360 ? S 22:52 0:00 nginx: master process nginx
nginx 14570 0.0 0.1 13724 5516 ? S 22:56 0:00 nginx: worker process
root 14601 0.0 0.0 221680 2432 pts/1 S+ 23:02 0:00 grep --color=auto nginx
[root@nginx sbin]# kill -9 14549
[root@nginx sbin]# ps aux | grep nginx
root 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginroot 14509 0.0 0.0 9836 2676 ? Ss 22:46 0:00 nginx: master process nginx
nginx 14570 0.0 0.1 13724 5516 ? S 22:56 0:00 nginx: worker process
root 14603 0.0 0.0 221680 2344 pts/1 S+ 23:03 0:00 grep --color=auto nginx
[root@nginx sbin]# nginx -V
nginx version: nginx/1.24.0
[root@nginx sbin]# ls nginx nginx.new nginx.old [root@nginx sbin]# nginx -s stop [root@nginx sbin]# ls nginx nginx.new nginx.old [root@nginx sbin]# rm -fr nginx [root@nginx sbin]# ls nginx.new nginx.old [root@nginx sbin]# mv nginx.new nginx [root@nginx sbin]# ls nginx nginx.old [root@nginx sbin]# nginx(启动服务)
[root@nginx sbin]# nginx -V
nginx version: nginx/1.26.2
[root@nginx sbin]# nginx -t(检查配置文件问题)
nginx服务的启动脚本
[root@nginx sbin]# 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@nginx sbin]# systemctl daemon-reload
[root@nginx sbin]# nginx -s stop
[root@nginx sbin]# ps aux | grep nginx
root 14665 0.0 0.0 221680 2308 pts/1 S+ 23:25 0:00 grep --color=auto nginx
[root@nginx sbin]# systemctl enable --now nginx
[root@nginx sbin]# ps aux | grep nginx
root 14691 0.0 0.0 9872 948 ? Ss 23:26 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 14692 0.0 0.1 13772 4788 ? S 23:26 0:00 nginx: worker process
root 14694 0.0 0.0 221680 2408 pts/1 S+ 23:26 0:00 grep --color=auto nginx
主配置文件:vim /usr/local/nginx/conf/nginx.conf
CPU的核心绑定:
worker_processes 1(auto); #启动工作进程数数量
worker_cpu_affinity 01 10;2核 0001 0010 0100 1000;4核
[root@nginx sbin]# systemctl daemon-reload 重启
#修改pam限制
[root@Nginx ~]# sudo -u nginx ulimit -n
1024
[root@Nginx ~]# vim /etc/security/limits.conf
nginx -(soft/hard) nofile 65535/100000
:wq
[root@Nginx ~]# sudo -u nginx ulimit -a
新建一个 PC web 站点
vim /usr/local/nginx/conf/nginx.conf
events {worker_connections 100000;use epoll;}#gzip on;include "/usr/local/nginx/conf.d/*.conf";(子配置文件)
[root@nginx sbin]# mkdir -p /usr/local/nginx/conf.d
[root@nginx sbin]# vim ~/.vimrc
set ts=4 ai sw=4
:wq
[root@nginx sbin]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /date/web/html;index index.html;
}
[root@nginx sbin]# mkdir -p /date/web/html [root@nginx sbin]# echo www.huihui.org > /date/web/html/index.html
[root@nginx sbin]# nginx -t
[root@nginx sbin]# nginx -s reload
解析:
打开c盘——>Windows——>system32——>drivers——>etc——>hosts——>用记事本打开——>编辑——>保存
[root@nginx sbin]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /date/web/html;index index.html;location /test1 {root /date/web;}
}
[root@nginx sbin]# mkdir /date/web/test1 -p [root@nginx sbin]# echo /date/web/test1 > /date/web/test1/index.html
[root@nginx 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@nginx sbin]# nginx -s reload
[root@nginx sbin]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /date/web/html;index index.html;location /test1 {root /date/web;}location /test2 {(真实别名)alias /date/web/test1;(软链接)}}
[root@nginx 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@nginx sbin]# nginx -s reload
location的用法详解
url:www.huihui.org/(uri)test1/
location后面加: = 、~ 、 ~* 、 ^~
= 精确匹配,大小写 敏感;
^~ 以什么开头,不区分大小写;
~ 路径的正则表达式,区分大小写;
~* 路径的正则表达式,不区分大小写;
\ (. * ?识别不了)转义为普通符号
不带符号 #匹配起始于此uri的所有的uri
eg1:location后面什么都不加的
url:www.huihui.org/(uri)test1/
location后面加: = 、~ 、 ~* 、 ^~
= 精确匹配,大小写 敏感;
^~ 以什么开头,不区分大小写;
~ 路径的正则表达式,区分大小写;
~* 路径的正则表达式,不区分大小写;
\ (. * ?识别不了)转义为普通符号
不带符号 #匹配起始于此uri的所有的uri
eg1:location后面什么都不加的
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;location /test {root /data/web;}}
[root@nginx ~]# mkdir /data/web/test -p
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# echo test page > /data/web/test/index.html
[root@nginx ~]# nginx -s reload
eg1:location后面加=
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;location /test {root /data/web;}location = /test {root /data/web;}}
[root@nginx ~]# echo test page > /data/web/test/index.html
[root@nginx ~]# nginx -s reload
匹配案例-优先级
[root@nginx sbin]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;location = /test {root /data/web2;}location /test {root /date/web1;}location ^~ /t{root /data/web3;}location ~ .html$ {root /data/web4;}location ~* .HTML$ {root /data/web5;}
}
[root@nginx ~]# mkdir /data/web{1..5}/testecho web1 > /data/web1/test/index.htmlecho web2 > /data/web2/test/index.htmlecho web3 > /data/web3/test/index.htmlecho web4 > /data/web4/test/index.htmlecho web5 > /data/web5/test/index.html[root@nginx ~]# nginx -s reload优先级排序:(~* 等于 ~) > 不带符号 > ^~ > =
Nginx用户认证
创建认证文件:
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd hui New password: hui Re-type new password: hui Adding password for user hui
[root@nginx ~]# cat /usr/local/nginx/.htpasswd hui:$apr1$50z25W/R$zGQCFyWmjqHogGxYXQX.m.
如果想再添加一个liu 一定把c去掉,不然会覆盖原来的文件
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd liu New password: liu Re-type new password:liu Adding password for user liu
现在的.htpasswd有两个文件
[root@nginx ~]# cat /usr/local/nginx/.htpasswd hui:$apr1$50z25W/R$zGQCFyWmjqHogGxYXQX.m. liu:$apr1$pfV6eNKz$2INsDl3qPu3zSj9ANmv2O1
how to use?
[root@nginx ~]# mkdir /data/web/hui [root@nginx ~]# echo yhx > /data/web/hui/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;location /hui {root /data/web;auth_basic "login password !!"; #认证ayth_basic_user_file "/usr/local/nginx/.htpasswd"; #认证}
}
[root@nginx ~]# nginx -s reload
自定义错误页面
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;error_page 404 /40x.html;location /hui {root /data/web;auth_basic "login password !!";auth_basic_user_file "/usr/local/nginx/.htpasswd";}location = /40x.html{root /data/web/errorpage;}
}
[root@nginx ~]# mkdir -p /data/web/errorpage
[root@nginx ~]# echo error page > /data/web/errorpage/40x.html
[root@nginx ~]# nginx -s reload
Nginx-自定义日志
[root@nginx ~]# ll /usr/local/nginx/logs/
总用量 24 -rw-r--r-- 1 root root 8542 8月 17 12:52 access.log -rw-r--r-- 1 root root 6777 8月 17 12:52 error.log -rw-r--r-- 1 root root 4 8月 17 09:52 nginx.pid
[root@nginx ~]# mkdir /var/log/huihui.org [root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/huihui.org/error.log; #自定义日志access_log /var/log/huihui.org/access.log; #自定义日志location /hui {root /data/web;auth_basic "login password !!";auth_basic_user_file "/usr/local/nginx/.htpasswd";}location = /40x.html{root /data/web/errorpage;}
}
[root@nginx ~]# nginx -s reload
访问:
[C:~]$ curl www.huihui.org % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15 100 15 0 0 987 0 --:--:-- --:--:-- --:--:-- 2142 www.huihui.org
[root@nginx ~]# cat /var/log/huihui.org/access.log 172.25.254.1 - - [17/Aug/2024:13:05:51 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/8.4.0"
Nginx-检测文件是否存在
如果文件不存在就转到default.html页面
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/huihui.org/error.log;access_log /var/log/huihui.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html; #检测有没有文件夹location /hui {root /data/web;auth_basic "login password !!";auth_basic_user_file "/usr/local/nginx/.htpasswd";}location = /40x.html{root /data/web/errorpage;}
}
[root@nginx ~]# nginx -s reload [root@nginx ~]# mkdir /data/web/html/error [root@nginx ~]# echo error default > /data/web/html/error/default.html
[root@nginx ~]# nginx -s reload
把文件夹都删了,curl 一下
Nginx中的长链接管理
在主配置文件中配置的
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#keepalive_timeout 0;keepalive_timeout 65 60; #回客户端的会话保持时间为65skeepalive_request 100; #长连接上所允许请求的资源的最大数量
下载测试工具:
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# curl -v www.huihui.org
* Trying 216.40.34.37:80...
* Connected to www.huihui.org (216.40.34.37) port 80 (#0)
> GET / HTTP/1.1
> Host: www.huihui.org
> User-Agent: curl/7.76.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< content-length: 0
< location: http://hoverstatus.com/
< cache-control: no-cache
< Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
<
* Connection #0 to host www.huihui.org left intact
[root@nginx ~]# telnet www.huihui.org 80
Trying 216.40.34.37...
Connected to www.huihui.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.huihui.orgHTTP/1.1 302 Found
content-length: 0
location: http://hoverstatus.com/
cache-control: no-cache
Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadConnection closed by foreign host.
Nginx-下载服务器的设定及优化
创一个文件夹
[root@nginx ~]# mkdir /data/web/download
写入一个数据
[root@nginx ~]# dd if=/dev/zero of=/data/web/download/huifile bs=1M count=100
记录了100+0 的读入 记录了100+0 的写出 104857600字节(105 MB,100 MiB)已复制,0.046372 s,2.3 GB/s
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/huihui.org/error.log;access_log /var/log/huihui.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /hui {root /data/web;auth_basic "login password !!";auth_basic_user_file "/usr/local/nginx/.htpasswd";}location = /40x.html{root /data/web/errorpage;}location /download {root /data/web;autoindex on;}
}
[root@nginx ~]# nginx -s reload
服务器有时间差;
怎么调整时间?
server {listen 80;server_name www.huihui.org;root /data/web/html;index index.html;error_page 404 /40x.html;error_log /var/log/huihui.org/error.log;access_log /var/log/huihui.org/access.log;try_files $uri $uri.html $uri/index.html /error/default.html;location /hui {root /data/web;auth_basic "login password !!";auth_basic_user_file "/usr/local/nginx/.htpasswd";}location = /40x.html{root /data/web/errorpage;}location /download {root /data/web;autoindex on;autoinndex_localtime on; #这个这个}
}autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间
autoindex_exact_size on; #计算文件确切大小(单位bytes),此为默认值,off只显示大概大小(单位kb、mb、gb)
limit_rate 1024k; #限速,默认不限速
Nginx 状态页
[root@nginx ~]# cd /usr//local/nginx/conf.d/
[root@nginx conf.d]# vim status.conf
server {listen 80;server_name hx.hx.org; ##没做解析到C盘下加进去root /data/web/html;index index.html;location /status {stub_status;#auth_basic"login"#auth_basic_user_file "/use/local/nginx/.htpasswd" #不能让其他人看要么用上一个,要么下面这个allow 172.25.254.1;deny all;}
}
[root@nginx conf.d]# nginx -s reload
打开浏览器
Nginx的数据压缩功能
打开主配置文件
[root@nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf
gzip on;gzip_comp_level 5;gzip_min_length 1k;gzip_http_version 1.1;gzip_vary on;gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/gif image/png;
[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# echo hello hxhx > /data/web/html/small.html
[root@nginx conf.d]# du -sh /usr/local/nginx/logs/ [root@nginx conf.d]# cat /usr/local/nginx/logs/access.log > /data/web/html/big.html
怎么检测?
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/small.html
HTTP/1.1 200 OK
Server: nginx/1.26.2
Date: Sat, 17 Aug 2024 08:46:57 GMT
Content-Type: text/html
Content-Length: 11
Last-Modified: Sat, 17 Aug 2024 08:40:55 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "66c06217-b"
Accept-Ranges: bytes
[root@nginx conf.d]# curl --head --compressed 172.25.254.100/big.htm