Nginx基礎

Nginx基礎

目錄

  • Nginx概述

  • Nginx的優點

  • Nginx相比Apache

  • 編譯安裝Nginx

  • Nginx配置文件講解

  • ngx_http_access_module模塊

  • ngx_http_auth_basic_module模塊

  • ngx_http_log_module模塊

  • ngx_http_stub_status_module模塊

Nginx概述

Engine X是一個高性能、高并發的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP服務器。由Igor Sysoev為俄羅斯訪問量第二的Rambler.ru站點開發的,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。

Nginx的優點

高并發:Nginx 是一個很強大的高性能Web和反向代理服務器,它具有很多非常優越的特性。在連接高并發的情況下,Nginx是Apache服務器不錯的替代品,能夠支持高達 50,000 個并發連接數的響應。 
負載均衡器·:Nginx作為負載均衡服務器:Nginx 既可以在內部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作為 HTTP代理服務器對外進行服務。 
代理服務器:Nginx本身就是一個反向代理服務器,可支持郵件服務器代理以及http代理

Nginx相比Apache

1.輕量級,同樣起web 服務,比apache 占用更少的內存及資源 
2.抗并發,nginx 處理請求是異步非阻塞的,而apache 則是阻塞型的,在高并發下nginx 能保持低資源低消耗高性能 
3.高度模塊化的設計,編寫模塊相對簡單 
4.配置簡潔易懂,正則配置讓很多事情變得簡單

編譯安裝Nginx

[root@centos7 ~]# yum -y groupinstall "Development Tools" "Server Platform Development"    # 安裝開發包組
[root@centos7 ~]# yum -y install pcre-devel openssl-devel zlib-devel   # 安裝依賴包
[root@centos7 ~]# useradd -r nginx  # 創建nginx系統用戶
[root@centos7 ~]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio   # 配置nginx
[root@centos7 ~]# make   # 編譯
[root@centos7 ~]# make install  # 安裝

Nginx配置文件講解

nginx配置文件組成:主配置文件nginx.conf,conf.d/*.conf; 
fastcgi,uwsgiscgi等協議相關的配置文件; 
mime.types:支持的mime類型

main段配置

正常運行必備的配置
   1、user  # 指定用于運行worker進程時的用戶
       Syntax: user user [group];
       Default:    user nobody nobody;
       Context:    main

   2、pid /PATH/TO/PID_FILE;    # 指定存儲nginx主進程進程號碼的文件路徑;
       Syntax: pid file;
       Default:    pid nginx.pid;
       Context:    main

   3、include file | mask;   # 指明包含進來的其它配置文件片斷;
       Syntax: include file | mask;
       Default:    —
       Context:    any

   4、load_module file;    # 指明要裝載的動態模塊;
       Syntax: load_module file;
       Default:    —
       Context:    main                    

性能優化相關的配置:
   1、worker_processes number | auto; # worker進程的數量;通常應該為當前主機的cpu的物理核心數;
       Syntax: worker_processes number | auto;
       Default:    worker_processes 1;
       Context:    main                    

   2、worker_cpu_affinity cpumask ...;  # 定義worker進程和cpu的綁定
       worker_cpu_affinity auto [cpumask];
       Default:    —
       Context:    main                        
       CPU MASK:
           000000010號CPU
           000000101號CPU
           ... ...

   3、worker_priority number; # 指定worker進程的nice值,設定worker進程優先級;[-20,20]
       Syntax: worker_priority number;
       Default:    worker_priority 0;
       Context:    main

   4、worker_rlimit_nofile number;  # worker進程所能夠打開的文件數量上限;
       Syntax: worker_rlimit_nofile number;
       Default:    —
       Context:    main

調試、定位問題:
   1、daemon on|off;     # 是否以守護進程方式運行Nignx;
       Syntax: daemon on | off;
       Default:    daemon on;
       Context:    main

   2、master_process on|off;  # 是否以master/worker模型運行nginx;默認為on
       Syntax: master_process on | off;
       Default:    master_process on;
       Context:    main

   3、error_log file [level];  # 定義錯誤日志文件路徑與級別
       Syntax: error_log file [level];
       Default:    error_log logs/error.log error;
       Context:    main, http, mail, stream, server, location

事件驅動相關的配置:
   events {
       ...
   }


   1、worker_connections number; # 每個worker進程所能夠打開的最大并發連接數數量;
       Syntax: worker_connections number;
       Default:    worker_connections 512;
       Context:    events
       worker_processes * worker_connections得出最大并發連接數

   2、use method;  # 指明并發連接請求的處理方法;
       Syntax: use method;
       Default:    —
       Context:    events
           use epoll;

   3、accept_mutex on | off;  # 處理新的連接請求的方法;on意味著由各worker輪流處理新請求,Off意味著每個新請求的到達都會通知所有的worker進程;建議使用on
       Syntax: accept_mutex on | off;
       Default:    accept_mutex off;
       Context:    events

http段配置

與套接字相關的配置:
   1、server { ... }  #配置一個虛擬主機;
       Default:    —
       Context:    http

       server {   # 配置虛擬主機示例
           listen address[:PORT]|PORT;
           server_name SERVER_NAME;
           root /PATH/TO/DOCUMENT_ROOT;
       }

   2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE  #定義虛擬主機所監聽的端口
       listen address[:port] [default_server] [ssl] [http2 | spdy]  [backlog=number] [rcvbuf=size] [sndbuf=size]
       Default:    listen *:80 | *:8000;
       Context:    server

   3、server_name name ...;  #指明虛擬主機的主機名稱;后可跟多個由空白字符分隔的字符串;
       Default:    server_name "";
       Context:    server

           支持*通配任意長度的任意字符;server_name *.magedu.com
           支持~起始的字符做正則表達式模式匹配;server_name ~^www\d+\.magedu\.com$

       匹配機制:
           (1) 首先是字符串精確匹配;
           (2) 左側*通配符;
           (3) 右側*通配符;
           (4) 正則表達式;

   4、tcp_nodelay on | off;  #在keepalived模式下的連接是否啟用TCP_NODELAY選項;將多個小包打包成一個報文發送給客戶端
       Default:    tcp_nodelay on;
       Context:    http, server, location


   5、sendfile on | off;  #是否啟用sendfile功能;      
       Default:    sendfile off;
       Context:    http, server, location, if in location

定義路徑相關的配置:
   6
、root path;   #設置web資源路徑映射;用于指明用戶請求的url所對應的本地文件系統上的文檔所在目錄路徑;
       Default:    root html;
       Context:    http, server, location, if in location

   7、location [ = | ~ | ~* | ^~ ] uri { ... }  #在一個server中location配置段可存在多個,用于實現從uri到文件系統的路徑映射;ngnix會根據用戶請求的URI來檢查定義的所有location,并找出一個最佳匹配,而后應用其配置;
       location @name { ... }
       Default:    —
       Context:    server, location

       =:對URI做精確匹配;例如, http://www.magedu.com/, http://www.magedu.com/index.html
           location = / {
               ...
           }
       ~:對URI
做正則表達式模式匹配,區分字符大小寫;
       ~*:對URI做正則表達式模式匹配,不區分字符大小寫;
       ^~:對URI的左半部分做匹配檢查,不區分字符大小寫;
       不帶符號:匹配起始于此uri的所有的url;

       匹配優先級:=, ^~, ~/~*,不帶符號;

       root /vhosts/www/htdocs/
       http://www.magedu.com/index.html --> /vhosts/www/htdocs/index.html

       server {
           root  /vhosts/www/htdocs/      
           location /admin/ {
               root /webapps/app1/data/
           }
       }

   8、alias path;   #定義路徑別名,文檔映射的另一種機制;僅能用于location上下文;
       Syntax: alias path;
       Default:    —
       Context:    location

       注意:location中使用root指令和alias指令的意義不同;
           (a) root,給定的路徑對應于location中的/uri/左側的/;
           (b) alias,給定的路徑對應于location中的/uri/右側的/;

   9、index file ...;   #默認主頁面定義
       Default:    index index.html;
       Context:    http, server, location

   10、error_page code ... [=[response]] uri;  #定義默認錯誤頁面
       Default:    —
       Context:    http, server, location, if in location

   11、try_files file ... uri;

定義客戶端請求的相關配置
   12、keepalive_timeout timeout [header_timeout];  #設定保持連接的超時時長,0表示禁止長連接;默認為75s;
       Default:    keepalive_timeout 75s;
       Context:    http, server, location

   13、keepalive_requests number;  #在一次長連接上所允許請求的資源的最大數量,默認為100;
       Default:    keepalive_requests 100;
       Context:    http, server, location

   14、keepalive_disable none | browser ...;  #對哪種瀏覽器禁用長連接;
       Default:    keepalive_disable msie6;
       Context:    http, server, location

   15、send_timeout time;  #向客戶端發送響應報文的超時時長,此處,是指兩次寫操作之間的間隔時長;
       Default:    send_timeout 60s;
       Context:    http, server, location

   16、client_body_buffer_size size;  #用于接收客戶端請求報文的body部分的緩沖區大?。荒J為16k;超出此大小時,其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置;
       Default:    client_body_buffer_size 8k|16k;
       Context:    http, server, location

   17、client_body_temp_path path [level1 [level2 [level3]]];  #設定用于存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量;
       Default:    client_body_temp_path client_body_temp;
       Context:    http, server, location

           16進制的數字;

           client_body_temp_path path  /var/tmp/client_body  1 2 2
               /var/tmp/client_body目錄下有16個一級子目錄,每個一級子目錄下面有256個二級子目錄,每個二級子目錄下面有256個三級子目錄

對客戶端進行限制的相關配置:
   18、limit_rate rate;  #限制響應給客戶端的傳輸速率,單位是bytes/second,0表示無限制;
       Default:    limit_rate 0;
       Context:    http, server, location, if in location

   19、limit_except method ... { ... }  #限制對指定的請求方法之外的其它方法的使用客戶端;
       Default:    —
       Context:    location

       limit_except GET {
           allow 192.168.1.0/32;
           deny  all;
       }      

文件操作優化的配置:
   20、aio on | off | threads[=pool];  #是否啟用aio功能;
       Default:    aio off;
       Context:    http, server, location

   21、directio size | off;  #在Linux主機啟用O_DIRECT標記,此處意味文件大于等于給定的大小時使用,例如directio 4m;
       Default:    directio off;
       Context:    http, server, location

   22、open_file_cache off;  # 是否開啟緩存
       open_file_cache max=N [inactive=time];
       Default:    open_file_cache off;
       Context:    http, server, location
           nginx可以緩存以下三種信息:
               (1) 文件的描述符、文件大小和最近一次的修改時間;
               (2) 打開的目錄結構;
               (3) 沒有找到的或者沒有權限訪問的文件的相關信息;

           max=N:可緩存的緩存項上限;達到上限后會使用LRU算法實現緩存管理;

           inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少于open_file_cache_min_users指令所指定的次數的緩存項即為非活動項;

   23、open_file_cache_valid time;  #緩存項有效性的檢查頻率;默認為60s;
       Default:    open_file_cache_valid 60s;
       Context:    http, server, location

   24、open_file_cache_min_uses number;  #在open_file_cache指令的inactive參數指定的時長內,至少應該被命中多少次方可被歸類為活動項;
       Default:    open_file_cache_min_uses 1;
       Context:    http, server, location

   25、open_file_cache_errors on | off;  #是否緩存查找時發生錯誤的文件一類的信息;
       Default:    open_file_cache_errors off;
       Context:    http, server, location

ngx_http_access_module模塊

實現基于ip的訪問控制功能
   26、allow address | CIDR | unix: | all;
       Default:    —
       Context:    http, server, location, limit_except

   27、deny address
| CIDR | unix: | all;
       Default:    —
       Context:    http, server, location, limit_except

ngx_http_auth_basic_module模塊

實現基于用戶的訪問控制,使用basic機制進行用戶認證;
   28、auth_basic string | off;
       Default:    auth_basic off;
       Context:    http, server, location, limit_except

   29、auth_basic_user_file file;
       Default:    —
       Context:    http, server, location, limit_except

       location /admin/
{
           alias /webapps/app1/data/;
           auth_basic "Admin Area";
           auth_basic_user_file /etc/nginx/.ngxpasswd;
       }


       注意:htpasswd命令由httpd-tools所提供;      

ngx_http_stub_status_module模塊

用于輸出nginx的基本狀態信息;

   Active connections: 291
   server accepts handled requests
       16630948 16630948 31070465
   Reading: 6 Writing: 179 Waiting: 106    

   Active connections: 活動狀態的連接數;
   accepts:已經接受的客戶端請求的總數;
   handled:已經處理完成的客戶端請求的總數;
   requests:客戶端發來的總的請求數;
   Reading:處于讀取客戶端請求報文首部的連接的連接數;
   Writing:處于向客戶端發送響應報文過程中的連接數;
   Waiting:處于等待客戶端發出請求的空閑連接數;

   30、stub_status;
       Default:    —
       Context:    server, location

   配置示例:
       location  /basic_status {
           stub_status;
       }

ngx_http_log_module模塊

he ngx_http_log_module module writes request logs in the specified format.

   31、log_format name string ...;   #日志格式
       Default:    log_format combined "...";
       Context:    http

       string可以使用nginx核心模塊及其它模塊內嵌的變量;

       $bytes_sent:發送到客戶端的字節數
       $connection:連接序列號
       $connection_requests:目前一些通過連接發出的請求(1.1.18
       $msec:時間與一個毫秒分辨率秒日志寫入的時間
       $pipe"p"如果請求被流水線
       $request_length:請求長度
       $request_time:請求處理時間在毫秒分辨率秒; 第一字節之間經過的時間是從在客戶端和日志寫入讀出之后,最后的字節被發送到客戶端
       $status:響應狀態
       $time_iso8601:在ISO 8601標準格式的本地時間
       $time_local:在通用日志格式的本地時間

   32、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];   #訪問日志文件路徑,格式及相關的緩沖的配置;
       access_log off;
       Default:    access_log logs/access.log combined;
       Context:    http, server, location, if in location, limit_except

           buffer=size
           flush=time

   33、open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];    #緩存各日志文件相關的元數據信息;
       open_log_file_cache off;
       Default:    open_log_file_cache off;
       Context:    http, server, location

           max:緩存的最大文件描述符數量;
           min_users:在inactive指定的時長內訪問大于等于此值方可被當作活動項;
           inactive:非活動時長;
           valid:驗正緩存中各緩存項是否為活動項的時間間隔;

原創文章,作者:zhai796898,如若轉載,請注明出處:http://www.www58058.com/56509

(0)
zhai796898zhai796898
上一篇 2016-11-01
下一篇 2016-11-01

相關推薦

  • Shell運算符

    Bash 支持很多運算符,包括算數運算符、關系運算符、布爾運算符、字符串運算符和文件測試運算符。 原生bash不支持簡單的數學運算,但是可以通過其他命令來實現,例如 awk 和 expr,expr 最常用。 expr 是一款表達式計算工具,使用它能完成表達式的求值操作。 例如,兩個數相加: #!/bin/bash val=`expr 2 + 2` echo …

    Linux干貨 2017-04-18
  • N25-第四周作業

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限。 1.  [root@jizo etc]# ls –d skel 2.  skel 3.  [root@jizo etc]# cd /home 4.  [root@jizo …

    Linux干貨 2016-12-26
  • 沉舟側畔千帆進,枯木頭前萬樹春

    N21第五周博客作業 1、  顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; ~]#  grep -E "^[[:space:]]+" //boot/grub/grub.conf root (hd0,0) kernel /vmlin…

    Linux干貨 2016-07-29
  • DNS以及bind使用

    DNS以及域名分發布式數據庫相關知識      一、 DNS相關介紹           1. DNS(Domain Name Service):域名解析服務協議         &nbsp…

    2015-03-17
  • LVS-dr模型

    網絡拓撲圖 需三臺主機 要在主機rs上做系統內核的限制 vs主機上配置vip和在rs主機上在lo環配置vip 那個先開始都行 rs主機上在lo環配置vip必須要晚于于在rs主機做內核的限制 在VS主機上配置VIP #ifconfig ens33:0 172.18.0.33 network 255.255.255.2550 broadcast 172.18.0…

    Linux干貨 2017-05-17
  • Linux基礎 & bash相關

    Q1:Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 文件管理類命令: cd, ls, touch, cp, mv, rm, cat, tac, more, less, tail, head; 詳細介紹以上命令: cd: 在Linux文件系統上,可以使用切換目錄命令cd將shell會話切換到另一個目錄。 命令格式: ~]#&nbsp…

    Linux干貨 2016-11-06
欧美性久久久久