linux服務基礎之nginx配置詳解


nginx簡單介紹:https://www.cnblogs.com/ckh2014/p/10848670.html

nginx編譯安裝:https://www.cnblogs.com/ckh2014/p/10848623.html

nginxp配置文件:

main 配置段:全局配置段
events{}: 定義event模型工作特性
http {}: 定義http協議相關的配置

配置指令: 要以分號結尾,語法格式:
  directive value1 [value2...]

支持使用變量:
  內置變量:
    模塊會提供內建變量定義
  自定義變量:
    set var_name value

主配置段的指令:
  用於調試、定位問題
  正常運行必備的配置
  優化性能的配置
  事件相關的配置

nginx相關配置:

主配置段的指令:

正常運行的必備配置

1. user USERNAME [GROUPNAME]  
      指定運行worker進程的用戶和組;
      比如:user nginx nginx;

2. pid /path/to/pid_file;
  指定nginx守護進程的pid文件:
    pid /var/run/nginx/nginx.pid;

3.worker_rlimit_nofile #;
  指定所有worker進程所能夠打開的最大文件句柄數;

性能優化相關的配置

1. worker_processes #
     worker進程的個數:通常應該略小於CPU物理核心數;

2. worker_cpu_affinity cpumask...;
    優點:提升緩存的命中率
    context switch:會產生CPU的不必要的消耗
    cpumask:
      0000 0001
      0000 0010
      0000 0100

   worker_cpu_affinity 00000001 00000010 00000100;

3. timer_resolution
  計時器解析度:降低此值,可減少gettimeofday()系統調用的次數;

4. worker_priority number;
  指明worker進程的nice值
    -20,19
    100,139

事件相關的配置:  

1. accept_mutex {off|on};
    master調度用戶請求至各worker進程時使用的負載均衡鎖;on表示能讓多個worker輪流的、序列化的去響應新請求;
2. lock_file;
  accept_mutex用到的鎖文件路徑;

3.use [epoll|rtsig|select|poll];
  指明使用的事件模型:建議讓Nginx自行選擇;

4. worker_connections #;
  設定單個worker進程所能夠處理的最大並發連接數量

  worker_connections * work_processes

用於調試、定位問題:

1. daemon {on|off};
  是否以守護進程方式運行nginx, 調試時應該設置為off

2. master_process {on|off};
  是否以master/worker模型來運行nginx;調試時可以設置為off

3. error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg];
    error_log 位置 級別;

    若要使用debug級別,需要在編譯nginx時使用--with-debug選項;

總結: 常需要進行調整的參數

worker_processes, worker_connections, worker_cpu_affinity, worker_priority

 新改動配置生效的方式:

nginx -s reload
  stop, quit, reopen

Nginx作為web服務器時使用的配置:

http {}: 由ngx_http_core_module模塊所引入;

配置框架:

http {
   upstream {
     ...
   }

   server {
     location URL {
       root "/path/to/somedir";
        ...
     } # 類似於httpd中的<Location>,用於定義URL與本地文件系統的映射關系;
     location URL {
        if ...{
          ...
        }
     }    } # 每個server類似於httpd中的一個<VirtualHost>;
   
   server {
     ...
   }
}

注意:與httpd相關的指令僅能放置於http、server/location、upstream、if上下文,但有些指令僅應用於這5種上下文中的某些種;

配置指令:

1. server {}
  定義一個虛擬主機;
  
示例:
  server {
    listen 8080;
    server_name www.alen.com;
    root "/vhosts/web1";
  }

2. listen
   指定監聽的地址和端口:
     listen address[:port];
     listen port;

3. server_name NAME [...];
  后可跟多個主機;名稱還可以使用正則表達式(~)或通配符;
  (1)先做精確匹配檢查:
  (2)左側通配符匹配檢查: *.alen.com
   (3) 右側通配符匹配檢查;如mail.*
   (4)正則表達式匹配檢查: 如~^.*\.alen\.com$
   (5) default_server;
  server {
    server_name www.alen.com;
  }
  server {
    server_name *.alen.com;
  }
  server {
    server_name mail.*;
  }

4. root path;
  設置資源路徑映射;用於指明請求的URL所對應的資源所在的文件系統上的起始路徑;

5. location [ = | ~* | ^~] uri {}
   location @name {}
   功能: 允許根據用戶請求的URI來匹配定義的各location;匹配到時,此請求將被相應的location配置塊中的配置所處理,例如
做訪問控制等功能
  =:精確匹配
  ~:正則表達式模式匹配檢查,區分字符大小寫;
  ~*:正則表達式模塊匹配檢查,不區分字符大小寫;
  ^~: URI的前半部分匹配,不檢查正則表達式

  匹配的優先級:精確匹配(=)、^~、~、~*、不帶任何符號的location

  server {
    listen 80;
    server_name www.alen.com;
    location / {
      root "/vhosts/web1";
    }
    
    location /images/ {
      root "/vhosts/images";
    }

    location ~* \.php$ {
      fcgipass
    }
  }

6. alias path;
  用於location配置段,定義路徑別名

  location /images/ {
    root "/vhosts/web1";
  }
  http://www.alen.com/images/a.jpg <-- /vhosts/web1/images/a.jpg

  location /images/ {
    alias "/www/pictures";
  }
  http://www.alen.com/images/a.jpg <-- /www/pictures/a.jpg
  
  注意: root表示指明路徑為對應的location "/" URL;alias表示路徑映射,即location指令后定義的URL是相對於alias所指明的路徑而言;

7.index file;
  默認主頁面;
    index index.php index.html;

8. error_page code [...] [=code] URI | @name
  根據http響應狀態碼來指明特用的錯誤頁面;

  error_page 404 /404_customed.html
  [=code]:以指定的響應嗎進行響應,而不是默認的原來的響應;默認表示以新資源的響應嗎為其響應嗎

9. 基於IP的訪問控制
  allow IP/Network;
  deny IP/Network;

10. 基於用戶的訪問控制
  basic, digest;

  auth_basic "";
  auth_basic_user_file ".PATH/TO/PASSWORD_FILE"
     賬號密碼文件建議使用htpasswd來創建

11. https服務

  生成私鑰,獲得證書簽署請求,並獲得證書;

  #server {
    # listen 443 ssl;
    # server_name localhost;
    # ssl_certificate cert.pem;
    # ssl_certificate_key cert.key;
    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 5m;
    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;
    # location / {
      # root html;
      # index index.html index.htm;
    # }
  #}

12. stub_status {on|off};
  僅能用於location上下文;

  location /status {
    stub_status on;
    allow 192.168.1.0/24;
    deny all;
  }
  
  結果示例:
   Active connections:6  # 當前所有處於打開狀態的連接數
   server accepts handled requests
    241 241 431  # 已經接受的連接,已經處理過的連接,已經處理過的請求數;在“保持連接”模式下,請求數量可能會多余連接數量
   Reading: 0 Writing: 1 Waiting: 5  
     # Reading: 正處於接收請求狀態的連接數;
     # Writing: 請求已經接收完成,正處於處理請求或響應的過程中的連接數
     # Waiting: 保持連接模式,且處於活動狀態的連接數

13. rewrite regex replacement flag;
    例如:
      rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
      
      http://www.alen.com/images/a/b/c/1.jpg --> /imgs/a/b/c/1.jpg
        
    flag:
      last: 此rewrite規則重寫完成后,不再被后面其他的rewrite規則進行處理;而是由User Agent重新對重寫后的URL再一次發起請求,並從頭開始執行類似的過程
      break:一旦此rewrite規則重寫完成后,由User Agent對新的URL重新發起請求,且不再會被當前loation內的任何rewrite規則檢查
      redirect:以302響應碼(臨時重定向)返回新的URL;
      permanent:以301響應碼(永久重定向)返回新的URL

14. if
  語法: if (condition) {...}
  應用環境: server, location
  
  condition:
    (1)變量名;
      變量值為空串,或者以“0”開始,則為false;其他均為true
    (2)以變量為操作數構成的比較表達式
      可使用=,!=類似的比較操作符進行測試
    (3)正則表達式的模式匹配操作
       ~: 區分大小寫的模式匹配檢查
       ~*: 不區分大小寫的模式匹配檢查
       !~和!~*:對上面兩種測試取反
    (4)測試路徑為文件可能性: -f, !-f
     (5) 測試指定路徑為目錄的可能性: -d, !-d
     (6)測試文件的存在性:-e,!-e
     (7)檢查文件是否有執行權限:-x,!-x
    
    例如:
      if ($http_user_agent ~* MSIE) {
        rewrite ^(.*)$ /msie/$1 break;
      }

15. 防盜鏈
   location ~* \.(jpg|gif|jpeg|png)$ {
     valid_referer none blocked www.alen.com;
     if ($invalid_referer){
       rewrite ^/ http://www.alen.com/403.html;
     }
   }

16. 定制訪問日志格式

    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 logs/access.log main;

  注意: 此處可用變量為nginx各模塊內建變量;

網絡連接相關的配置:

1. keepalive_timeout #;
    長連接的超時時長,默認為75s;

2.keepalive_requests #;
  在一個長連接上所能夠允許請求的最大資源數

3.keepalive_disable [msie6|safari|none];
  為指定類型的User Agent禁用長連接;

4. tcp_nodelay on|off;
  是否對長連接使用TCP_NODELAY選項;

5. client_header_timeout #;
  讀取http請求報文首部的超時時長;

6. client_body_timeout #;
  讀取http請求報文body部分的超時時長;

7. send_timeout #;
  發送響應報文的超時時長;

 fastcgi的相關配置:

LNMP:PHP啟用fpm模型;

 


免責聲明!

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



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