zoukankan      html  css  js  c++  java
  • [nginx]编译安装及安全优化

    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做了改善.

    1. 根据HTTP referer实现防盗链, 利用referer,并且针对扩展名rewrite重定向,下面的代码为利用referer且针对扩展名rewrite重定向,即实现防盗链的Nginx配置。
    2. 根据cookie防盗链
    3. 通过加密变换访问路径实现防盗链
    4. 在所有网站资源上添加网站信息,让盗链人员帮你做推广宣传
    • 设置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;
            }
        }
    
  • 相关阅读:
    软件测试人员的年终绩效考核怎么应对
    收藏
    顶踩组件 前后两版
    订阅组件
    hdu 1963 Investment 完全背包
    hdu 4939 Stupid Tower Defense 动态规划
    hdu 4405 Aeroplane chess 动态规划
    cf 414B Mashmokh and ACM 动态规划
    BUPT 202 Chocolate Machine 动态规划
    hdu 3853 LOOPS 动态规划
  • 原文地址:https://www.cnblogs.com/iiiiher/p/8550830.html
Copyright © 2011-2022 走看看