Nginx中的模块分类及常见核心模块

1. Nginx 模块分类

Nginx模块可分为:

  1. 核心模块
    Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件
    驱动机制 、进程管理等核心功能
  2. 标准HTTP模块
    提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应
    头设置 等等
  3. 可选HTTP模块
    主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如: Flash
    多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
  4. 邮件服务模块
    实现反向代理功能,包括TCP协议代理
  5. 第三方模块
    是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等
    在这里插入图片描述

2. Nginx核心模块

2.1 user

作用:
进程运行使用的用户和组
示例:

user www www; 
Syntax:	user user [group];
Default:	
user nobody nobody;
Context:	main
Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.

2.2 pid

作用:
指定存储nginx主进程号的文件路径
示例:

pid logs/nginx.pid; 
Syntax:	pid file;
Default:	
pid logs/nginx.pid;
Context:	main
Defines a file that will store the process ID of the main process.

2.3 include

作用:
指明包含进来的其他配置文件
示例:

include vs/*.conf;
Syntax:	include file | mask;
Default:	—
Context:	any
Includes another file, or files matching the specified mask, into configuration. Included files should consist of syntactically correct directives and blocks.

Usage example:

include mime.types;
include vhosts/*.conf;

2.4 worker_processes

作用:
worker进程的数量,应小于等于cpu核心数,auto为当前主机cpu核心数
范例:

worker_processes 4;
Syntax:	worker_processes number | auto;
Default:	
worker_processes 1;
Context:	main
Defines the number of worker processes.

The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it).

The auto parameter is supported starting from versions 1.3.8 and 1.2.5.

2.5 worker_cpu_affinity

作用:
配置CPU亲和,将worker进程与通过cpumask与指定cpu绑定,减少切换造成的CPU时间损耗.
范例:

worker_cpu_affinity 0001 0010 0100 1000;
Syntax:	worker_cpu_affinity cpumask ...;
worker_cpu_affinity auto [cpumask];
Default:	—
Context:	main
Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.

For example,

worker_processes    4;
worker_cpu_affinity 0001 0010 0100 1000;
binds each worker process to a separate CPU, while

worker_processes    2;
worker_cpu_affinity 0101 1010;
binds the first worker process to CPU0/CPU2, and the second worker process to CPU1/CPU3. The second example is suitable for hyper-threading.

The special value auto (1.9.10) allows binding worker processes automatically to available CPUs:

worker_processes auto;
worker_cpu_affinity auto;
The optional mask parameter can be used to limit the CPUs available for automatic binding:

worker_cpu_affinity auto 01010101;
The directive is only available on FreeBSD and Linux.

2.6 worker_priority

作用:
指定worker进程的nice值,范围[-20,20]
范例:

worker_priority -10;
Syntax:	worker_priority number;
Default:	
worker_priority 0;
Context:	main
Defines the scheduling priority for worker processes like it is done by the nice command: a negative number means higher priority. Allowed range normally varies from -20 to 20.

Example:

worker_priority -10;

2.7 worker_rlimit_nofile

作用:
指定worker进程能够打开的最大文件数
范例:

worker_rlimite_nofile 2000;
Syntax:	worker_rlimit_nofile number;
Default:	—
Context:	main
Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.

2.8 daemon

作用:
是否已守护进程方式运行Nginx
范例:

daemon on
Syntax:	daemon on | off;
Default:	
daemon on;
Context:	main
Determines whether nginx should become a daemon. Mainly used during development.

2.9 master_process

作用:
是否已master-worker模型运行Nginx。该指令适用于 nginx 开发人员。
范例:

master_process on;
Syntax:	master_process on | off;
Default:	
master_process on;
Context:	main
Determines whether worker processes are started. This directive is intended for nginx developers.

2.10 error_log

作用:
配置错误日志路径和日志文件名
范例:

error_log logs/error.log error;
Syntax:	error_log file [level];
Default:	
error_log logs/error.log error;
Context:	main, http, mail, stream, server, location
Configures logging. Several logs can be specified on the same configuration level (1.5.2). If on the main configuration level writing a log to a file is not explicitly defined, the default file will be used.

The first parameter defines a file that will store the log. The special value stderr selects the standard error file. Logging to syslog can be configured by specifying the “syslog:” prefix. Logging to a cyclic memory buffer can be configured by specifying the “memory:” prefix and buffer size, and is generally used for debugging (1.7.11).

The second parameter determines the level of logging, and can be one of the following: debug, info, notice, warn, error, crit, alert, or emerg. Log levels above are listed in the order of increasing severity. Setting a certain log level will cause all messages of the specified and more severe log levels to be logged. For example, the default level error will cause error, crit, alert, and emerg messages to be logged. If this parameter is omitted then error is used.

For debug logging to work, nginx needs to be built with --with-debug, see “A debugging log”.
The directive can be specified on the stream level starting from version 1.7.11, and on the mail level starting from version 1.9.0.

2.11 events

作用:
事件驱动相关配置
范例:

events {                                  #事件驱动相关配置
    use epoll;                            #指明并发连接请求的处理方式
    worker_connections 2048;              #每个worker进程能够打开的最大并发连接数
    #accpet mutex on | off;               
    #处理新连接的方式,on意味着由每个worker轮流处理新请求,off意味着每个新请求到达都会通知所有worker进程
}
Syntax:	events { ... }
Default:	—
Context:	main
Provides the configuration file context in which the directives that affect connection processing are specified.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PHP 模块适用于 Nginx 的主要模块是:FastCGI 和 PHP-FPM。FastCGI 是一种通信协议,用于在 Web 服务器与 PHP 解释器之间传递请求,而 PHP-FPM 是一个实现了 FastCGI 协议的 PHP 模块,用于管理 PHP 进程。这两个模块Nginx 与 PHP 集成的重要部分,是很多用户在生产环境选择的首选方案。 ### 回答2: PHP适合与Nginx配合使用的模块有FastCGI、php-fpm和rewrite。 首先,PHP使用FastCGI协议与Nginx进行通信,通过FastCGI将HTTP请求转发给后端的PHP解释器进行处理。这样可以提高PHP的性能和并发处理能力,同时分离了Web服务器和应用服务器,使系统更加灵活和可扩展。 其次,php-fpm(PHP FastCGI Process Manager)是PHP的一个提供FastCGI接口的进程管理器。它可以独立运行,独自管理PHP进程,而不需要依赖Web服务器。Nginx与php-fpm配合使用时,可以通过配置文件指定php-fpm的监听地址和端口,以及进程池配置等参数,进一步提高系统性能和稳定性。 最后,rewrite模块Nginx核心模块之一,可以对URL进行重写和重定向操作。在与PHP配合使用时,可以利用rewrite模块实现URL的美化和重定向功能,提高SEO友好性和用户体验。 总结来说,FastCGI、php-fpm和rewrite模块是PHP适合与Nginx配合使用的模块。它们可以提高系统性能、并发处理能力,分离Web服务器和应用服务器,并提供URL的美化和重定向功能。 ### 回答3: 在PHP,有一些适合与Nginx一起使用的模块。下面是一些常见的适合Nginx的PHP模块: 1. PHP-FPM:这是一个PHP FastCGI进程管理器,适用于与Nginx集成。PHP-FPM通过处理PHP脚本的请求,将其传递给Nginx进行处理。 2. OpCache:这个模块缓存PHP脚本,以提高性能。它通过在内存存储PHP编译代码来减少每次执行脚本的时间,从而提高响应速度。 3. Memcached:这是一个在分布式系统缓存数据的高效模块。它可以与PHP一起使用,以在Nginx和PHP之间共享缓存数据,提高性能和响应速度。 4. Redis:与Memcached类似,Redis也是一个高性能的缓存和存储系统。它提供了更多的功能和灵活性,可以与Nginx和PHP一起使用来提高性能。 5. Xdebug:这个模块是一个用于调试PHP代码的强大工具。它可以与Nginx一起使用,以便在开发和调试过程进行实时调试和分析。 总之,以上提到的这些PHP模块都适合与Nginx一起使用,可以提高性能、加速响应速度并提供其他重要的功能。这些模块的组合取决于您的具体需求和项目要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值