多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ ... ... #核心摸块 events { #事件模块 ... } http { # http 模块 server { # server块 location [PATTERN] { # location块 ... } location [PATTERN] { ... } } server { ... } } mail { # mail 模块 server { # server块 ... } } ~~~ https://www.nginx.cn/doc/index.html ## 核心模块 主要设置一些影响 Nginx 服务器整体运行的配置指令。比如:worker_processes 1;`worker_processes `值越大,可以支持的并发处理量就越多。 ~~~text user www www; # 配置用户或者组。 worker_processes auto; # 允许生成的进程数,默认为1 error_log /www/wwwlogs/nginx_error.log crit; # 错误日志路径,级别。 pid /www/server/nginx/logs/nginx.pid; # 指定 nginx 进程运行文件存放地址 ~~~ 详细:[Nginx - 主模块](https://www.nginx.cn/doc/core/mainmodule.html) ## 事件模块 涉及的指令主要影响 Nginx 服务器与用户的网络连接。比如:worker_connections 1024;支持的最大连接数。 ~~~text events { accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off use epoll; #事件驱动模型select|poll|kqueue|epoll|resig worker_connections 1024; #最大连接数,默认为512 } ~~~ 详细:[Nginx - 事件模块](https://www.nginx.cn/doc/core/events.html) ## http 模块 又包括 HTTP 全局块和 Server 块,是服务器配置中最频繁的部分,包括配置代理、缓存、日志定义等绝大多数功能。Server 块:配置虚拟主机的相关参数。Location 块:配置请求路由,以及各种页面的处理情况。 ~~~text http { include mime.types; # 文件扩展名与文件类型映射表 default_type application/octet-stream; # 默认文件类型,默认为text/plain access_log off; # 取消服务日志 sendfile on; # 允许 sendfile 方式传输文件,默认为off,可以在 http 块,server 块,location 块。 sendfile_max_chunk 100k; # 每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 keepalive_timeout 65; # 连接超时时间,默认为75s,可以在http,server,location块。 server { keepalive_requests 120; #单连接请求上限次数。 listen 80; #监听端口 server_name 127.0.0.1; #监听地址 index index.html index.htm index.php; root your_path; #根目录 location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } } } ~~~ ### location 查找规则 ~~~json location = / { …… } ~~~ - “=” 开头表示精确匹配,如 A 中只匹配根目录结尾的请求,后面不能带任何字符串。 - “^~” 开头表示 uri 以某个常规字符串开头,不是正则匹配 - “~” 开头表示区分大小写的正则匹配; - “~*” 开头表示不区分大小写的正则匹配 - “/” 通用匹配,如果没有其它匹配,任何请求都会匹配到