[nginx]编译安装及安全优化
nginx_upstream_check_module
nginx-module-vts
nginx打补丁
nginx编译安装
- 下载
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar -xf nginx-1.12.2.tar.gz
- 准备环境
useradd -s /sbin/nologin -M www
yum install perl-devel openssl-devel gcc -y
- 编译安装
cd /usr/local/src/nginx-1.12.2
./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx-1.12.2 /usr/local/nginx
cd /usr/local/nginx/conf
egrep -v '^$|#' nginx.conf.default > nginx.conf
echo "PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile
source /etc/profile
- 管理命令
nginx -t #检查语法
nginx -s stop
nginx -s reload
注: Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。
nginx安全优化
改软件名
改版本号
限制访问(dir/file/扩展名)
限制访问方法
限制上传大小
防盗链
防爬虫
防非法解析
目录安全
使用普通用户运行
www运行worker
错误页优化
改软件名
- 查看下jd的,竟然是JDWS/2.0
[root@n1 conf]# curl -I www.jd.com
HTTP/1.1 302 Moved Temporarily
Server: JDWS/2.0
- 1.修改内核(头部信息)
[root@n1 nginx-1.12.2]# vim /usr/local/src/nginx-1.12.2/src/core/nginx.h
...
13 define NGINX_VERSION "7.0"
14 define NGINX_VER "MTWS/" NGINX_VERSION
...
- 2.修改头部
[root@n1 nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: MTWS" CRLF;
- 3.修改错误页(response header)
[root@n1 nginx-1.12.2]# vim src/http/ngx_http_special_response.c
22 "<hr><center>MTWS</center>" CRLF
...
29 "<hr><center>MTWS</center>" CRLF
...
36 "<hr><center>MTWS</center>" CRLF
- 修改结果检查
[root@n1 src]# grep -nir mtws .
./core/nginx.h:14:#define NGINX_VER "MTWS/" NGINX_VERSION
./core/nginx.h:22:#define NGINX_VAR "MTWS"
./http/ngx_http_header_filter_module.c:49:static u_char ngx_http_server_string[] = "Server: MTWS" CRLF;
./http/ngx_http_special_response.c:22:"<hr><center>MTWS</center>" CRLF
./http/ngx_http_special_response.c:29:"<hr><center>MTWS</center>" CRLF
./http/ngx_http_special_response.c:36:"<hr><center>MTWS</center>" CRLF
- 然后编译安装(清理下之前的环境),测试
[root@n1 ~]# curl -I 192.168.2.11
HTTP/1.1 200 OK
Server: MTWS/7.0.0
- 重新编译
nginx -s stop
rm -rf /usr/local/nginx*
cd /usr/local/src/nginx-1.12.2
./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx-1.12.2 /usr/local/nginx
改版本号
- 默认有版本号
[root@n1 ~]# curl -I 192.168.2.11
#获取的是响应头信息
HTTP/1.1 200 OK
Server: **nginx/1.12.2**
...
- 修改nginx.conf
http{
...
server_tokens off;
...
}
- 再次测试,发现没版本号了
[root@n1 ~]# curl -I 192.168.2.11
HTTP/1.1 200 OK
Server: MTWS #这里的版本号没了
...
限制访问(dir/file/扩展名)
http -> server -> location
location匹配 dir/file/扩展名 来通过ip 阻断
重定向到相应页面
[location语法](http://www.cnblogs.com/iiiiher/p/8545244.html)
限制访问方法
利用Nginx限制HTTP的请求方法
#Only allow these request methods
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 501;
}
#Do not accept DELETE,SEARCH and other methods
限制上传大小
上传文件大小的限制(动态应用)
nginx限制客户端上传附件的大小
防盗链(HTTP referer)
盗链和防盗链场景模拟实现 针对方法1做了改善.
- 根据HTTP referer实现防盗链, 利用referer,并且针对扩展名rewrite重定向,下面的代码为利用referer且针对扩展名rewrite重定向,即实现防盗链的Nginx配置。
- 根据cookie防盗链
- 通过加密变换访问路径实现防盗链
- 在所有网站资源上添加网站信息,让盗链人员帮你做推广宣传
- 设置expires的方法如下:
server {
listen 80;
server_name www.maotai.com;
root html/www;
index index.html index.htm;
access_log logs/www_access.log main;
#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.maotai.com maotai.com;
if ($invalid_referer){
rewrite ^/ http://www.maotai.com/img/nolink.jpg;
}
access_log off;
root html/www;
expires 1d;
break;
}
}
防爬虫
范例1:阻止下载协议代理,命令如下:
## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget)
{
return 403;
}
范例2:添加内容防止N多爬虫代理访问网站,命令如下:
这些爬虫代理使用“|”分隔,具体要处理的爬虫可以根据需求增加或减少,添加的内容如下:
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo!Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot")
{
return 403;
}
防非法解析
禁止非法域名解析到本网站
- 第一种方式:配置一个server虚拟主机区块,放置在所有server区块最前面
server {
listen 80;
server_name - ;
return 501;
}
- 第二种方式:将计就计,通过你的域名访问时候,自动跳转到我的域名上
server {
listen 80 default_server;
server_name _;
rewrite ^(.*) http://www.maotai.com/$1 permanent;
}
if ($host !~ ^www\.maotai\.com$)
{
rewrite ^(.*) http://www.maotai.com/$1 permanent;
}
目录安全
Nginx站点目录文件及目录权限优化
使用普通用户启动nginx
1、切换到普通用户家目录下,创建nginx所需文件
[nginx@web01 ~]$ mkdir -p blog/{conf,logs,html}
[nginx@web01 ~]$ cd blog/
[nginx@web01 blog]$ cp /usr/local/nginx/conf/nginx.conf.default ./conf/
[nginx@web01 blog]$ grep -vE "^$|#" conf/nginx.conf.default > conf/nginx.conf
[nginx@web01 blog]$ cp /usr/local/nginx/conf/mime.types conf/
2、编写配置文件
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
error_log /home/nginx/blog/logs/error.log;
user inca inca;
pid /home/nginx/blog/logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type usr/local/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 8080;
server_name www.maotai.com;
root /home/nginx/blog/html;
location / {
index index.html index.htm;
}
access_log /home/nginx/blog/logs/web_blog_access.log main;
}
}
注意:普通用户不能使用知名端口,需要使用其他端口启动服务
3、检查配置文件语法,并启动nginx服务
/usr/local/nginx/sbin/nginx -t -c /home/nginx/blog/conf/nginx.conf
或
/usr/local/nginx/sbin/nginx -c /home/nginx/blog/conf/nginx.conf &>/dev/null &
注意:忽略一些不正确的输出信息
worker进程以www用户运行
[root@n1 nginx]# ps -ef|grep nginx
root 12067 1 0 09:01 ? 00:00:00 nginx: master process nginx
**www** 12152 12067 0 09:04 ? 00:00:00 nginx: worker process
- 方法1: 编译时候指定
useradd -s /sbin/nologin -M www
./configure --prefix=/usr/local/nginx-1.12.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
方法2: 修改nginx.conf
user www www; # 用户名,用户组
错误页优化
NGINX错误页面友好显示
范例1:对错误代码403实行本地页面跳转,命令如下:
server {
listen 80;
server_name www.maotai.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 403 /403.html; #<==当出现403错误时,会跳转到403.html页面 /403.html是相对于站点根目录html/www的。
}
范例2:50x页面放到本地单独目录下,进行优雅显示。
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data0/www/html;
}
范例3:改变状态码为新的状态码,并显示指定的文件内容,命令如下:
error_page 404 =200 /empty.gif;
server {
listen 80;
server_name www.maotai.com;
location / {
root /data0/www/bbs;
index index.html index.htm;
fastcgi_intercept_errors on;
error_page 404 =200 /ta.jpg;
access_log /app/logs/bbs_access.log commonlog;
}
}
范例4:错误状态码URL重定向,命令如下:
server {
listen 80;
server_name www.maotai.com;
location / {
root html/www;
index index.html index.htm;
error_page 404 https://clsn.cnblogs.com;
#<==当出现404错误时,会跳转到指定的URL https://clsn.cnblogs.com页面显示给用户,这个URL一般是企业另外的可用地址
access_log /app/logs/bbs_access.log commonlog;
}
}
[nginx]编译安装及安全优化的更多相关文章
- Linux下nginx编译安装教程和编译参数详解
这篇文章主要介绍了Linux下nginx编译安装教程和编译参数详解,需要的朋友可以参考下 一.必要软件准备1.安装pcre 为了支持rewrite功能,我们需要安装pcre 复制代码代码如下: # y ...
- nginx编译安装
Nginx编译安装 1.nginx官网:http://wiki.nginx.org/Install下载:http://nginx.org/en/download.html 2.编译安装# wget h ...
- LNMP平台搭建之一:nginx编译安装
参考博客:https://www.cnblogs.com/zhang-shijie/p/5294162.html jack.zhang 一.环境说明 系统环境:centos6.5 [root@lo ...
- Nginx编译安装lua-nginx-module
lua-nginx-module 模块可以将Lua的强大功能嵌入NGINX服务器. 下载Nginx源码 如果已安装Nginx,需要查看当前安装版本的编译参数: $ /usr/local/nginx/s ...
- Nginx编译安装:
第三方模块 在nginx.org -------- wiki 找 --add-module= 添加 Nginx编译安装: 安装开发环境 ]# yum groupinstall " ...
- 20190418 CentOS7实用技能综合:系统安装 + WinScp客户端连接 + 防火墙端口号iptables + Nginx编译安装 + MySQL编译安装 + Redis编译安装 + MongoDB编译安装 + ActiveMQ/RocketMQ/RabbitMQ编译安装 + ...各类常用生产环境软件的编译安装
系统安装 + WinScp客户端连接 + 防火墙端口号iptables + Nginx编译安装 + MySQL编译安装 + Redis编译安装 + MongoDB编译安装 + ActiveMQ/Roc ...
- Nginx编译安装第三方模块http_substitutions_filter_module2222
Nginx编译安装第三方模块http_substitutions_filter_module Rming -- 阅读 安装 Http 编译 module filter nginx 模块 >> ...
- Nginx编译安装第三方模块http_substitutions_filter_module
Nginx编译安装第三方模块http_substitutions_filter_module 分类:服务器技术 作者:rming 时间:-- . >>ngx_http_substitu ...
- nginx编译安装新模块
nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 这里以安装第三方ngx_http_google_filter_module模块为例 下载第三方扩展模块ngx_ht ...
随机推荐
- V-rep学习笔记:Reflexxes Motion Library 1
V-REP中集成了在线运动轨迹生成库Reflexxes Motion Library Type IV,目前Reflexxes公司已经被谷歌收购.(The Reflexxes Motion Librar ...
- V-rep学习笔记:碰撞检测与距离计算
V-REP可以在几何组件中快速判断各种干扰与碰撞,以及计算多个组件间的最小距离. 碰撞检测 V-REP可以检测两个碰撞体实体(Collidable objects are objects that c ...
- CString与UTF8互转代码
这个代码网上很多,留在这里做个备份. static std::string ConvertCStringToUTF8( CString strValue ) { std::wstring wbuffe ...
- 转error while loading shared libraries的解決方法
error while loading shared libraries的解決方法 者 icq 21:03 | 靜態連結網址 | 迴響 (0) | 引用 (1) | 點閱次數 (270) | Prog ...
- code vs 3492 细胞个数
题目链接:http://codevs.cn/problem/3492/ 题目描述 Description 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为 ...
- 微信小程序支付源码,后台服务端代码
作者:尹华南,来自原文地址 微信小程序支付绕坑指南 步骤 A:小程序向服务端发送商品详情.金额.openid B:服务端向微信统一下单 C:服务器收到返回信息二次签名发回给小程序 D:小程序发起支付 ...
- Linux下的二进制兼容性的检测
都知道Linux的二进制兼容与windows相比简直是天差地别,windows的二进制兼容做得特别好,很老的游戏都能在windows 10上跑,写的好的程序,在XP上也不是不可以.但是Linux就不一 ...
- javascript 的 jasmine 的測试语句
首先建立环境场景: 一般三个文件夹 lib jasmine的系统文件存放文件夹 spec 写測试用例的文件夹 src 存放源码的文件夹(被測对象) specRunner.html 測试入口文件. 入口 ...
- 在string资源中添加变量
public void checkModeByAction() { if(TxrjConstant.ACTION_ADD_RECIPIENT.equals(getIntent().getAct ...
- Xcode使用小技巧-filter查找功能和查看最近修改的文件
今天偶然发现了关于Xcode的一个小技巧: 1.查看最近修改的文件 2.使用filter查找制定文件 没错,就是下面这个东西,很容易忽略的一个小工具,在Xcode左下角位置. 通过这个,我们能够在整个 ...