你們一塊兒討論討論呀javascript
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.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 }
#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; }