参考资料

  1. Nginx 启动、停止、重启、加载配置详细说明以及案例
  2. Nginxalias 访问路径别名指令详细说明以及案例
  3. Nginx + PHP-FPM 配置指南
  4. nginx配置文件详解
  5. 如何结合WAF使用Nginx提高安全性?
  6. Service Worker文件如何更新?
  7. Nginx核心配置指令详细说明以及案例
  8. nginx配置文件详解

Nginx基本介绍

  1. 定义:

  • 高性能HTTP和反向代理服务器

  • 轻量级Web服务器

  • 事件驱动架构

  1. 核心参数:

2.1 全局块参数:
worker_processes auto;  # 工作进程数
error_log /var/log/nginx/error.log;  # 错误日志路径
pid /run/nginx.pid;  # 进程ID文件位置

2.2 events块参数:
worker_connections 1024;  # 单个工作进程最大连接数
use epoll;  # 事件驱动模型
multi_accept on;  # 同时接受多个连接

2.3 http块参数:
include mime.types;  # MIME类型文件
default_type application/octet-stream;  # 默认MIME类型
sendfile on;  # 启用高效文件传输
keepalive_timeout 65;  # 保持连接超时时间

  1. 配置示例:

3.1 静态网站配置:
server {
   listen 80;
   server_name example.com;
   root /var/www/html;
   index index.html;

location / {
    try_files $uri $uri/ =404;
}

}

3.2 反向代理配置:
server {
   listen 80;
   server_name api.example.com;

location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

}

3.3 负载均衡配置:
upstream backend {
   server 192.168.1.100:8080;
   server 192.168.1.101:8080;
   server 192.168.1.102:8080;
}

server {
   listen 80;
   server_name app.example.com;

location / {
    proxy_pass http://backend;
}

}

  1. 常用指令:

  • access_log: 访问日志配置

  • gzip: 压缩配置

  • rewrite: URL重写

  • ssl_certificate: SSL证书配置

  • client_max_body_size: 客户端最大请求体大小