第七周


第七周作业

一、完成 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

使用说明

  1. 将脚本保存为 install_nginx.sh
  2. 赋予脚本执行权限:chmod +x install_nginx.sh
  3. 运行脚本:./install_nginx.sh

二、完成 Nginx 平滑升级,总结步骤

升级步骤

  1. 下载新版本源码:从 Nginx 官方网站下载最新版本的源码包,例如 nginx-1.25.0.tar.gz
  2. 解压并配置
    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
    
  3. 编译但不安装
    make
    
  4. 停止旧版本 Nginx
    /usr/local/nginx/sbin/nginx -s stop
    
  5. 替换旧版本的可执行文件
    cp -f objs/nginx /usr/local/nginx/sbin/nginx
    
  6. 启动新版本 Nginx
    /usr/local/nginx/sbin/nginx
    
  7. 验证升级是否成功
    /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 反向代理

  • 定义:Nginx 作为代理服务器,将客户端的请求转发到后端服务器,然后将后端服务器的响应返回给客户端。
  • 配置示例
    server{
     listen 80;
     server_name www.m99-magedu.com;
     #root /var/www/html/www.m99-magedu.com;
     location /{
     proxy_pass http://10.0.0.210; #210要开启WEB服务,请求的是默认
    default_Serve 配置
     }
    }
    [root@ubuntu ~]# curl http://10.0.0.210
    hello world
    #被转发到后端210
    [root@ubuntu ~]# curl http://www.m99-magedu.com 
    hello world
    #转发到指定IP指定端口
    server{
     listen 80;
     server_name www.m99-magedu.com;
     location /{
     proxy_pass http://10.0.0.210:8080;
     }
    }
    #后端主机配置
    server {
     listen 8080;
     root /var/www/html/8080;
    }
    [root@ubuntu ~]# curl www.m99-magedu.com
    hello 8080
    #转发到指定域名
    server{listen 80;
     server_name www.m99-magedu.com;
     location /{
     proxy_pass http://www.node-1.com;
     }
    }
    [root@ubuntu ~]# cat /etc/hosts
    10.0.0.210 www.node-1.com
    #后端主机配置
    server {
     listen 80;
     root /var/www/html/www.node-1.com;
     server_name www.node-1.com;
    }
    [root@ubuntu ~]# echo "node-1" > /var/www/html/www.node-1.com/index.html
    #测试
    [root@ubuntu ~]# curl www.m99-magedu.com
    node-1
    
    
  • 优点
    • 负载均衡:可以将请求分发到多个后端服务器。
    • 安全:隐藏后端服务器的真实地址。
    • 缓存:可以缓存静态资源,提高响应速度。

HTTPS 安全加密

  • 定义:HTTPS 是 HTTP 的安全版本,通过 SSL/TLS 协议对数据进行加密,确保数据传输的安全性。
  • 配置示例
    server {
        listen 443 ssl;
        server_name secure.example.com;
    
        ssl_certificate /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;
    
        location / {
            proxy_pass http://backend.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
  • 证书生成
    • 使用 OpenSSL 生成自签名证书:
      openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/server.key -out /etc/nginx/ssl/server.crt
      
    • 使用 Let's Encrypt 提供免费证书:
      certbot certonly --nginx -d secure.example.com
      
  • 优点
    • 数据加密:防止数据在传输过程中被窃取。
    • 身份验证:通过证书验证服务器的身份。
    • 完整性保护:确保数据在传输过程中未被篡改。

posted @ 2025-04-06 19:38  你好,运维人  阅读(28)  评论(0)    收藏  举报