当前位置: 首页 > news >正文

Nginx 配置文件解析

nginx 配置文件解析

1、nginx 配置文件模块介绍

nginx 配置文件采用 分层模块化设计,主要分为以下几大模块

模块层级作用域核心功能
main全局生效控制进程、日志、性能等全局参数
events网络连接层定义连接处理模型与并发参数
httpHTTP 协议层所有 HTTP 相关配置的容器(可嵌套多个 server)
server虚拟主机层定义域名、端口、根目录等主机参数
locationURL 路由层根据 URL 规则处理请求
upstream负载均衡层定义后端服务器集群与负载均衡

2、语法规则

2.1 基础语法

# 当行指令格式:指令名 参数1 参数2 ...;
worker_processes auto;  # 指令以分好结尾

# 指令块格式:指令名 { ... }
events {
  worker_connections: 1024;  # 快内指令须缩进
  use epoll;
}

# 注释:以 # 开头
# 错误日志 路径配置
error_log  logs/error.log  info;

2.3 高级特性

  • 变量:使用 $ 符号引用

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
    
  • 正则匹配:~ (区分大小写) 或 ~* (不区分)

    location ~* \.(jpg|png)$ {...}  # 匹配图片请求
    
  • 文件包含:通过 include 提升可维护性

    include /etc/nginx/conf.d/*.conf;  # 加载子配置文件
    

3、nginx 配置文件 解析

#user  nobody;  # 运行用户(安全加固必选项)
worker_processes  1;  # 工作进程数(推荐设为 CPU 核心数)

#error_log  logs/error.log;  # 错误日志与级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;  # 进程 PID 文件路径


events {
    worker_connections  1024;  # 每个工作进程允许的最大连接数。1024 表示每个工作进程最多可以处理 1024 个并发连接
}


http {
    include       mime.types;  # 包含 MIME 类型定义文件,用于映射文件扩展名到 MIME 类型
    default_type  application/octet-stream;  # 默认的 MIME 类型。如果无法匹配到具体的 MIME 类型,则使用此值
		
  	# 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # 指定 访问日志的路径和格式
    access_log  logs/access.log  main;

    # 启用 高效文件传输模式,提升静态文件的传输性能
    sendfile        on;
  
  	# 启用后会发送数据包时进行优化(如合并小包),通常与 sendfile 配合使用
    tcp_nopush     on;

    # 设置长连接的超时时间(单位:秒)。65 表示客户端在 65 秒内没有新请求时,连接会被关闭
    #keepalive_timeout  0;
    keepalive_timeout  65;

  	# 启用 GZIP 压缩功能,减少传输数据量。
    gzip  on;
    gzip_types text/plain text/css application/json;  # 压缩文件类型
    gzip_min_length 1024;         # 最小压缩文件大小
    gzip_comp_level 6;            # 压缩级别(1-9)
  
    upstream backend {
    		# 负载策略(默认轮询)
    		server 192.168.1.100:8080 weight=5;  # 权重分配(权重越高处理越多请求)
    		server 192.168.1.101:8080;
    		server backup.example.com:8080 backup;  # 备用服务器

    		# 会话保持策略(三选一)
    		ip_hash;                     # 基于客户端IP哈希
    		# least_conn;                # 最少连接数优先
    		# fair;                      # 第三方模块,响应时间优先

    		keepalive 32;                # 保持的长连接数(提升性能)
		}


    server {
    	  # 监听端口。80 是 HTTP 协议的默认端口
        listen       80;
        # 定义服务器名称:localhost 表示该虚拟主机响应针对 localhost 的请求
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
    
        # 访问控制(白名单/黑名单)
        allow 192.168.1.0/24;         # 允许指定网段
        deny all;                     # 拒绝其他所有IP

    	  # 定义根路径 的处理规则
        location / {
            # 设置 网站的根目录。 html 表示相对于 nginx 安装目录的 html 文件夹
            root   html;
            # 指定默认首页文件
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;  # 文件查找顺序(适用于SPA)
        }
    
        location /api/ {
            proxy_pass http://backend;    # 反向代理到后端集群
            proxy_set_header Host $host;  # 透传原始域名
            proxy_set_header X-Real-IP $remote_addr;  # 传递客户端真实IP
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;  # 指定 SSL 证书 和 私钥的路径
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;  # 指定 SSL 会话缓存
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;  # 指定 加密算法
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

相关文章:

  • 内存池项目(1)——前置知识
  • CF2074F Counting Necessary Nodes
  • 电动垂直起降飞行器(eVTOL)
  • 每天五分钟深度学习框架pytorch:搭建LSTM完成时间序列的预测
  • 汉明码:从奇偶校验到错误精确定位的传奇编码
  • 【11408学习记录】英语通知写作速成攻略:框架拆解+宾语从句疑难全破解
  • 21 天 Python 计划:MySQL 库相关操作
  • DB-Mysql中TIMESTAMP与DATETIME的区别
  • 【Flask开发】嘿马文学web完整flask项目第3篇:2.用户认证,2.用户认证【附代码文档】
  • 【STM32单片机】#6 定时器比较输出
  • OceanSim: 基于Isaac Sim GPU 加速水下机器人感知仿真框架
  • 基于SpringBoot酒店管理系统设计和实现(源码+文档+部署讲解)
  • qt socket编程正确重启tcpServer的姿势
  • 同一份数据,Redis为什么要存两次
  • 人脸考勤管理一体化系统(人脸识别系统,签到打卡)
  • WinForm真入门(9)——RichTextBox控件详解
  • 基于大数据的美团外卖数据可视化分析系统
  • 【易飞】易飞批量选择品号处理方法,工作效率提升300%
  • 学透Spring Boot — 018. 优雅支持多种响应格式
  • 多智能体优秀开发框架
  • 怎么使用网站服务器/互联网广告怎么做
  • 要加强网站内容建设/厦门百度代理
  • 上海网安网站建设/企业网站推广注意事项
  • 网站建设平面要多少分辨率/百度知道下载
  • 做舞台灯光的在哪些网站接订单呢/营销平台建设
  • 有专业制作网站的公司吗/怎样宣传网站