一个纯净的实验环境是初学者完成实验部署的基础
nginx软件包地址
一.nginx源码编译
[root@nginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -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 nginx-1.24.0]# rpm -ivh oniguruma-devel-6.9.6-1.el9.5.x86_64.rpm
[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 nginx-1.24.0]# make && make install
[root@Nginx ~]# vim ~/.bash_profile
[root@Nginx ~]# source ~/.bash_profile
把nginx软件的命令执行路径添加到环境变量中:
[root@nginx nginx-1.24.0]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (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@nginx ~]# tar zxf nginx-1.26.1.tar.gz
[root@nginx ~]# cd nginx-1.26.1/
[root@nginx ~]# ./configure --with-http_ssl_module --withhttp_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 sbin]# nginx -V
nginx version: nginx/1.26.1
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (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
#只要make无需要make install
#把之前的旧版的nginx命令备份
[root@Nginx ~]# cd /usr/local/nginx/sbin/
[root@Nginx sbin]# cp nginx nginx.24
#把新版本的nginx命令复制过去
[root@Nginx sbin]# \cp -f /root/nginx-1.26.1/objs/nginx /usr/local/nginx/sbin
#检测一下有没有问题
[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
# 旧nginx的master进程id
[root@Nginx sbin]# kill -USR2 42470 #nginx worker ID
[root@nginx sbin]# kill -WINCH 42407
平滑回滚
[root@nginx objs]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ll
总用量 11168
-rwxr-xr-x 1 root root 5752872 8月 21 21:13 nginx
-rwxr-xr-x 1 root root 5678264 8月 21 21:01 nginx.24
[root@nginx sbin]# cp nginx nginx.26
[root@nginx sbin]# mv nginx.24 nginx
三.自定义子配置文件
[root@Nginx ~]# mkdir /usr/local/nginx/conf.d/
[root@centos8 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
......
include /apps/nginx/conf/conf.d/*.conf; #在配置文件的最后面添加此行 #注意不要放在最前面,会导致前面的命令无法生效
}
四.root和alias
Root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location
Alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制; 仅能用于location上下文,此指令使用较少
root #给定的路径对应于location中的/uri左侧的/
alias #给定的路径对应于location中的/uri的完整路径
root
[root@nginx conf.d]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx conf.d]# echo test1 > /data/web1/test1/index.html
[root@nginx conf.d]# nginx -s reload
alias
五.location符号匹配
=: #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~: #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头 #对uri的最左边部分做匹配检查,不区分字符大小写
~: #用于标准uri前,表示包含正则表达式,并且区分大小写
~*: #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号: #匹配起始于此uri的所有的uri
对目录匹配:优先级大小:~和~*优先级相等,其次是不带符号,再次是^~,最次是=,= 不支持目录是对文件做匹配的
对文件匹配:= > (~ = ~*) >不带符号 > ^~
[root@localhost conf.d]# vim location.conf
server {
listen 80;
server_name www.haha.com;
root /data/web/html;
index index.html;
location /test { 优先级二级
root /data/web1;
}
location = /index.html { 优先级四级
root /data/web2/test;
}
location ~* /test { 先匹配优先级最高
root /data/web4;
}
location ~ /test { 优先级最高
root /data/web3;
}
location ^~ /test { 优先级三级
root /data/web5;
}
}
[root@localhost conf.d]# echo web1 test page > /data/web1/test/index.html
[root@localhost conf.d]# echo web2 test page > /data/web2/test/index.html
[root@localhost conf.d]# echo web3 test page > /data/web3/test/index.html
[root@localhost conf.d]# echo web4 test page > /data/web4/test/index.html
[root@localhost conf.d]# echo web5 test page > /data/web5/test/index.html
测试:
[root@localhost conf.d]# curl www.haha.com/test/
web4 test page
[root@localhost conf.d]# curl www.haha.com/test/
web1 test page
六.用户认证界面
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
[root@nginx ~]# mkdir /data/web1/lee
[root@nginx ~]# echo lee > /data/web1/lee/index.html
[root@nginx ~]# vim /usr/local/nginx/conf.d/vhost.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# cat /usr/local/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.haha.com;
root /data/web1;
location /lee {
auth_basic "login password !!!"; # 认证提示信息
auth_basic_user_file /usr/local/nginx/.htpasswd; # 用户认证文件路径
}
}
七.自定义错误页面
[root@localhost conf.d]# vim err_usr.conf
server {
listen 80;
server_name www.haha.com;
root /data/web/html;
index index.html;
error_page 404 /40x.html
location = /40x.html {
root /data/web/errorpage;
}
}
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# mkdir -p /data/web/errorpage
[root@localhost conf.d]# echo error page > /data/web/errorpage/40x.html
测试:
[root@localhost conf.d]# curl www.haha.com/efq
error page
八.自定义错误日志
[root@localhost conf.d]# vim err_usr.conf
server {
listen 80;
server_name www.haha.com;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/haha.com/error.log;
access_log /var/log/haha.com/access.log;
location = /40x.html {
root /data/web/errorpage;
}
location /v {
root /data/web;
auth_basic "login password !!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
[root@localhost conf.d]# mkdir -p /var/log/haha.com
[root@localhost conf.d]# nginx -s reload
测试:
[root@localhost conf.d]# ll /var/log/lhd.com/
总用量 0
-rw-r--r-- 1 root root 0 8月 16 14:50 access.log
-rw-r--r-- 1 root root 0 8月 16 14:50 error.log
[root@localhost conf.d]# curl www.haha.com
www.haha.com
[root@localhost conf.d]# cat /var/log/haha.com/access.log
192.168.182.100 - - [16/Aug/2024:14:51:46 +0800] "GET / HTTP/1.1" 200 12 "-" "curl/7.76.1"
[root@localhost conf.d]# curl www.haha.com/ll
error page
[root@localhost conf.d]# cat /var/log/haha.com/error.log
2024/08/16 14:52:06 [error] 3873#3873: *14 open() "/data/web/html/ll" failed (2: No such file or directory), client: 192.168.182.100, server: www.lhd.com, request: "GET /ll HTTP/1.1", host: "www.haha.com"
九.检查文件是否存在
[root@localhost conf.d]# vim err_usr.conf
server {
listen 80;
server_name www.haha.com;
root /data/web/html;
index index.html;
error_page 404 /40x.html;
error_log /var/log/lhd.com/error.log;
access_log /var/log/lhd.com/access.log;
try_files $uri $uri.html $uri/index.html /error/default.html
location = /40x.html {
root /data/web/errorpage;
}
location /haha {
root /data/web;
auth_basic "login password !!";
auth_basic_user_file "/usr/local/nginx/.htpasswd";
}
}
测试:
[root@localhost conf.d]# mkdir /data/web/html/error
[root@localhost conf.d]# echo error default > /data/web/html/error/default.html
[root@localhost conf.d]# curl www.haha.com/lsdq
error page
[root@localhost conf.d]# curl www.haha.com/error/default.html
error default