news 2026/10/22 17:40:28

Nginx企业实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Nginx企业实战

任务一:Nginx 流量监控(VTS)

企业场景: 公司需要监控网站访问流量,了解请求数量、网络流量及连接状态。

任务要求:

1. 安装并配置 Nginx VTS 监控模块。

2. 开启流量统计功能。

3. 配置监控数据访问地址。

4. 验证监控页面或 JSON 数据是否正常。

监控页面正常

JSON数据正常

5. 展示请求数、接收流量、发送流量、活动连接数等指标。

6. 使用shell脚本 统计 访问网站次数

#!/bin/bash # 统计 www.tancheng.com 网站访问次数 VTS_URL="http://127.0.0.1/vts-status?format=json" DOMAIN="www.tancheng.com" # 获取json数据 JSON_DATA=$(curl -s ${VTS_URL}) # 提取该域名请求次数 REQ_COUNT=$(echo ${JSON_DATA} | jq -r ".serverZones.\"${DOMAIN}\".requestCounter") # 提取全局总请求 GLOBAL_REQ=$(echo ${JSON_DATA} | jq -r ".requests") echo "=====================================" echo "网站域名:${DOMAIN}" echo "域名访问总次数:${REQ_COUNT}" echo "Nginx全局所有站点总请求:${GLOBAL_REQ}" echo "====================================="

任务二:Nginx 一键安装脚本

企业场景: 公司需要快速部署多台 Web 服务器,减少重复手动安装操作。

任务要求:

1. 编写 `install_nginx.sh` 一键安装脚本。

2. 自动安装依赖、下载源码并编译安装 Nginx。

3. 自动创建相关目录并配置开机自启。

4. 自动启动 Nginx,并检查服务状态。

5. 脚本具备错误提示,关键步骤失败时能够停止执行。

6. 重复执行时避免覆盖已有配置或重复安装造成异常。

#!/bin/bash # install_nginx.sh 源码编译nginx 1.20.2 set -euo pipefail # ========== 变量区 ========== src_dir="/usr/local/src" ins_dir="/usr/local/nginx" user="nginx" group="nginx" conf="${ins_dir}/conf/nginx.conf" svc_file="/usr/lib/systemd/system/nginx.service" # ============================ # 颜色 red='\033[0;31m' green='\033[0;32m' nc='\033[0m' # 错误退出函数 err_exit() { echo -e "${red}[error] $1${nc}" 1>&2 exit 1 } echo -e "${green}==== start nginx source install ====${nc}" # 检查是否已经安装,防止重复覆盖 if [ -x "${ins_dir}/sbin/nginx" ];then echo -e "${green}[info] nginx already installed at ${ins_dir}" echo "[info] version:" ${ins_dir}/sbin/nginx -v echo "[info] exit, skip install, no overwrite!" exit 0 fi # 校验系统 if [ ! -f /etc/redhat-release ];then err_exit "script only support centos" fi # 安装编译依赖 echo -e "${green}[info] install deps${nc}" yum install -y gcc gcc-c++ make zlib-devel pcre-devel openssl-devel wget || err_exit "yum install deps fail" # 创建nginx系统用户,无登录shell id ${user} >/dev/null 2>&1 || useradd -M -s /sbin/nologin ${user} # 创建源码目录 mkdir -p ${src_dir} || err_exit "create src dir fail" cd ${src_dir} || err_exit "cd to src dir fail" # nginx版本1.20.2 pkg="nginx-1.20.2.tar.gz" if [ ! -f ${pkg} ];then echo -e "${green}[info] download nginx ${pkg}${nc}" wget http://nginx.org/download/${pkg} || err_exit "download nginx src fail" else echo "[info] src package exists, skip download" fi # 解压源码 rm -rf nginx-1.20.2 tar -zxf ${pkg} || err_exit "tar unpack fail" cd nginx-1.20.2 || err_exit "cd nginx src folder fail" # configure编译配置 echo -e "${green}[info] run configure${nc}" ./configure \ --prefix=${ins_dir} \ --user=${user} \ --group=${group} \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_module \ --with-http_realip_module || err_exit "configure failed" echo -e "${green}[info] compiling make${nc}" make || err_exit "make compile error" echo -e "${green}[info] make install${nc}" make install || err_exit "make install fail" # 防止覆盖已有systemd服务文件 if [ -f "${svc_file}" ];then err_exit "${svc_file} already exists, skip writing service file" fi # 写入systemd服务文件 echo -e "${green}[info] create systemd nginx service${nc}" cat > ${svc_file} <<EOF [Unit] Description=Nginx Web Server After=network.target [Service] Type=forking PIDFile=${ins_dir}/logs/nginx.pid ExecStart=${ins_dir}/sbin/nginx ExecReload=${ins_dir}/sbin/nginx -s reload ExecStop=${ins_dir}/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload || err_exit "daemon-reload fail" systemctl enable nginx || err_exit "enable nginx boot fail" # 启动nginx,校验状态 echo -e "${green}[info] start nginx${nc}" systemctl start nginx || err_exit "start nginx failed" ${ins_dir}/sbin/nginx -t || err_exit "nginx config test fail" systemctl is-active nginx | grep -q active || err_exit "nginx not running" # 安装完成信息输出 echo -e "${green}================ install finish ================${nc}" echo "install path: ${ins_dir}" echo "config file: ${conf}" echo "bin file: ${ins_dir}/sbin/nginx" echo "check version: ${ins_dir}/sbin/nginx -v" echo "check compile args: ${ins_dir}/sbin/nginx -V" echo "service cmd:" echo "systemctl start/stop/restart/reload nginx" echo "systemctl enable/disable nginx" echo "systemctl status nginx" echo -e "${green}=================================================${nc}"

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/22 17:38:20

(ICLR 2020)MAML 靠的不是快速学习,而是特征复用

文章目录导读背景动机方法框架核心实验&#xff1a;特征复用是 MAML 有效的主导因素衍生算法网络头与主体的分工泛化&#xff1a;元学习的普遍规律总结思考补充导读 论文标题&#xff1a;Rapid Learning or Feature Reuse? Towards Understanding the Effectiveness of MAML&…

作者头像 李华
网站建设 2026/10/22 17:29:13

Ice:macOS 菜单栏图标隐藏管理完全指南,一键快速取回

Ice&#xff1a;macOS 菜单栏图标隐藏管理完全指南&#xff0c;一键快速取回 【免费下载链接】Ice Powerful menu bar manager for macOS 项目地址: https://gitcode.com/GitHub_Trending/ice/Ice 菜单栏挤了二十多个图标&#xff0c;蓝牙和 Wi‑Fi 还被刘海盖住一半——…

作者头像 李华
网站建设 2026/10/22 17:27:48

agent-self-scheduling - SKILL

name: agent-self-scheduling description: “Schedule AI agent runs with cron, loops, or external clocks while avoiding unsafe tight autonomous timers.” category: agent-orchestration risk: critical source: community source_repo: davidondrej/skills source_ty…

作者头像 李华
网站建设 2026/10/22 17:25:34

【TongWeb7.0.4.9M11开启远程访问以及忘记密码如何重置】

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/10/22 17:11:28

noname 无名杀网页版三国杀 免费完整体验指南

noname 无名杀网页版三国杀 免费完整体验指南 【免费下载链接】noname 项目地址: https://gitcode.com/GitHub_Trending/no/noname noname&#xff08;无名杀&#xff09;是一个可自行搭建的开源网页版三国杀项目&#xff0c;浏览器打开即玩&#xff0c;无需安装客户端…

作者头像 李华