Nginx的核心功能通过模块化架构实现,主要分为以下几类:
大多数内置模块通过配置文件启用:
# 示例:启用gzip压缩
http {
gzip on;
gzip_types text/plain application/xml;
# 启用SSL
server {
listen 443 ssl;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
}
}
Nginx支持在编译时通过--add-module
参数添加第三方模块:
# 下载Nginx源码和第三方模块
wget http://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
git clone https://github.com/第三方模块仓库.git
# 编译安装
cd nginx-1.25.3
./configure --add-module=../第三方模块路径 \
--with-http_ssl_module \
--with-http_v2_module
make
sudo make install
# 编译为动态模块
./configure --add-dynamic-module=../第三方模块路径
# 在nginx.conf中加载
load_module modules/模块名.so;
ngx_brotli:Brotli压缩算法
brotli on;
brotli_types text/plain text/css application/json;
ngx_pagespeed:Google开发的页面优化
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
ModSecurity:WAF防火墙
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
ngx_http_auth_pam_module:PAM认证
ngx_http_geoip2_module:IP地理位置
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_data_country_code country iso_code;
}
lua-nginx-module:Lua脚本支持
location /hello {
content_by_lua_block {
ngx.say("Hello, Lua!")
}
}
nginx-rtmp-module:流媒体支持
rtmp {
server {
listen 1935;
application live {
live on;
}
}
}
一个Nginx模块通常包含: - 模块配置结构 - 指令定义 - 处理函数 - 模块上下文
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r);
// 指令定义
static ngx_command_t ngx_http_hello_commands[] = {
{ ngx_string("hello"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hello,
0,
0,
NULL },
ngx_null_command
};
// 模块上下文
static ngx_http_module_t ngx_http_hello_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
// 模块定义
ngx_module_t ngx_http_hello_module = {
NGX_MODULE_V1,
&ngx_http_hello_module_ctx, /* module context */
ngx_http_hello_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
// 处理函数
static ngx_int_t ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_handler;
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) {
ngx_buf_t *b;
ngx_chain_t out;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = b;
out.next = NULL;
b->pos = (u_char *) "Hello, world!";
b->last = b->pos + sizeof("Hello, world!") - 1;
b->memory = 1;
b->last_buf = 1;
r->headers_out.content_length_n = b->last - b->pos;
ngx_http_send_header(r);
return ngx_http_output_filter(r, &out);
}
模块选择原则:
性能考虑:
安全建议:
调试技巧:
# 查看已加载模块
nginx -V
# 调试日志
error_log /var/log/nginx/error.log debug;
通过合理使用Nginx的模块系统,可以构建出高性能、高可定制的Web服务解决方案。