nginx 配置的一些參數


/etc/nginx/nginx.conf


worker_rlimit_nofile#;      --指定一個worker 進程所能打開的最大文件描述符數量
worker_rlimit_sigpending#;    --指定每個用戶能夠發往進程的信號的數量


性能優化相關的配置
  1.work_processes      --worker 進程的個數,通常為物理CPU核心數量減1,可以設為auto,實現自動設定
  2.worker_cpu_affinity CPUMASK CPUMASK...;
          0001
          0010
          0100
          1000
      worker_cpu_affinity 00000001 00000010 00000100;
  3. worker_priority nice ; [-20,19]

調試,定位問題的配置
  1. daemon [off|on]      --是否以守護進程方式啟動nignx
  2. master_process on|off| --是否以master/worker 模型運行nignx
  3. error_log /PATH/TO/ERROR_LOG level --錯誤日志文件以及級別, 出於調試的需要,可以設定為debug,但debug僅在編譯時使用了"--with-debug"選項才有效

event{}
  1.worker_connections#; 每個worker進程所能夠響應的最大並發請求數量=worker_processer*work_connections
  2.use [epoll|rgsig|select|poll];定義使用的事件模型;建議讓nginx 自動選擇
  3.accept_mutex[on|off];  各worker 接收用戶的請求的負載均衡鎖,啟用時,表示用於多個worker輪流地,序列化的響應新請求;
  4.lock_file /PATH/TO/LOCK_FILE;

增加nginx path到環境變量
cat /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH

. /etc/profile.d/nginx.sh
mkdir -pv /data/www/vhost2
nginx -s reload    -------> nginx 重起
ss -tnl     ------->查看端口是否打開

主機相關的指令
1.server {}
 定義虛擬主機
 server{
      listen PORT;                   --基於port:listen 指令監聽在不同的端口;
      server_name NAME;            --基於hostname server_name  指令指向在不同的主機名;
      root /path/to/documentroot;
   }
 
2.listen
    listen address[:port]{default_sever}[ssl][http2|spdy]
    listen port{default_sever}[ssl][http2|spdy]
    default_sever :設置默認虛擬主機用於基於IP地址,或使用了任意不能對應任何一個server的name時所返回站點;
    ssl:用於限制只能通過SSL連接提供服務
    spdy:SPDY protocol (SPEEDY),在編譯了spdy模塊的情況下,用於支持SPDY協議
    htpp2: http version 2;
3. server_name NAME [...];
    后可以跟多個主機名,可以使用通配符和正則表達式
    (1) 首先精匹配
    (2) 左側通配符  *.doufupi.com
    (3) 右側通配符  www.doufupi.*
    (4)    正則        ~^,*\.doufupi\.com$
    (5) default_sever
4. tcp_nodelay on|off;    對於keepalive模式下的連接是否使用TCP_NODELAY選項
5. tcp_nopush on|off;    是否啟用TCP_NOPUSH(FREEBSE)或TCP_CORK(linux)選項,僅在sendfile 為on時有用;
6. sendfile on|off;    是否啟用sendfile 功能

路徑相關的指令
7. root
    設置web資源的路徑映射;用於指明請求的URL所對應的文檔目錄路徑;
    server{
        ...
        root /data/www/host1;
    }
    http:www.doufupi.com/images/IMEG345.jpg-->/data/www/host1/images/IMEG345.jpg
    server{
        ...
        server name wwww.doufupi.com;
        location /images/{
        root /data/imgs/;
        }
    }
    http:www.doufupi.com/images/IMEG345.jpg-->/data/imgs/images/IMEG345.jpg
8. location [=|-|~*|^-] url {...}
    location @name {...}
    功能:允許根據用戶請求的URL來匹配定義的各個location,匹配時,此請求將被相應的location塊中的配置所處理;簡言之,即用於為需要用到專用配置的URL提供特定配置;
    = :URL的精確匹配
    - : 做正則表達式匹配,區分字符大小寫
    -*: 做正則表達式匹配,不區分字符大小寫
    ^-: URL左半部分匹配,不區分字符大小寫
    匹配優先級: = > ^- > -/-*> 不帶符號的URL


9. alias
    定義路徑別名,只能用用location配置段,定義路徑別名
    location /images/ {
        root /data/imgs/;
    }
    location /images/ {
        alias /data/imgs/;
    }
    
    注意:
        root指令:路徑為對應的location的"/"的這個URL:      /images/IMEG345.jpg -->/data/imgs/images/IMEG345.jpg
        alias指令:路徑為對應的location的"/url/"的這個URL: /images/IMEG345.jpg -->/data/imgs/IMEG345.jpg


10. index 指網站默認指定的主頁
    可以根據不同的IP 給定不同的主頁
    誰的使用范圍小,誰的優先級別高
    
11. error_page code...[=[response]] url;
    根據http的狀態碼重定向錯誤頁面;
    error_page 404=200 /404.html; (以指定的響應碼進行響應)


12. try_file
    try_file file ... =code; 嘗試查找第1至第N-1個文件,第一個是為返回給請求者的資源,若1至N-1文件都不存在,則跳轉到最后一個URL(
    必須不能匹配到當前LOCATION,而應該匹配至其它LOCATION,否則會導致死循環);
    
用戶請求相關的配置:
13.keeplive_timeout            設定keeplive連接的超時時長;0 表示禁止長連接
14. keepalive_requests number;     在keepalive 連接上所允許請求的最大資源數量 ,default 100;
15. keepalive_disable non|browser ...;   指明禁止為何種瀏覽器使用keepalive功能
16. send_timeout#            發送報文的超時時長,deafaule 60s
17. client_body_buffer_size size     接收客戶請求報文body的緩沖區大小,deafaule16K,超出此指定大小時,其將存於磁盤上
18. client_body_temp_path path [level [level2 [level3]]];\   用於存儲客戶端請求body的臨時存儲路徑及子目錄結構和數量
    client_body_temp_path  /var/tmp/client_body 2 2;

對客戶端請求的進行限制
19. limit_excepte METHOD {...}
20. limit_rate_speed;      限制客戶端每秒種所能傳輸的字節數,default 0 表示無限制

文件操作優化相關的配置
21. aio on|off
22. directio on|off
23. open_file_cache
    open_file_cache max=N[inactive=time];
        nginx可以緩存以下三種信息:
        (1) 文件描述符,文件大小和最近一次修改時間;
        (2) 打開的目錄結構
        (3)沒有找到的或者沒有權限操作文件的相關信息
        max=N 可以緩存的最大條目上限,一旦達到上限,則會使用LRU算法從緩存中刪除最近最少使用的緩存
        inactive=time 在此處指定的時長內沒有被訪問過的緩存是為非活動緩存項,因此直接刪除


24.open_file_cache_errors on|off`      是否緩存找不到其文件,或者沒有權限訪問的文件相關信息
25.open_file_cache_valid time:     每隔多久檢查一次緩存中緩存項的有效性 deafaule 60S
26. open_file_cache_min_user number   緩存項在非活動期限內最少應該被訪問的次數
    
27.allow address |CDIR|unix|all;
28.deny address |CDIR|unix|all;
    location ~ ^/1.html {
    root /soft/code;
    index index.html;
    deny 61.142.20.34;          #deny主指定iP
    allow all;
    }
    
    location ~ ^/admin {
    root /soft/code;
    index index.html;
    allow 61.142.20.34;         #allow指定iP
    deny  all;          
    }

    //配置拒絕某⼀一個IP, 其他全部允許
    location ~ ^/1.html {
        root /usr/share/nginx/html;
        index index.html;
        deny 192.168.56.1;
        allow all;
        }
    //只允許某⼀一個⽹網段訪問,其它全部拒絕
    location / {
        root html;
        index index.php index.html index.htm;
        allow 192.168.56.0/24;
        deny all;
        }

nginx_http
htpasswd -c -m /etc/nginx/.ngxhtpasswd jerrry
htpasswd -c -m /etc/nginx/auth_conf jerrry
htpasswd -b -m /etc/nginx/auth_conf mark #新增
htpasswd  -m /etc/nginx/.ngxhtpasswd jerrry
29.auth_basic string |off $        使用http basic 對用戶 需要安裝包httpd-tools
30. auth_basic_user_file file;
    文件格式 :
        name1:password1
        name1:password2:comment
    密碼格式:
    (1)encrypted with crypt function;
    (2)md5加密;
       
    location /admin/ {
        auth_basic "Admin Area";
        auth_basic_user_file /etc/nginx/.ngxhtpasswd;
    }
31. log_format
32. access_log path[ format ]       access_log off;
33. open_lof_file_cache max=N

   open_lof_file_cache off;

  ngx_http_stub_status


34.stub_status:
  通過指定url輸出stub status;
      location /status/ {
          auth_status;
      }
  ngx_http_referer_module模塊配置(基於請求報文中的Referer首部的值做訪問控制)
35. valid_refers none|blocked|server_name|string ...;
    none:
    blocked:
    arbitrary string:
    regular expression:

36.limit_req_zone
    limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
    server {
    listen  80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        #limit_req zone=req_zone;
        limit_req zone=req_zone burst=3 nodelay;
        }
    }

37.limit_conn_zone
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    server {
    listen  80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        #limit_req zone=req_zone;
        limit_conn_zone zone=conn_zone 1;
        }
    }
連接限制沒有請求限制有效?
我們前⾯面說過, 多個請求可以建⽴立在⼀一次的TCP連接之上, 那么我們對請求的精度限制,當然⽐比對⼀一個連接的限制會更更加的有效。
因為同⼀一時刻只允許⼀一個連接請求進⼊入。但是同⼀一時刻多個請求可以通過⼀一個連接進⼊入。所以請求限制才是⽐比較優的解決⽅方案。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM