Post

Nginx场景

Nginx场景

🔹1. Nginx 最重要的就是配置吗?

✅ 是的,配置是Nginx的核心

  • Nginx 本身是一个非常轻量、模块化的高性能服务器,几乎所有功能都由配置文件控制。
  • Nginx 没有复杂的内部逻辑,它的“强大”在于灵活的配置能力,能实现从反向代理、负载均衡到高并发网站服务等多种用途。

所以,掌握配置,是掌握 Nginx 的核心。


🔹2. 掌握了配置 = 掌握了 Nginx 吗?

✅ 大体上可以这么说,但更准确的说法是:

掌握各种实际场景下的 Nginx 配置,才算真正掌握了 Nginx。

因为:

  • 单纯记住配置指令没用,必须理解 这些配置背后的场景需求Nginx 的行为机制
  • 还需要了解 Nginx 的模块、日志调试、性能调优、安全等维度。

🔹3. 你的方向很对 —— 从场景入手非常合适!

这是很多资深工程师的学习方式,因为:

每一种真实业务场景,都是对 Nginx 能力的一次完整应用。


🔹4. 学习路径:通过场景掌握 Nginx

我们以“从简单到复杂”的场景出发,帮你全面掌握 Nginx:

场景学习重点示例
✅ 静态网站托管locationrootindex用 Nginx 部署 HTML、图片、JS 文件
✅ 反向代理proxy_passupstream把流量转发到 Node、Flask、Spring 等后端
✅ 负载均衡upstream 多后端配置做轮询、IP 哈希、健康检查等
✅ HTTPS 配置ssl_certificatessl_protocols使用 Let’s Encrypt、单域/多域证书配置
✅ 动静分离正则匹配不同后缀做不同处理动态用 proxy,静态走本地
✅ Gzip 压缩gzip on提升网页传输速度
✅ 反爬虫 / 安全limit_req、黑名单、Fail2Ban防止恶意请求
✅ 子路径代理location /api/ 转发支持前后端分离项目的 API 代理
✅ WebSocket 支持proxy_http_version 1.1 + Upgrade聊天系统等实时通信
✅ 配合前端构建系统Vue/React 的 history 模式适配try_files 配置
✅ Nginx + Dockerdocker-compose 中使用 nginx 反代多服务协同部署
✅ 性能优化worker_processes、缓存配置处理高并发场景
✅ 日志与监控access_loglog_format结合 ELK 或 Prometheus

🔹5. 补充能力:不仅仅是配置文件

为了真正掌握 Nginx,还建议了解以下内容:

领域内容
🔧 调试能力利用 error.logaccess.log 定位问题
📦 模块机制内置模块 vs 第三方模块,比如 ngx_http_lua_module
🚧 高可用部署配合 Keepalived、Consul 做热备
🔐 安全机制防盗链、限制方法、隐藏版本号等
🧩 Lua 扩展用 Lua 写一些轻量业务逻辑
📈 性能监控stub_status 或接入 Grafana 展示状态

✅ 总结

掌握各种场景下的 Nginx 配置 ≈ 掌握了 Nginx。

配置是核心,但最终目标是:

  • 看得懂别人写的复杂配置
  • 写得出满足业务场景的配置
  • 能排查问题、做优化

============================= 🧾 NGINX 配置块全景 Cheatsheet =============================

一、🌐 Main 块(全局块)

位置:nginx.conf 顶层

常用配置指令:

  • user nginx; # 指定运行用户
  • worker_processes auto; # 工作进程数(建议使用 auto)
  • error_log /var/log/nginx/error.log warn; # 错误日志路径和级别
  • pid /var/run/nginx.pid; # PID 文件路径
  • daemon on|off; # 是否后台运行
  • worker_rlimit_nofile 65535; # 进程可打开最大文件数

二、🔁 Events 块

位置:Main 块内

常用配置指令:

  • worker_connections 1024; # 每个 worker 最大连接数
  • use epoll|kqueue|select; # 事件模型(Linux 推荐 epoll)
  • multi_accept on; # 同时接受多个连接

三、🌐 HTTP 块

位置:Main 块内,核心部分

通用设置:

  • include mime.types; # 引入 MIME 类型
  • default_type application/octet-stream; # 默认 MIME 类型
  • sendfile on; # 启用零拷贝
  • tcp_nopush on; # 减少网络包数量
  • keepalive_timeout 65; # HTTP Keep-Alive 超时
  • server_tokens off; # 隐藏 Nginx 版本信息

日志配置:

  • log_format main '...'; # 日志格式定义
  • access_log /var/log/nginx/access.log main;

压缩配置:

  • gzip on;
  • gzip_types text/plain text/css application/json ...;
  • gzip_min_length 1024;

缓存配置:

  • proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m max_size=1g inactive=60m use_temp_path=off;
  • proxy_cache mycache;

反向代理:

  • proxy_pass http://backend;
  • proxy_set_header Host $host;
  • proxy_http_version 1.1;
  • proxy_cache_bypass $http_upgrade;

跨域:

  • add_header Access-Control-Allow-Origin *;

四、🏠 Server 块(虚拟主机)

位置:HTTP 块内,可多个

通用配置:

  • listen 80; # 监听端口
  • listen 443 ssl; # 监听 HTTPS
  • server_name example.com; # 域名
  • root /var/www/html; # 网站根目录
  • index index.html index.htm; # 默认首页文件

HTTPS 配置:

  • ssl_certificate /etc/nginx/ssl/cert.pem;
  • ssl_certificate_key /etc/nginx/ssl/cert.key;
  • ssl_protocols TLSv1.2 TLSv1.3;
  • ssl_ciphers HIGH:!aNULL:!MD5;

重定向配置:

  • return 301 https://$host$request_uri;
  • rewrite ^/old/(.*)$ /new/$1 permanent;

安全限制:

  • deny all;
  • allow 192.168.0.0/24;
  • limit_conn addr 10;

五、📁 Location 块(路径处理)

位置:Server 块内,可多个

匹配方式:

  • location = /exact {} # 精确匹配
  • location /prefix {} # 前缀匹配
  • location ~ \.php$ {} # 正则匹配(区分大小写)
  • location ~* \.(jpg|png)$ {}# 正则匹配(不区分大小写)

配置指令:

  • proxy_pass http://127.0.0.1:3000;
  • root /var/www/static;
  • alias /data/images/; # 映射目录
  • try_files $uri $uri/ /index.html;
  • index index.html;
  • add_header X-Powered-By Nginx;
  • expires 7d; # 浏览器缓存控制

WebSocket 支持:

  • proxy_http_version 1.1;
  • proxy_set_header Upgrade $http_upgrade;
  • proxy_set_header Connection "upgrade";

访问控制:

  • auth_basic "Restricted";
  • auth_basic_user_file /etc/nginx/.htpasswd;

六、🔗 Upstream 块(负载均衡)

位置:HTTP 块内

示例配置:

1
2
3
4
5
upstream backend {
    server backend1.example.com weight=3;
    server backend2.example.com max_fails=3 fail_timeout=30s;
    server 127.0.0.1:8000 backup;
}

支持的算法:

  • round-robin(默认)
  • ip_hash
  • least_conn
  • hash $request_uri consistent;

七、🧭 Map 块(变量映射)

位置:HTTP 块内

示例:

1
2
3
4
5
map $http_user_agent $is_bot {
    default 0;
    ~*googlebot 1;
    ~*bingbot 1;
}

八、📁 Include(引入配置)

  • include /etc/nginx/conf.d/*.conf;
  • include mime.types;

常用于模块化管理配置文件。

This post is licensed under CC BY 4.0 by the author.

Trending Tags