# 三個配置文件: # testserver1: server_name testserver1 www.rona1do.top; root /opt/app/code1; # testserver2: server_name testserver2 www.rona1do.top; root /opt/app/code2; # testserver3: server_name testserver3 www.rona1do.top; root /opt/app/code3;
配置上述相同server_name的三個虛擬主機,會先訪問testserver1,訪問的優先級是按照服務器的讀取順序,即文件名的排序。
上述優先級自上而下優先級下降,前兩個匹配是精確匹配,匹配到之後就再也不往下找,正則匹配匹配到相應的字符串也會繼續往下尋找是否有更精確的匹配。
按順序檢查文件是否存在
# 先檢查對應的url地址下的文件存不存在,若是不存在找/index.php,相似於重定向 location / { try_file $uri /index.php; }
location /request_path/image/ { root /local_path/image/; } # 請求:http://www.rona1do.top/request_path/image/cat.png # 查詢: /local_path/image/request_path_image/cat.png
location /request_path/image/ { alias /local_path/image/; } # 請求:http://www.rona1do.top/request_path/image/cat.png # 查詢: /local_path/image/cat.png
通用解決辦法: 能夠跟第一級代理協商,設置頭信息x_real_ip記錄用戶的ip
set x_real_ip=$remote_addr
413:request entity too largephp
502:bad gatewaynginx
504:gateway time-outgit
當前系統結構瓶頸github
瞭解業務模式sql
性能與安全shell
安裝vim
yum install httpd-tools
使用後端
文件句柄安全
設置方式性能優化
- 系統全局性修改、用戶局部性修改、進程局部性修改
系統全局性修改 和 用戶局部性修改:
配置文件:/etc/security/limits.conf
# root:root用戶 root soft nofile 65535 # hard 強制限制、soft 超過會發送提醒(郵件等),不限制 root hard nofile 65535 # *:全部用戶 * soft nofile 65535 * hard nofile 65535
進程局部性修改
配置文件:/etc/nginx/nginx.conf
# 針對nginx進程進行設置 worker_rlimit_nofile 35535;
CPU親和:將進程/線程與cpu綁定,最直觀的好處就是提升了cpu cache的命中率,從而減小內存訪問損耗,提升程序的速度。
cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
cat /proc/cpuinfo | grep "cpu cores" | uniq
top
,再按1
# /etc/nginx/nginx.conf # nginx建議數量跟cpu核心數保持一致 worker_processes 2; # 配置cpu親和 worker_cpu_affinity 0000000000000001 0000000000000010 # 與上一行等價,自動對應(Nginx1.9版本以上) worker_cpu_affinity auto
查看Nginx的cpu綁定狀況:ps -eo pid,args,psr | grep [n]ginx
# nginx服務使用nginx用戶(最好不要使用root用戶) user nginx; # cpu親和(最好跟核心數保持一致) worker_processes 2; worker_cpu_affinity auto; # error的日誌級別設置爲warn error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; # 文件句柄對於進程間的限制(建議1w以上) worker_rlimit_nofile 35535; # 事件驅動器 events { use epoll; # 限制每個worker_processes進程能夠處理多少個鏈接 worker_connections 10240; } http { include /etc/nginx/mime.types; default_type application/octet-stream; #字符集(服務端響應發送的報文字符集) charset utf-8; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; # 靜態資源的處理 sendfile on; #tcp_nopush on; keepalive_timeout 65; # gzip壓縮(對於IE6或如下版本對於gzip壓縮支持不是很好) gzip on; # IE6或如下不進行壓縮(兼容) gzip_disable "MSIE [1-6]\."; gzip_http_version 1.1; include /etc/nginx/conf.d/*.conf; }
爬蟲行爲和惡意抓取、資源盜用
後臺密碼撞庫,經過猜想密碼字典不斷對後臺系統嘗試性登陸,獲取後臺登陸密碼
利用一些能夠上傳的接口將惡意代碼植入到服務器中,再經過url去訪問以執行代碼
# 文件上傳漏洞解決辦法 location ^~ /upload { root /opt/app/images; if ($request_file ~* (.*)\.php){ return 403; } }
利用未過濾/未審覈用戶輸入的攻擊方法,讓應用運行本不該該運行的SQL代碼
使用waf步驟:
git clone https://github.com/loveshell/ngx_lua_waf.git
cd ngx_lua_waf
mv ngx_lua_waf /etc/nginx/waf
vim /etc/nginx/waf/conf.lua
,修改RulePath爲對應路徑(/etc/nginx/waf/wafconf)vim /etc/nginx/waf/wafconf/post
,加入一行,\sor\s+
,放sql注入的正則# /etc/nginx/nginx.conf lua_package_path "/etc/nginx/waf/?.lua"; lua_shared_dict limit 10m; init_by_lua_file /etc/nginx/waf/init.lua; access_by_lua_file /etc/nginx/waf/waf.lua
waf/conf.lua
配置文件中打開防cc攻擊配置項
CCDeny="on"
CCrate="100/60" #每60秒100次請求
定義Nginx在服務體系中的角色
設計評估
硬件
系統
關聯服務
配置注意事項