nginx的一般安全配置

更新日期: 2019-07-03阅读: 1.6k标签: nginx

项目一般配置,例如default.conf

server {
    #引入lua文件加载一些默认配置
    include conf.d/lua_core.conf;
    #引入一些默认localtion的配置
    include conf.d/limits.conf;

    #指定dcoment_root
    root /var/www/html/default/public;

    #加载项目相关的localtion的配置
    include conf.d/rewrite/thinkphp.conf;

    #测试用
    location = /lua {
        set $loggable 0;
        default_type text/html;
        content_by_lua 'ngx.say("hello world")';
        #这个链接必须是在浏览器下访问,受制于此文件内的配置
        include conf.d/lua/must_be_human.conf;
    }

    access_log /var/log/nginx/access.default.log json_log buffer=64k flush=10s if=$loggable;
    error_log /var/log/nginx/error.default.log error;
}


nginx的主入口nginx.conf

user  nginx;
worker_processes  auto;

pid /var/run/nginx.pid;
error_log  /var/log/nginx/nginx_error.log warn;

worker_rlimit_nofile 51200;

events {
    use epoll;
    worker_connections 51200;
    multi_accept on;
}

http  {
    include /etc/nginx/mime.types;
    default_type  application/octet-stream;
    
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
    client_body_buffer_size 5m;
    client_body_temp_path /var/cache/nginx/client_body_temp 1 2;
    
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    client_body_timeout 30;
    client_header_timeout 30;
    keepalive_timeout 30;
    send_timeout 30;
    
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 1m;
    fastcgi_buffers 8 1m;
    fastcgi_busy_buffers_size 2m;
    fastcgi_temp_file_write_size 4m;
    fastcgi_temp_path /var/cache/nginx/fastcgi_temp 1 2;
    
    proxy_http_version 1.1;
    proxy_buffer_size 1m;
    proxy_buffers 4 1m;
    proxy_busy_buffers_size 2m;
    proxy_temp_file_write_size 2m;
    proxy_cache_path /var/cache/nginx/nginx_proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=10g;
    
    gzip on;
    gzip_min_length  1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/json;
    gzip_vary on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";
    
    server_tokens off;
    access_log off;
    autoindex off;
    
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    
    map $http_x_forwarded_for $clientRealIp {
        ""  $remote_addr;
        ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
    }
    
    map $sent_http_content_type $expires {
        default                    1d;
        text/html                  off;
        text/css                   1d;
        application/javascript     1d;
        ~image/                    1d;
    }
    
    map $host $resp_body {
        default "";
    }
    
    map $host $loggable {
        default 1;
    }
    
    map $host $request_body_sub {
        default "";
    }
    
    #日志采用json形式,方便和jq命令查找分析
    log_format json_log escape=json '{"realip":"$clientRealIp","timestamp":"$time_iso8601","host":"$http_host","request":"$request","req_body":"$request_body_sub","status":"$status","resp_body":"$resp_body","size":$body_bytes_sent,"ua":"$http_user_agent","cookie":"$http_cookie","req_time":"$request_time","uri":"$uri","referer":"$http_referer","xff":"$http_x_forwarded_for","ups_status":"$upstream_status","ups_addr":"$upstream_addr","ups_time":"$upstream_response_time"}';
    
    lua_need_request_body on;
    #不关闭这个,不能启动lua module
    lua_load_resty_core off;
    
    include /etc/nginx/conf.d/vhost/*.conf;
}

lua_core.conf中的一些内容
#将输入输出也写进log中
body_filter_by_lua_block {
    if ngx.var.request_method == "POST" then
        ngx.var.request_body_sub = string.sub(ngx.var.request_body or "", 1, 1000)

        local resp_body = string.sub(ngx.arg[1] or "", 1, 1000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        if ngx.arg[2] then
            ngx.var.resp_body = ngx.ctx.buffered
        end
    end
}

#放在顶级server中进行筛选
rewrite_by_lua_block {
    local body = nil
    if ngx.var.request_method == "POST" then
        ngx.req.read_body()
        body = ngx.unescape_uri(string.sub(ngx.var.request_body or "", 1, 1000))
    elseif ngx.var.request_method == "GET" then
        body = ngx.unescape_uri(string.sub(ngx.var.args or "", 1, 1000))
    end
    
    -- 检测请求数据中的不安全参数
    if body and string.len(body) >= 8 then
        local patterns = {
            "(shell_exec|phpinfo|system|passthru|preg_\\w+|execute|echo)\\s*\\(",
        }

        for _, pattern in pairs(patterns) do
            local match = ngx.re.match(body, pattern, "ijo")
            if match then
                ngx.status = 200
                ngx.say("hello world")
                ngx.exit(200)
            end
        end
    end
}


limits.conf

#https的时候要用到
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header strict-transport-security "max-age=31536000; includeSubdomains";

fastcgi_hide_header X-Powered-By;

#只允许特定的METHOD
if ($request_method !~ ^(GET|POST|HEAD|OPTION)$) {
    return 405;
}

#禁止默认的命令行工具访问
if ($http_user_agent ~* (pytho[n]?|curl|wget)) {
    return 403;
}

# 防止外部直接thinkphp漏洞攻击
if ($request_uri ~* ^/index\.php) {
    return 405;
}

#letsencrypt需要访问这个地址下文件
location ^~ /.well-known {
    try_files $uri $uri/ =404;
    access_log off;
}

#禁止所以点开头的访问
#eg: /upload/../index.php
location ~ /\. {
    deny all;
}

#upload下php无运行权限,防止上传漏洞
location ~* /upload[s]?/.*\.php$ {
    return 404;
}

#静态文件就不需要记录在日志了
location ~* \.(map|gif|jpg|png|css|js|ico|swf|pdf|apk|exe|eot|otf|ttf|woff|woff2)$ {
    try_files $uri =404;
    access_log off;
}

location = /favicon.ico {
    try_files $uri =404;
    access_log off;
}


lua的一些应用

#一般爬虫无法动态cookie,用作判断是否是浏览器行为
rewrite_by_lua_block {
    local random = ngx.var.cookie_random
    if(random == nil) then
        random = math.random(999999)
    end

    local token = ngx.md5("salt" .. ngx.var.remote_addr .. random)
    if (ngx.var.cookie_token ~= token) then
        ngx.header["Set-Cookie"] = {"token=" .. token, "random=" .. random}
        return ngx.redirect(ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.request_uri)
    end
}

thinkphp.conf相关的配置

location / {
    try_files $uri $uri/ /index.php?s=$uri&$query_string;
}

#杜绝其他上传php文件的漏洞
location = /index.php {
    try_files $uri =404;
    fastcgi_pass   php-fpm:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php; # $fastcgi_script_name
    include        fastcgi_params;
}

链接: https://www.fly63.com/article/detial/4492

Nginx常用配置--nginx之proxy_pass代理后端https请求完全解析

前提条件:nginx源码或nginx plus源码、一个代理服务器或一个代理服务器组、SSL证书和私钥,你可以从一个可信任证书颁发机构(CA)购买一个服务器证书,或者你可以使用openssl库创建一个内部CA签名

nginx:支持https

查看nginx模块,如果看到with-ssl那就是有的。注册ssl证书并下载,配置nginx就比如说,还没有配置https前你配置了80,那么你http://域名/直接默认访问80端口,那么一样的

nginx做http向https的自动跳转

首先让nginx服务器监听两个端口,分别是80端口和443端口,注意监听443端口的时候需要配置证书的认证以及创建自签名证书!关于证书的认证的以及创建自签名的证书,nginx的配置如下,只给出了两个server的配置,可以直接复制到http块中。

Nginx配置https和wss

常见的服务器有三种:Nginx、IIS、Apache,都可以配置https,但是没必要全部知道,因为Nginx可以起到反向代理的作用,会配置Nginx就足够了。在/etc/nginx/conf.d目录下新建https.conf

nginx加速_开启Gzip/文件做缓存

开启Gzip:给Nginx上 ngx_http_gzip_module 这个模块,用 nginx -V 命令查看 configure arguments 是否有,没有的话需要编译加载这个模块;给文件做缓存:图片文件,字体文件,js和css都是些可以用来缓存的文件

nginx和php-fpm的进程启停重载总结

ginx和php-fpm对于-USR2、-HUP信号的处理方式不一样:TERM, INT(快速退出,当前的请求不执行完成就退出),QUIT (优雅退出,执行完当前的请求后退出)

Nginx解析PHP的原理 | CGI、FastCGI及php-fpm的关系

php-fpm采用master/worker架构设计, master进程负责CGI、PHP公共环境的初始化及事件监听操作。worker进程负责请求的处理功能。在worker进程处理请求时,无需再次初始化PHP运行环境,这也是php-fpm性能优异的原因之一

Nginx 禁止某 IP 访问

总有一些不怀好意的人来访问我的网站,而且频率还很高,所以就用简单的方式禁止访问,就用 Nginx 来实现。想要添加黑名单,只要在 blocksip.conf 中添加 IP ,然后 reload 即可。

Nginx服务器 之反向代理与负载均衡

客户端就可以通过请求代理服务器,获取想要的资源,但客户端并不知道给他资源的是哪个服务器。这种方式就是反向代理。当一台服务器的单位时间内的访问量越大的时候,服务器的压力会越大。我们通常通过负载均衡的方式来分担服务器的压力。

前端如何通过Nginx代理做到跨域访问API接口

Nginx作为反向代理服务器,就是把http请求转发到另一个或者一些服务器上。通过把本地一个url前缀映射到要跨域访问的web服务器上,就可以实现跨域访问。对于浏览器来说,访问的就是同源服务器上的一个url

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!