nginx保姆及教学

一、概览与核心概念

NGINX 是高性能的反向代理、负载均衡器、HTTP 静态服务器与 TCP/UDP(stream)代理。它以事件驱动、异步非阻塞著称,适合高并发场景。
nginx.org

配置文件以 nginx.conf 为入口,常见结构:全局指令 → events → http(或 stream)→ server → location。全局还能 load_module 动态模块。

二、常用命令

nginx -t # 配置测试

nginx -s reload # 平滑重载(不丢失连接)

systemctl start nginx
systemctl enable nginx

nginx -V # 查看编译参数和模块

nginx完整配置

点击查看代码
# nginx.conf - 完整示例(注释详尽)
user  nginx;
worker_processes auto;         # 工作进程数量配置 建议 auto(等同 CPU core 数) 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

# 全局 worker 级别事件配置
events {
    use epoll;                 # Linux 推荐 use epoll  高性能事件驱动机制处理网络请求。
    worker_connections  10240; # 每 worker 最大连接数(和 ulimit / sysctl 协同)
    multi_accept on;   #一次性把所有等待的 accept 队列全部接收
}

# 全局 http 配置
http {
    include       /etc/nginx/mime.types;    #作用:告诉 NGINX 使用哪个文件来映射文件类型
    default_type  application/octet-stream;     #当 NGINX 无法从 mime.types 中找到匹配的文件类型时,使用这个默认 MIME 类型。

    sendfile        on;             # 零拷贝    机制,将文件直接从磁盘传输到网络,不经过用户空间。
    tcp_nopush      on;             # 延迟发送 TCP 包,把头和内容合并发 大文件下载、sendfile 静态文件
    tcp_nodelay     on;             # 立即发送 TCP 包,关闭 Nagle WebSocket、低延迟 API、RPC
    keepalive_timeout  65;          # 客户端连接保持的时间
    types_hash_max_size 2048;       # 

    # 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local]  '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent" "$request_time"';

    access_log  /var/log/nginx/access.log  main;
    error_log   /var/log/nginx/error.log warn;

    # 缓存(proxy_cache)路径与 zone
    proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=one:100m max_size=10g inactive=60m use_temp_path=off;

    # 代理连接池到上游
    upstream backend_app {
        least_conn;
        server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
        server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
        keepalive 32; # 与 upstream keepalive 合理搭配
    }

    # HTTP/2 + TLS 推荐(示例)
    server {
        listen 443 ssl http2;
        server_name example.com www.example.com;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;   # ssl证书的crt的位置
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;   #ssl证书key的位置
        # 使用 Mozilla 推荐配置(见文档)
        ssl_protocols TLSv1.2 TLSv1.3;   #指定允许的 TLS 协议版本
        ssl_ciphers 'TLS_AES_...'; # 按 Mozilla 推荐生成
        ssl_prefer_server_ciphers off;
        ssl_session_cache shared:SSL:10m;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        # 反向代理示例(含缓存)
        location / {
            proxy_pass http://backend_app;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_read_timeout 90;
            proxy_connect_timeout 5s;
            proxy_send_timeout 30s;

            proxy_cache one;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            add_header X-Proxy-Cache $upstream_cache_status;
        }

        # WebSocket 支持(保持 Upgrade 头)
        location /ws/ {
            proxy_pass http://backend_app;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }

        # 上传大小
        client_max_body_size 50m;
    }

    # HTTP -> HTTPS 重定向
    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://$host$request_uri;
    }

    # stream 块请在主配置中单独定义(见下文 stream 示例)
}

posted @ 2025-12-09 17:44  中午吃麻辣烫  阅读(2)  评论(0)    收藏  举报