Nginx基礎指南詳解

  1. web服務軟件種類介紹
    常用來提供靜態Web服務的軟件有如下三種:

Apache: 這是中小型Web服務的主流,Web服務器中的老大哥。

Nginx: 大型網站Web服務的主流,曾經Web服務器中的初生牛犢,現已長大.Nginx的分支Tengine(http://tengine.taobao.org/)目前也在飛速發展

Lighttpd:這是一個不溫不火的優秀Web軟件,社區不活躍,靜態解析效率很高。
在Nginx流行前,它是大併發靜態業務的首選,國內百度貼吧、豆瓣等衆多網站都有Lighttpd奮鬥的身影。

常用來提供動態服務的軟件
PHP(FastCGI):
大中小型網站都會使用,動態網頁語言PHP程序的解析容器。
它可配合Apache解析動態程序,不過,這裏的PHP不是FastCGI守護進程模式,而是mod_php5.so(module)。
也可配合Nginx解析動態程序,此時的PHP常用FastCGI守護進程模式提供服務。

Tomcat:
中小企業動態Web服務主流,互聯網Java容器主流(如jsp、do)。
Resin:
大型動態Web服務主流,互聯網Java容器主流(如jsp、do)。

  1. nginx軟件服務介紹
    如果你聽說或使用過Apache軟件,那麼很快就會熟悉Nginx軟件,與Apache軟件類似,
    Nginx(“engine x”)是一個開源的,支持高性能、高併發的WWW服務器和代理服務軟件。它是由俄羅斯人lgor Sysoev開發的,最初被應用在俄羅斯的大型網站www.rambler.ru上。後來作者將源代碼以類BSD許可證的形式開源出來供全球使用。Nginx可以運行在UNIX、Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操作系統中

  2. nginx軟件特徵介紹
    · 支持高併發:能支持幾萬併發連接(特別是靜態小文件業務環境)
    · 資源消耗少:在3萬併發連接下,開啓10個Nginx線程消耗的內存不到200MB
    · 支持異步網絡I/O事件模型epoll(Linux 2.6+) apache(select)

  3. nginx軟件功能介紹
    1)作爲Web服務軟件(處理用戶訪問靜態請求)
    2)反向代理或負載均衡服務
    3)前端業務數據緩存服務

  4. nginx軟件模型特點說明
    apache與nginx軟件對比說明???
    apache使用select模型
    nginx使用epoll模型
    舉例說明:宿舍管理員
    select模型版管理員 會一個一個房間查詢人員
    epoll模型版管理員 會進行檢索後,直接找到需要找的人
    舉例說明:幼兒園阿姨
    select模型版阿姨 會一個一個小朋友進行詢問,確認哪個小朋友需要上廁所
    epoll模型版阿姨 會告知想上廁所小朋友自覺站到響應位置

  5. nginx軟件編譯安裝
    第一個里程:軟件依賴包安裝
    pcre-devel: perl語言正則表達式兼容軟件包
    openssl-devel:使系統支持https方式訪問
    yum install -y pcre-devel openssl-devel

    第二個里程:創建一個管理nginx進程的虛擬用戶
    useradd www -s /sbin/nologin/ -M

    第三個里程:下載並解壓nginx軟件
    cd /server/tools
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar xf nginx-1.12.2.tar.gz

    第四個里程:進行軟件編譯安裝
    軟件編譯安裝三部曲:
    ①. 編譯配置
    ./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
    --prefix=PATH 指定軟件安裝在什麼目錄下
    --user=USER 指定軟件worker進程管理用戶,利用www虛擬用戶管理worker進程
    --group=USER
    --with-http_ssl_module 使nginx程序可以支持HTTPsF訪問功能
    --with-http_stub_status_module 用於監控用戶訪問nginx服務情況
    ②. 編譯過程
    ③. 編譯安裝

    第五個里程:爲nginx程序軟件創建鏈接目錄
    ln -s /application/nginx-12.2 /application/nginx

    第六個里程:啓動nginx程序服務
    /application/nginx/sbin/nginx

  6. nginx軟件程序目錄結構

    conf --- nginx程序所有配置文件保存目錄
    nginx.conf nginx程序主配置文件
    精簡nginx.conf配置文件內容:
    grep -Ev "#|^$" nginx.conf.default >nginx.conf

    nginx配置文件組成:
    ①. main nginx主區塊
    ②. event nginx事件區塊
    ③. http nginx http功能區塊
    ④. server nginx 網站主機區塊
    ⑤. location nginx 匹配或者定位區塊

    html --- nginx程序站點目錄
    logs --- nginx程序日誌文件保存目錄
    sbin --- nginx程序命令所在目錄
    nginx命令參數說明:
    -V --- 查看nginx軟件編譯配置參數
    -t --- 檢查nginx配置文件語法格式是否正確
    -s --- 用於管理nginx服務運行狀態
    stop 停止nginx服務
    reload 平滑重啓nginx服務器
    重啓nginx服務
    nginx -s stop 先停止
    nginx 再啓動

  7. 編寫nginx服務配置
    三個語法格式說明:
    ①. 大括號要成對出現
    ②. 每一行指令後面要用分號結尾
    ③. 每一個指令要放置在指定的區塊中

    實現編寫一個網站頁面
    worker_processes 1;
    events {
    worker_connections 1024;
    }
    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 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;
    location / {
    root html/blog;
    index index.html index.htm;
    }
    }

    第二個里程創建站點目錄:
    mkdir -p /application/nginx/html/{www,bbs,blog}

    第三個里程創建站點目錄下首頁文件:
    for name in www bbs blog;do echo "10.0.0.7 $name.etiantian.org" >/application/nginx/html/$name/index.html;done
    for name in www bbs blog;do cat /application/nginx/html/$name/index.html;done
    10.0.0.7 www.etiantian.org
    10.0.0.7 bbs.etiantian.org
    10.0.0.7 blog.etiantian.org

    第四個里程:進行訪問測試
    瀏覽器訪問測試:
    注意:需要編寫windows主機hosts文件,進行解析
    命令行訪問測試:
    利用curl命令在linux系統中訪問測試
    注意:需要編寫linux主機hosts文件,進行解析

    虛擬主機配置文件編寫方法:
    ①. 基於域名的虛擬主機配置方法(最常用)
    ②. 基於端口的虛擬主機配置方法
    說明:當你訪問的網站域名在虛擬主機配置中不存在時,默認會將第一個虛擬主機的配置頁面響應給用戶
    ③. 基於IP地址的虛擬主機配置方法
    說明:nginx服務中只要涉及IP地址的修改,都需要重啓nginx服務,而不能採用平滑重啓

09 Nginx服務日誌信息
錯誤日誌 訪問日誌

  1. 錯誤日誌
    Syntax: error_log file [level];
    Default:
    error_log logs/error.log error;
    Context: main, http, mail, stream, server, location
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;

    vim nginx.conf
    error_log /tmp/error.log error;

    補充說明:

    錯誤日誌的,默認情況下不指定也沒有關係,因爲nginx很少有錯誤日誌記錄的。
    但有時出現問題時,是有必要記錄一下錯誤日誌的,方便我們排查問題。
    error_log 級別分爲 debug, info, notice, warn, error, crit 默認爲crit
    該級別在日誌名後邊定義格式如下:
    error_log /your/path/error.log crit;

    crit 記錄的日誌最少,而debug記錄的日誌最多。
    如果nginx遇到一些問題,比如502比較頻繁出現,但是看默認的error_log並沒有看到有意義的信息,
    那麼就可以調一下錯誤日誌的級別,當你調成error級別時,錯誤日誌記錄的內容會更加豐富

  2. 訪問日誌(重點關注)
    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; --- 調用定義格式信息,生成訪問日誌
    $remote_addr 10.0.0.1 --- 訪問客戶端的源地址信息
    $remote_user - --- 訪問客戶端認證用戶信息 ???
    [$time_local] --- 顯示訪問時間
    $request GET / HTTP/1.1 --- 請求行信息
    $status 304 --- 狀態碼信息(304狀態碼利用緩存顯示頁面信息)
    $body_bytes_sent --- 服務端響應客戶端的數據大小信息
    $http_referer --- 記錄鏈接到網站的域名信息 ???
    $http_user_agent --- 用戶訪問網站客戶端軟件標識信息
    用戶利用客戶端瀏覽器測試訪問時,win10默認瀏覽器會有異常問
    $http_x_forwarded_for --- ??? 反向代理
    官方鏈接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

  3. 日誌要進行切割

    1. 利用shell腳本實現日誌切割
      [root@web01 scripts]# vim cut_log.sh
      #!/bin/bash

      data_info=$(date +%F-%H:%M)

      mv /application/nginx/logs/www_access.log /application/nginx/logs/access.log.$data_info
      /application/nginx/sbin/nginx -s reload

      cut nginx log cron

      • /6 /bin/sh /server/scripts/cut_log.sh &>/dev/null

09 Nginx服務location區塊說明
利用location區塊可以用於定位或者匹配網站資源信息
企業需求解決
搭建好一臺nginx的web服務器。配置好內網卡地址與外網卡地址
web服務的網站域名爲www.etiantian.org,站點目錄爲html/www
要求內網用戶可以訪問網站http://www.etiantian.org/AV資源信息
要求外網用戶禁止訪問網站http://www.etiantian.org/AV資源信息

①. 如何利用nginx進行訪問控制
deny allow
ngx_http_access_module --- 實現訪問控制模塊
官方鏈接:nginx.org/en/docs/http/ngx_http_access_module.html
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}

②. 如何定位站點目錄資源信息
location區塊進行定位站點目錄下資源信息
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
官方鏈接:http://nginx.org/en/docs/http/ngx_http_core_module.html#location

第一個里程:編寫nginx配置文件
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
}
}

第二個里程:創建測試訪問資源
mkdir AV
echo "AV info" >AV/oldboy.html
cat AV/oldboy.html 

第三個里程:重啓nginx服務
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload

location [ = | ~ | ~* | ^~ ] uri { ... }
=     --- 精確匹配網站uri資源信息
~     --- 區分大小寫匹配網站uri資源信息
~*    --- 不區分大小寫匹配網站uri資源信息
^~    --- 優先匹配網站uri資源信息
/AV/  --- 指定匹配網站資源目錄信息
/     --- 默認匹配網站資源信息
!     --- 對匹配的內容進行取反

location = / {
    [ configuration A ]       --- 優先級最高 ①
}

location / {                  --- 所有匹配都不滿足時候,匹配默認location ④
    [ configuration B ]
}

location /documents/ {        --- 根據資源目錄進行匹配         ③
    [ configuration C ]
}

location ^~ /images/ {        --- 優先匹配 ②
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {  --- 不區分大小寫匹配網站資源  ③
    [ configuration E ]
}

10 Nginx服務rewrite模塊功能說明

  1. 實現域名地址信息跳轉
  2. 用於做僞靜態
    www.etiantian.org/oldboy?edu.html ---動態資源
    www.etiantian.org/oldboy-edu.html ---僞靜態

    實現類似百度重寫域名的功能?
    baidu.com ===> www.baidu.com
    etiantian.org ===> www.etiantian.org

    rewrite
    Syntax: rewrite regex replacement [flag];
    Default: —
    Context: server, location, if

    last
    stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
    break
    stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
    redirect
    returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
    permanent
    returns a permanent redirect with the 301 code.

    rewrite指令實踐操作一:(錯誤)
    [root@web01 extra]# cat bbs.conf
    server {
    listen 80;
    server_name www.etiantian.org bbs.org;
    rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
    root html/bbs;
    index index.html index.htm;
    }

    [root@web01 extra]# curl -L etiantian.org
    curl: (47) Maximum (50) redirects followed
    [root@web01 extra]# curl -Lv etiantian.org --- 顯示無限循環過程
    說明:以上配置進入了無限循環狀態

    rewrite指令實踐操作二:(正確)
    cat bbs.conf
    server {
    listen 80;
    server_name etiantian.org;
    rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
    }
    server {
    listen 80;
    server_name bbs.etiantian.org bbs.org;
    root html/bbs;
    index index.html index.htm;
    }

    rewrite指令實踐操作三:(正確)
    [root@web01 extra]# cat bbs.conf
    server {
    listen 80;
    server_name bbs.etiantian.org bbs.org;
    if ($host ~ "^etiantian.org$") {
    rewrite ^/(.
    ) http://bbs.etiantian.org/$1 permanent;
    }
    root html/bbs;
    index index.html index.htm;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章