Nginx-架構篇

Nginx-架構篇

1、Nginx常見問題

1. 相同server_name多個虛擬主機優先級訪問

# 三個配置文件:

# 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,訪問的優先級是按照服務器的讀取順序,即文件名的排序。

2. location匹配優先級

  • =:進行普通字符精確匹配,也就是徹底匹配
  • ^~:表示普通字符匹配,使用前綴匹配
  • ~ ~ :表示執行一個正則匹配(加不區分大小寫)
上述優先級自上而下優先級下降,前兩個匹配是精確匹配,匹配到之後就再也不往下找,正則匹配匹配到相應的字符串也會繼續往下尋找是否有更精確的匹配。

3. Nginx的try_files的使用

按順序檢查文件是否存在
# 先檢查對應的url地址下的文件存不存在,若是不存在找/index.php,相似於重定向
location / {
    try_file $uri /index.php;
}

4. Nginx的alias和root區別

  • root
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
  • alias
location /request_path/image/ {
    alias /local_path/image/;
}
# 請求:http://www.rona1do.top/request_path/image/cat.png
# 查詢: /local_path/image/cat.png

5. 用什麼樣的方法傳遞用戶的真實IP地址

  • 在有代理的狀況下,remote_addr獲取的是代理的ip,不是用戶的ip
  • x-forwarded-for容易被篡改
通用解決辦法: 能夠跟第一級代理協商,設置頭信息x_real_ip記錄用戶的ip
set x_real_ip=$remote_addr

6. Nginx中常見錯誤碼

  • 413:request entity too largephp

    • 用戶上傳文件限制:client_max_body_size
  • 502:bad gatewaynginx

    • 後端服務無響應
  • 504:gateway time-outgit

    • 後端服務超時

2、Nginx性能優化

1. 性能優化考慮點

  • 當前系統結構瓶頸github

    • 觀察指標(top查看狀態、日誌等)、壓力測試
  • 瞭解業務模式sql

    • 接口業務類型,系統層次化結構
  • 性能與安全shell

    • 配置防火牆太過於注重安全,會下降性能

2. ab接口壓力測試工具

  1. 安裝vim

    • yum install httpd-tools
  2. 使用後端

    • ab -n 2000 -c 2 http://127.0.0.1/
    • -n:總的請求數
    • -c:併發數
    • -k:是否開啓長鏈接

3. 系統與Nginx性能優化

  1. 文件句柄安全

    • LinuxUnix一塊兒皆文件,文件句柄就是一個索引
  2. 設置方式性能優化

    - 系統全局性修改、用戶局部性修改、進程局部性修改
系統全局性修改 和 用戶局部性修改:
配置文件: /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;

4. CPU的親和

CPU親和:將進程/線程與cpu綁定,最直觀的好處就是提升了cpu cache的命中率,從而減小內存訪問損耗,提升程序的速度。
  • 物理CPU數量:cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
  • CPU核心: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

5. Nginx通用配置優化

# 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;
}

3、Nginx安全

1. 常見的惡意行爲

  • 爬蟲行爲和惡意抓取、資源盜用

    • 基礎防盜鏈功能,不讓惡意用戶能輕易的爬取網站對外數據
    • secure_link_module,對數據安全性提升加密驗證和實效性,適合如核心重要數據
    • acces_module,對後臺、部分用戶服務的數據提供IP防控

2. 常見的攻擊手段

  • 後臺密碼撞庫,經過猜想密碼字典不斷對後臺系統嘗試性登陸,獲取後臺登陸密碼

    • 後臺登陸密碼複雜度
    • access_module,對後臺提供IP防控
    • 預警機制(一個IP在一段時間內重複不斷請求等)

3. 文件上傳漏洞

利用一些能夠上傳的接口將惡意代碼植入到服務器中,再經過url去訪問以執行代碼
# 文件上傳漏洞解決辦法
location ^~ /upload {
    root /opt/app/images;
    if ($request_file ~* (.*)\.php){
        return 403;
    }
}

4. SQL注入

利用未過濾/未審覈用戶輸入的攻擊方法,讓應用運行本不該該運行的SQL代碼
  • Nginx+LUA配置WAF防火牆防止SQL注入

圖片描述

使用waf步驟:

  1. git clone https://github.com/loveshell/ngx_lua_waf.git
  2. cd ngx_lua_waf
  3. mv ngx_lua_waf /etc/nginx/waf
  4. vim /etc/nginx/waf/conf.lua,修改RulePath爲對應路徑(/etc/nginx/waf/wafconf)
  5. vim /etc/nginx/waf/wafconf/post,加入一行,\sor\s+,放sql注入的正則
  6. 集成waf:
# /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
  1. reload Nginx

5. 複雜的訪問攻擊中CC攻擊

  • waf/conf.lua配置文件中打開防cc攻擊配置項

    • CCDeny="on"
    • CCrate="100/60" #每60秒100次請求

4、Nginx總結

  1. 定義Nginx在服務體系中的角色

    • 靜態資源服務
    • 圖片描述
    • 代理服務
    • 圖片描述
    • 動靜分離
  2. 設計評估

    • 硬件

      • CPU、內存、硬盤
    • 系統

      • 用戶權限、日誌目錄存放
    • 關聯服務

      • LVS、keepalive、syslog、Fastcgi
  3. 配置注意事項

    • 合理配置
    • 瞭解原理(HTTP、操做系統...)
    • 關注日誌
相關文章
相關標籤/搜索