第七周作业
一、完成 Nginx 编译安装脚本
脚本内容
#!/bin/bash
# 安装依赖包
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 创建用户
groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
# 下载 Nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module
# 编译安装
make && make install
# 配置环境变量
echo "export PATH=/usr/local/nginx/sbin:\$PATH" >> /etc/profile
source /etc/profile
# 启动 Nginx
/usr/local/nginx/sbin/nginx
使用说明
- 将脚本保存为
install_nginx.sh。
- 赋予脚本执行权限:
chmod +x install_nginx.sh。
- 运行脚本:
./install_nginx.sh。
二、完成 Nginx 平滑升级,总结步骤
升级步骤
- 下载新版本源码:从 Nginx 官方网站下载最新版本的源码包,例如
nginx-1.25.0.tar.gz。
- 解压并配置:
tar -zxvf nginx-1.25.0.tar.gz
cd nginx-1.25.0
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module
- 编译但不安装:
make
- 停止旧版本 Nginx:
/usr/local/nginx/sbin/nginx -s stop
- 替换旧版本的可执行文件:
cp -f objs/nginx /usr/local/nginx/sbin/nginx
- 启动新版本 Nginx:
/usr/local/nginx/sbin/nginx
- 验证升级是否成功:
/usr/local/nginx/sbin/nginx -v
注意事项
- 在升级过程中,确保配置文件未被修改,否则需要重新复制旧版本的配置文件。
- 如果升级后出现问题,可以通过
/usr/local/nginx/sbin/nginx -s stop 停止新版本,然后重新启动旧版本。
三、总结 Nginx 核心配置,并实现 Nginx 多虚拟主机
Nginx 核心配置
- 全局块:
worker_processes:设置工作进程数量,通常与 CPU 核心数一致。
worker_connections:设置每个工作进程允许的最大连接数。
error_log:指定错误日志文件路径。
- events 块:
worker_connections:与全局块中的设置一致。
- http 块:
include:引入其他配置文件,例如 mime.types。
default_type:默认的 MIME 类型。
server:定义虚拟主机的配置。
多虚拟主机配置示例
[root@ubuntu sites-enabled]# cat www.m99-magedu.com
server{
listen 80;
server_name www.m99-magedu.com;
root /var/www/html/www.m99-magedu.com;
}
[root@ubuntu sites-enabled]# cat www.m99-magedu.net
server{
listen 80;
server_name www.m99-magedu.net;
root /var/www/html/www.m99-magedu.net;
}
[root@ubuntu ~]# mv /var/www/html/index.nginx-debian.html /tmp/
[root@ubuntu ~]# echo "welcome to nginx" > /var/www/html/index.html
[root@ubuntu ~]# mkdir /var/www/html/www.m99-magedu.{com,net}
[root@ubuntu ~]# echo "this page from com" > /var/www/html/www.m99-
magedu.com/index.html
[root@ubuntu ~]# echo "this page from net" > /var/www/html/www.m99-
magedu.net/index.html
[root@ubuntu ~]# tree /var/www/html/
/var/www/html/
├── index.html
├── www.m99-magedu.com
│ └── index.html
└── www.m99-magedu.net
└── index.html
[root@ubuntu ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ubuntu ~]# systemctl reload nginx.service
[root@ubuntu ~]# cat /etc/hosts
10.0.0.181 www.m99-magedu.com www.m99-magedu.net
[root@ubuntu ~]# curl www.m99-magedu.com
this page from com
[root@ubuntu ~]# curl www.m99-magedu.net
this page from net
[root@ubuntu ~]# cat /etc/nginx/sites-enabled/abc.m99-magedu.org
server{
listen 81;
listen 82;
server_name abc.m99-magedu.org;
root /var/www/html/abc.m99-magedu.org;
}
#所有88端口的访问都会被此规则匹配
[root@ubuntu ~]# cat /etc/nginx/sites-enabled/www.m99-magedu.com
server{
listen 88;
server_name www.m99-magedu.com;
root /var/www/html/www.m99-magedu.com;
}
[root@ubuntu ~]# systemctl reload nginx.service
#客户端测试
[root@ubuntu ~]# curl 10.0.0.206
welcome to nginx
[root@ubuntu ~]# curl 10.0.0.206:81
this page from abc.m99-magedu.org
[root@ubuntu ~]# curl 10.0.0.206:82
this page from abc.m99-magedu.org
[root@ubuntu ~]# curl 10.0.0.206:88
this page from com
#用默认端口,被默认规则匹配
[root@ubuntu ~]# curl abc.m99-magedu.org
welcome to nginx
#指定域名和端口
[root@ubuntu ~]# curl abc.m99-magedu.org:81
this page from abc.m99-magedu.org
[root@ubuntu ~]# curl abc.m99-magedu.org:82
this page from abc.m99-magedu.org
#指定域名和端口,被服务器上默认 88 匹配
[root@ubuntu ~]# curl abc.m99-magedu.org:88
this page from com
配置文件路径
- 配置文件通常位于
/usr/local/nginx/conf/nginx.conf。
- 虚拟主机配置可以写在
/etc/nginx/sites-enabled 文件中
四、根据课程演示,完成 Nginx 日志格式定制
默认日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
自定义日志格式
log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'request_time=$request_time upstream_response_time=$upstream_response_time';
应用自定义日志格式
access_log /var/log/nginx/access.log custom;
日志字段说明
$remote_addr:客户端 IP 地址。
$remote_user:客户端用户名称。
$time_local:本地时间。
$request:请求的完整行。
$status:HTTP 状态码。
$body_bytes_sent:发送给客户端的字节数。
$http_referer:请求来源页面。
$http_user_agent:客户端浏览器信息。
$request_time:请求处理时间。
$upstream_response_time:后端服务器响应时间。
五、总结 Nginx 反向代理及 HTTPS 安全加密
Nginx 反向代理
HTTPS 安全加密