Skip to content
On this page

Nginx 工具与资源

本章介绍与Nginx相关的工具、资源和学习材料,帮助您更好地使用和管理Nginx。

在线工具

SSL配置生成器

配置验证工具

命令行工具

基本命令

bash
# 启动Nginx
nginx

# 停止Nginx
nginx -s stop

# 优雅停止Nginx
nginx -s quit

# 重新加载配置(零停机)
nginx -s reload

# 重新打开日志文件
nginx -s reopen

# 测试配置文件语法
nginx -t

# 使用指定配置文件测试
nginx -c /path/to/nginx.conf -t

# 显示版本和编译信息
nginx -v
nginx -V

系统服务管理

bash
# 使用systemd管理
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl status nginx
sudo systemctl enable nginx  # 开机自启
sudo systemctl disable nginx # 禁用开机自启

监控工具

系统监控

bash
# 查看Nginx进程
ps aux | grep nginx

# 查看端口监听状态
netstat -tlnp | grep nginx
ss -tlnp | grep nginx

# 查看网络连接
netstat -an | grep :80
netstat -an | grep :443

# 查看资源使用情况
top -p $(pgrep -d',' nginx)

日志分析工具

bash
# 实时查看访问日志
tail -f /var/log/nginx/access.log

# 实时查看错误日志
tail -f /var/log/nginx/error.log

# 统计访问IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head

# 统计访问URL
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head

# 统计HTTP状态码
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -nr

# 查找404错误
grep " 404 " /var/log/nginx/access.log

# 查找5xx错误
grep " 5[0-9][0-9] " /var/log/nginx/access.log

第三方模块

常用第三方模块

  1. ngx_http_geoip_module - 根据IP地理位置提供不同服务
  2. ngx_http_perl_module - 嵌入Perl处理
  3. ngx_http_image_filter_module - 图片处理和缩放
  4. nginx-module-vts - 虚拟主机流量监控
  5. nginx-lua - 集成Lua脚本

编译安装带模块的Nginx

bash
# 下载Nginx源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -xzf nginx-1.22.0.tar.gz
cd nginx-1.22.0

# 下载所需模块源码
git clone https://github.com/openresty/lua-nginx-module.git

# 配置编译选项
./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/usr/lib/nginx/modules \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --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-file-aio \
    --with-http_sub_module \
    --add-module=../lua-nginx-module

# 编译安装
make && sudo make install

配置管理工具

Ansible

yaml
# nginx.yml
---
- name: Install and configure Nginx
  hosts: webservers
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Copy Nginx configuration
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx

    - name: Start and enable Nginx
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: restart nginx
      systemd:
        name: nginx
        state: restarted

Docker

docker
# Dockerfile
FROM nginx:alpine

# 复制自定义配置
COPY nginx.conf /etc/nginx/nginx.conf

# 复制站点文件
COPY ./html /usr/share/nginx/html

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]
yaml
# docker-compose.yml
version: '3.8'
services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - app
    networks:
      - webnet

  app:
    build: .
    networks:
      - webnet

networks:
  webnet:

性能测试工具

Apache Bench (ab)

bash
# 基本压力测试
ab -n 10000 -c 100 http://your-server/

# 测试POST请求
ab -n 1000 -c 10 -p data.txt -T "application/x-www-form-urlencoded" http://your-server/

wrk

bash
# 安装wrk
git clone https://github.com/wg/wrk.git
cd wrk && make

# 基本测试
wrk -t12 -c400 -d30s http://your-server/

# 带脚本的测试
wrk -t12 -c400 -d30s -s script.lua http://your-server/

JMeter

对于更复杂的测试场景,可以使用Apache JMeter进行负载测试。

学习资源

官方文档

书籍推荐

  1. "Nginx HTTP Server" by Clement Nedelcu
  2. "Nginx Cookbook" by Derek DeJonghe
  3. "Building Web Infrastructure with Nginx" by Alessandro Fael Garcia

在线教程

社区和论坛

安全资源

安全配置检查清单

漏洞监测

备份和恢复

配置备份脚本

bash
#!/bin/bash
# backup-nginx-config.sh

BACKUP_DIR="/backup/nginx"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

# 备份配置文件
tar -czf $BACKUP_DIR/config_$DATE.tar.gz /etc/nginx/

# 备份日志(可选,根据磁盘空间决定)
# tar -czf $BACKUP_DIR/logs_$DATE.tar.gz /var/log/nginx/

# 清理7天前的备份
find $BACKUP_DIR -name "config_*.tar.gz" -mtime +7 -delete

echo "Nginx配置已备份到 $BACKUP_DIR"

配置版本控制

bash
# 使用Git管理配置文件
cd /etc/nginx
git init
git add .
git commit -m "Initial Nginx configuration"

# 每次修改配置前
git add .
git commit -m "Update configuration: <description>"

通过使用这些工具和资源,您可以更高效地管理、监控和优化Nginx服务器。