nginx詳解


一,服務介紹

是一個開源,支持高性能,高並發的www服務和代理服務軟件,占用資源少,且功能豐富而逐漸流行起來,

  • web軟件(apache.lls)
  • 反向代理負載均衡功能 (與lvs和haproxy專業代理軟件)
  • 緩存服務功能(squid,varnish)

二,服務功能以及特性

nginx重要特性

    • 可針對靜態資源做高並發訪問及緩存
    • 可使用方向代理,並且可進行數據緩存
    • 具有簡單負載均衡,節點健康檢查和容錯功能
    • 支持遠程fastCGI服務的緩存加速
    • 支持fastCGI,Uwsgi,SCGI and memcached servers的加速和緩存
    • 支持ssl tls sni
    • 具有模塊化的架構,過濾器包含gzip ranges 支持 chunked響應,xslt ssl及圖像縮放等功能,在ssl過濾器中,一個包含多個ssl的頁面,如果經過fastCGI或反向代理處理,可被並行處理

他所具備的其他www服務特性如下:

    • 支持基於名字,端口以及IP的多虛擬主機站點
    • 支持keepalived 和pipelined
    • 可進行簡單,方便,領過的配置和管理
    • 支持修改nginx配置,並且在代碼上顯示,可平滑重啟,不中斷業務訪問
    • 可自定義訪問日志格式,臨時緩沖寫日志操作,快速日志淪陷及通過rsyslog處理日志
    • 可利用信號控制nginx進程
    • 支持3xx-5xx狀態嗎重定向
    • 支持rewrite模塊,支持URL重寫及正則表達式匹配
    • 支持基於客戶端IP地址和HTTP基本認證的訪問控制
    • 支持PUT DELETE MKCOL COPY,以及MOVE等特殊的http請求方法
    • 支持FLV流和MP4流技術產品應用
    • 支持http響應速率限制
    • 支持同一IP地址的並發連接或請求數限制

企業面試時需要解答如下步驟 nginx http服務器的特色及優點

    • 支持高並發:能支持幾萬並發連接(特別是靜態小文件業務環境)
    • 資源消耗少:在三萬並發連接下,開啟10個nginx線程消耗不到200M內存
    • 可以做http反向代理及加速緩存,既負載均衡功能,內置對RS節點服務器健康檢查共功能,這相當於專業的haproxy軟件或lvs功能
    • 具備squid等專業軟件的緩存功能
    • 支持異步網絡IO實踐模型epol

nginx軟件的主要企業功能應用

    • 作為web服務軟件
      • 使用nginx運行html,js,css小圖片等靜態數據(次功能類似lighttpd軟件)
      • nginx結合fast CGI運行PHP等動態程序(例如利用fastcgi_pass方式)
      • nginx結合tomcat/resin等支持java動態程序(常用proxy_pass方式)
    • 反向代理或負載均衡
    • 前端業務數據緩存服務

 

三,服務區別

apache:

  • 非常穩定
  • prefock模式取消了進程創建開銷,性能很高
  • 處理動態業務時,因關聯到后端的引擎和數據庫,瓶頸不在於apache本身上
  • 高並發時消耗系統封資源相對多一些
  • 基於傳統的select模型,高並發能力有限
  • 支持擴展庫,可通過DSO,apxs方法編譯安裝額外的插件功能,不需要重新編譯apache
  • 功能多,更穩定,更安全,插件也多

nginx:

  • 基於異步網絡I/O模型(epoll,kqueue)
  • 具有支持高性能,高並發的特性,並發連接可達數萬
  • 對像文件(小於1mb的小文件)高並發支持很好,性能很高
  • 不支持類似apache的dso模式,擴展庫必須編譯進主程序(缺點)
  • 支持web,proxy,cache三大重點功能,並且很優秀
  • 市場份額逐年增加

 

四,epoll模型和select模型區別形象比喻介紹

  • nginx     使用異步epoll模型
  • apache  使用同步select模型

五,如何根據企業場景正確選擇web服務器

  • 靜態業務:若是高並發場景,盡量才有nginx
  • 動態業務:理論上采用nginx和apache均可,建議選擇nginx,要避免相同業務服務軟件多樣化,額外增加維護成本,動態業務可以由nginx兼前端代理,再根據頁面元素的類型或則目錄,向后轉發到后端相應的服務器進行處理
  • 既有靜態又有動態業務:就采用nginx

 

六,linux系統安裝軟件的多種方法

1.rpm安裝

    • rpm -ivh 包名.rpm(有依賴問題,安裝A,A需要先安裝B)

2.yum安裝

    • yum -y install 軟件名 (解決rpm安裝的依賴問題,安裝更簡安裝)
    • 優點:簡單,易用,高效
    • 缺點:不能定制

3.編譯安裝(C語言源碼,編譯二進制等)

    • ./configure(配置),make(編譯),make install(安裝)
    • 優點:可以定制
    • 缺點:復雜,效率低

4.企業定制化制作rpm包,搭建yum倉庫,把我定制的rpm包放到yum倉庫,進行yum安裝

    • 優點:結合2.3的優點
    • 缺點:復雜

 

七,安裝nginxweb服務實戰

1.安裝nginx做需的pcre庫

pcre中文perl監控正則表達式,安裝pcre庫就是為了使nginx支持具備URL重新功能的rewirte模塊,如果不安裝pcre庫,則nginx無法使用rewrite模塊功能,nginx的rewrite模塊功能幾乎是企業應用的必須,安裝過程如下:

a,yum -y install pcre pcre-devel   

 b.yum -y install openssl openssl-devel

 c.wegt xxx nginx-xxx.tar.gz

 d.tar xf nginx-xxx.tar.gz

 e.cd nginx-xxx

 f.useradd www -s /sbin/nologin -M

./configure  --help

 g./configure --user=www --group=www --prefix=/application/nginx-1.8.0/  --with-http_ssl_module  --with-http_stub_status_module

h.echo $0  確定是否安裝成功否

make

make install

ln -s /application/nginx-1.8.0/ /application/nginx

 

八,自定義站定目錄以及簡單編寫開發網頁內容詳解

  • conf    配置文件

egrep -v "^$|#" nginx.conf.default >nginx.conf  把開頭是$|#去掉

下面代表一個網站

 server {

        listen 80;

        server_name www.etiantian.com;   ###域名       

                location / {                              #安裝根目錄                        

                 root html;                              #安裝相對路徑

                 index index.html index.htm;  #默認打開文件

        }

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {               root html;

        }     }

 下面代表php安裝情況

 <?php 
phpinfo();
?> 

sbin    執行文件

log      日志文件

html    站點目錄

 

九,nginx常用模塊介紹

 

核心模塊

標准模塊

 

 

十,nginx主配置文件默認參數詳解

 

worker_processes 1;             #---------------------------------->>>main區,nginx核心功能模塊    進程數量1,worker數量很多
events {
worker_connections 1024;     #---------------------------------->>> 一個worker最大服務數量,同時可以接的數量,最大並發數worker*
}                                              #---------------------------------->>>events區,nginx核心功能模塊
http {                                       #---------------------------------->>>http區,nginx核心功能模塊
include mime.types;               #---------------------------------->>>include包含,這個配置文件包含mime.types
default_type application/octet-stream;
sendfile on;                             #---------------------------------->>>開啟高效傳輸模式,優化會講
keepalive_timeout 65;            #---------------------------------->>>超時時間,鏈接時間
server {                                   #---------------------------------->>>一個server對應一個網站,
listen 80;                                 #---------------------------------->>>端口必須有,每個server都有
server_name localhost;          #---------------------------------->>>域名必須有,每個server都有
location / {                               #---------------------------------->>>沒有做任何匹配,默認找這個文件對應的內容
root html;
index index.html index.htm;
}

location /50z.html{                  #---------------------------------->>>如果匹配上了,默認就走這個文件
root html;
}
error_page 500 502 503 504 /50x.html; #---------------------------------->>>出現錯誤,找這個文件,優雅匹配
location = /50x.html {
root html;
}
}
}

 

十一,虛擬主機概念和類型介紹

所謂虛擬主機,再web服務里就是一個獨立的網站站點,這個站點對應獨立的域名,也可能是IP或則端口,具有獨立的程序或資源目錄,可以獨立的對外提供服務供用戶訪問,這個獨立的站點再配置上有一定格式的標簽標記,

    • 對於apache軟件來說,一個虛擬主機的標簽段通常被包含再<virtualHost></virtualHost>內,
    • 而nginx軟件則使用一個server{}標簽來標示一個虛擬主機,

一個web服務里可以有多個虛擬主機標簽對,同時支持多個虛擬主機站點

例子:blog.51cto.com www.51cto.com edu.51cto.com home.51cto.com

十二,多種類型的虛擬主機詳細介紹

  • 基於域名的虛擬主機,通過域名來區分,外部網站
    • nginx基於域名的虛擬主機實戰
    • http {
          include mime.types;
          default_type application/octet-stream;
          sendfile on;
          keepalive_timeout 65;
          server {
              listen 80;
              server_name www.etiantian.org;
              location / {
                  root html/www;
                  index index.html index.htm;
               }
      }
      server {
              listen 80;
              server_name bbs.etiantian.org;
              location / {
                  root html/bbs;
                  index index.html index.htm;
              }
      }
      server {
           listen 80;
          server_name blog.etiantian.org;

            autoindex on;    #可以避免403錯誤而且可以提供以惡喜愛啊

    location / {
        root html/blog;
        index index.html index.htm;
    }
}
}

 具體流程:   

dns解析

請求報文請求頭里面有host,

服務器接收到端口和域名,

然后就從上往下走,

因為只有一個localtion,

就走默認的,/讀配置,走下面配置,

如果都沒有就返回403錯誤

  • 基於端口的虛擬主機,通過端口來區分,內部網站,網站的后台
    • nginx基於端口的虛擬主機實戰
      • http {
            include mime.types;
            default_type application/octet-stream;
            sendfile on;
            keepalive_timeout 65;
            server {
                listen 80;
                server_name www.etiantian.org;
                location / {
                    root html/www;
                    index index.html index.htm;
                 }
        }
        server {
                listen 81;
                server_name www.etiantian.org;
                location / {
                    root html/bbs;
                    index index.html index.htm;
                }
        }
        server {
             listen 82;
            server_name www.etiantian.org;

                  autoindex on;    #可以避免403錯誤而且可以提供以惡喜愛啊

       

          location / {
              root html/blog;
              index index.html index.htm;
          }
      }
      }

       

  • 基於IP的的虛擬主機,基本不用
    • nginx基於ip的虛擬主機實戰
  • http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        server {
            listen 192.168.1.9;
            server_name www.etiantian.org;
            location / {
                root html/www;
                index index.html index.htm;
             }
    }
    server {
            listen 192.168.1.10;
            server_name www.etiantian.org;
            location / {
                root html/bbs;
                index index.html index.htm;
            }
    }
    server {
         listen 192.168.1.11;
        server_name www.etiantian.org;

            autoindex on;    #可以避免403錯誤而且可以提供以惡喜愛啊

 

    location / {
        root html/blog;
        index index.html index.htm;
    }
}
}

 

 

十三,虛擬主機配置步驟檢查及檢測策略-配置文件優化等

  • include  /vhost/*.conf
  • 這個操作可以把server文件全部拷貝出來單獨做成一個xxx.conf的文件

十四,配置ngixn虛擬主機別名以及別人企業場景的應用說明

server {
        listen 192.168.1.10;
        server_name www.etiantian.org etiantian.org;
        location / {
            root html/bbs;
            index index.html index.htm;
        }
}

十五,nginx狀態模塊詳解與實戰(前提是安裝了nginx狀態模塊--with-http_stub_status_module)

  location / {
            stub_status on;
            access_log off;
        }

十六,nginx日志及錯誤日志詳解

  • error_log  /www/log/error.log crit;

放在主配置文件俱佳

十七,ngixn訪問日志介紹與實踐

 

 

模板

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日志輪詢切割實戰

apache:cronlog rotate

nginx:cron+scripts

  • cd /usr/local/nginx/logs
  • /bin/mv www_access.log www_access_$(date +%F).log
  • /usr/local/nginx/sbin/nginx -s reload 
  • rsync 。。。。。。同步到備份服務器
  • del date before 7 day

crontab -e  定時任務 00 00 * * *  /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

十九,nginxlocation重要語法講解及實踐檢驗

根據用戶請求的URL來執行不同的應用,其實就是根據用戶請求的網站地址URL匹配,匹配成功即進行相關的操作

匹配語法:

location[=|~|~*|^~]url                                {~}

指令           匹配標識 匹配的網站網址                匹配URL后要執行的匹配段

www.etiantian.org/oldboy/

localtion /oldboy/ {

    do something

}                                                          指定匹配

 

location / {
   root html;
   index index.html index.htm;
 }                                                 默認匹配

 

二十,nginxlocation重要實踐及結論總結

server{

listen 80;

server_name www.etiantian.org etiantian.org

location / {
   return 401;
 }     

location / {
   return 401;
 }  

location  = / {
   return 402;
 }  

location /documents/{
   return 403;
 }  

location ^~ /images/ {
   return 404;
 }  

location ~* \. (gif/jpg/jpeg)$ {
   return 500;
 }  

}

 

第一名 “location=/{”  精確匹配/

第二名 “location ^~/images/{” 匹配常規字符串,不做正則匹配檢查

第三名 “location~*\.(gif|jpg|jpeg)${”  匹配正則

第四名 “location/documents/{”  匹配常規字符串,如果有正則則優先匹配正則

第五名 “location/{”  所有location 都不能匹配后的默認匹配

二十一,nginxrewrite介紹及語法講解

主要功能也是實現URL地址重寫,nginx的rewrite規則需要PRCE軟件的支持,既通過perl兼容正則表達式語法進行匹配規則的。

語法:rewrite regex replacement [flag]

rewrite */(.*)http://blog.etiantian.org/$1 permanet;   $1就是(.*)

location ^~ /images/ {
   rewrite */(.*)http://blog.etiantian.org/$1 permanet;   $1就是(.*);
 }  

ngixnrewrite實戰案例301及url跳轉

 用別名和用跳轉的區別

別名 效果好

301   能夠看到域名

 

域名跳轉,當訪問老域名的時候跳轉到新域名

nginx用戶訪問呢及密碼驗證實戰

location / {

auth_basic       "oldboy train"

auth_basic_user_file conf/htpasswd

 

root html/www

index index.html index.htm

}

一般內部網站使用得比較多

 

 


免責聲明!

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



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