[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 ...
 
随机推荐
- Artistic Style在windows下的使用(C/C++)
			
ArtisticStyle是一个开源的源码格式化工具.主页地址为:http://astyle.sourceforge.net/,它能够应用在C.C++.Objective-C.C#.Java等程序语言 ...
 - excel文件批量重命名
			
1.创建bat文件 2.在文件内输入以下格式的内容并保存,注意期间有空格 ren 1.txt 0011.txt ren 2.txt 0021.txt ren 3.txt 0031.tx ...
 - django之创建第4-2个项目-访问class类属性和类方法
			
1.修改index <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
 - Web工程中各类地址的写法
			
1)总体原则 在java web开发中,只要是url地址,那么最好以“/”开头,也就是绝对路径的方式.那么这个“/”到底代表什么呢? 如果“/”是给服务器用的,则代表当前web工程:如果是给浏览器用的 ...
 - Appium原理及版本变化细节
			
Appium原理小结 Api接口调用selenium的接口,Android底层用android的instrumentation(API2.3+ 通过绑定另外一个独立的selendroid项目来实现的) ...
 - UVA - 10298 Power Strings (KMP求字符串循环节)
			
Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...
 - adjustResize和adjustPan的比较
			
在下面的描述中,编辑框的maxLine都设定为10. 在信息列表界面中,编辑框在RelativeLayout中定义.编辑框上边(above)的列表组件的高度不会缩小为0,导致显示出现一点问题. 当信息 ...
 - 【原创+亲测可用】JS如何区分微信浏览器、QQ浏览器和QQ内置浏览器
			
1.原理: 通过不同移动端的ua弹窗 获取user-agent 参数包含的信息,进行判断浏览器类型 在Android上 QQ内置环境的ua中有关键字 MQQBrowser, 并且后面包含一个[空白符+ ...
 - K均值算法
			
为了便于可视化,样本数据为随机生成的二维样本点. from matplotlib import pyplot as plt import numpy as np import random def k ...
 - lu协程练习
			
生产者和消费者问题:当协程调用yield时,从一个悬而未决的resume中返回.简单的协程练习: function receive() local status,value = coroutine.r ...
 
			
		