1 卸载不需要的模块
卸载不需要的 nginx 模块, 最大限度地将 nginx 加载的模块最小化
① 建议配置
(1)检查需要禁用的模块
在编译 nginx 服务器时,使用下面的命令查看哪些模块应该启用,哪些模应
该禁用:
# ./configure --help | less
一旦处选了要禁用的模块,需要与相关人员沟通确认,并经过测试不影响业
务运行。
(2)例如,要禁用 autoindex 和 SSI 模块,命令如下:
# ./configure --without-http_autoindex_module --without-http_ssi_module
# make
# make install
② 备注事项
Nginx 不包含不必要的模块或者输入./configure --help | less 进行检查
2 防盗链设置
防止其他网站盗链本网站资源
① 建议配置
(1)修改配置文件
#vi /usr/local/nginx/conf/nginx.conf
具体设置如下:
location ~* ^. \.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.nsfocus.com
http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/ [img]http://www.nsfocus.com/images/default/logo.webp[/img];
# return 403;
}
}
根据应用场景,设置合适的域名
(2)重新启动 nginx 服务
② 备注事项
从非法网站访问所保护的资源,出现设置的页面。同时检查配置文件 #more /usr/local/nginx/conf/nginx.conf
3 自定义错误信息
1)修改 src/http/ngx_http_special_response.c,自己定制错误信息
## messages with just a carriage return.
static char ngx_http_error_400_page[] = CRLF;
static char ngx_http_error_404_page[] = CRLF;
static char ngx_http_error_413_page[] = CRLF;
static char ngx_http_error_502_page[] = CRLF;
static char ngx_http_error_504_page[] = CRLF;
常见错误:
400 bad request
404 NOT FOUND
413 Request Entity Too Large
502 Bad Gateway
504 Gateway Time-out
(2)重新启动 nginx 服务
② 备注事项
URL 地址栏中输入 http://ip:8800/manager12345,访问出错时,返回自定义的错误页面
4 隐藏 nginx 服务信息头
① 建议配置
修改 nginx解压路径/src/http/ngx_http_header_filter_module.c文件的第48
和 49 行内容,自定义头信息:
static char ngx_http_server_string[] = “Server:**XX.com” CRLF;
static char ngx_http_server_full_string[] = “Server:**XX.com” CRLF;
添加如下代码到 nginx.conf 配置文件,禁止错误页面中显示 nginx 版本号:
server_tokens off
② 备注事项
服务信息头显示设置的内容,检查 http 服务信息头内容
5 补丁更新
安装系统补丁,修补漏洞
1 、参考配置操作
手动安装补丁或安装最新版本软件,所安装的补丁,应首先在经过测试验证;安装前,要做好数据备份。
2查看版本和编译器信息
欢迎大家分享更好的思路^^_^^ !