溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx相關(guān)命令有哪些

發(fā)布時間:2021-11-17 11:27:27 來源:億速云 閱讀:142 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“Nginx相關(guān)命令有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Nginx相關(guān)命令有哪些”吧!

1. Install

1.1 編譯前Lib安裝

備注:因?yàn)镃entos安裝的最小版本 所以缺的基礎(chǔ)Lib有點(diǎn)多 。。這里應(yīng)該是全的了

yum -y install gcc.x86_64 gcc-plugin-devel.x86_64 pcre-devel.x86_64 pcre-static.x86_64 pcre.x86_64 openssl-devel.x86_64 openssl-static.x86_64 openssl.x86_64 libxml2-static.x86_64 libxml2.x86_64 libxml2-devel.x86_64 libxslt-devel.x86_64 libxslt.x86_64 gd-devel.x86_64 gd.x86_64 GeoIP-devel.x86_64 GeoIP.x86_64

1.2 編譯安裝

1.2.1 configure

1.2.1.1 可用參數(shù)

  1. --prefix=PATH: 設(shè)定nginx的安裝目錄

  2. --user=name: 設(shè)定nginx worker process 以什么用戶權(quán)限運(yùn)行,可以用過nginx.conf修改

  3. --group=name:設(shè)定nginx worker process 以什么用戶組權(quán)限運(yùn)行,可以用過nginx.conf修改

  4. --with-threads :nginx是否使用線程池

  5. --with-file-aio :設(shè)定在Linux上是否使用AIO

1.2.1.2 常用但不默認(rèn)構(gòu)建module表

  1. --with-http_ssl_module https模塊,默認(rèn)不構(gòu)建,需要OpenSSL庫支持

  2. --with-http_realip_module real_ip模塊保證后端服務(wù)器的HttpRequestHeader中的IP為客戶端實(shí)際IP,默認(rèn)不構(gòu)建;

  3. --with-http_gunzip_module 對被gzip壓縮的響應(yīng)進(jìn)行解壓適應(yīng)無法解壓的客戶端,默認(rèn)不構(gòu)建

  4. --with-http_gzip_static_module 向客戶端發(fā)送以gzip壓縮過的文件,默認(rèn)不構(gòu)建

  5. --with-http_slice_module 將http大請求劃分成幾個小請求,默認(rèn)不構(gòu)建

  6. --with-debug 支持debug日志,默認(rèn)不構(gòu)建

  7. --with-stream 支持四層的負(fù)載均衡,默認(rèn)不構(gòu)建

1.2.1.3 一個常用configure樣例

./configure --prefix=/opt/nginx   --with-http_ssl_module  --with-http_realip_module  --with_stream

2. Begin Usage

2.1 nginx 啟動相關(guān)命令

  1. ./nginx 啟動nginx

  2. ./nignx -s stop — 快速關(guān)閉,即使有正在處理的請求直接關(guān)系連接

  3. ./nignx -s quit — 優(yōu)雅的關(guān)閉,不接受處理新請求;

  4. ./nignx -s reload — 重新載入配置

  5. ./nignx -s reopen — 重新打開日志輸出

  6. ./nginx -t 檢測磁盤上的配置文是否合法

2.2 配置文件文件及解釋

2.2.1 基本結(jié)構(gòu)

Nginx相關(guān)命令有哪些

2.2.2 基礎(chǔ)配置解釋

user  nginx nginx; # 需要創(chuàng)建一個nginx 用戶及nginx用戶組,禁止登陸
worker_processes  8; # 一般和CPU的核心數(shù)量一致即可,假定8個;
# worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; 
# worker_cpu_affinity 僅linux系列支持,8核8位,4核心4位(0001 0010 0100 1000);設(shè)定進(jìn)程的和CPU核心的綁定關(guān)系,可以見減少進(jìn)程在CPU核心間調(diào)度產(chǎn)生的L123緩存的調(diào)度,從而提升效能
error_log  logs/error.log  info; # 設(shè)定nginx的日志位置及日志等級
pid        logs/nginx.pid; # 設(shè)定 nginx master進(jìn)程Id文件
worker_rlimit_nofile 65536; # 設(shè)定每個worker進(jìn)程最大句柄數(shù),linux `ulimit -n 65536` 修改
events {
    use epoll; # 使用的IO模型,select、poll、epoll; Linux 可用的就這三個,默認(rèn)epoll;
    worker_connections  65536; #設(shè)定每個worker process 可以處理的連接數(shù),不能超過上面的worker_rlimit_nofile值
}

http {
    include       mime.types; # 引入文件 mime.types;
    default_type  application/octet-stream; #  默認(rèn)的mime type為 application/octet-stream
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"' ; # 設(shè)定日志格式
    access_log  logs/access.log  main;
    sendfile        on; # 設(shè)定 nginx 是否使用 sendfile 函數(shù)輸出文件,對于下載服務(wù)時請關(guān)閉;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;

    upstream backend { # 負(fù)載均衡模塊,一個后端服務(wù)器組;像 upstream,server等等相關(guān)的東西都可以另起文件,然后include 進(jìn)來;
        server 192.168.1.11 weight = 5;
        server 192.168.1.12 weight = 2;
        server 192.168.1.10 backup;
    }

    server { #可以有多個不同的server(name、IP、端口不完全一致就可以)
        listen       127.0.0.1:80; # 監(jiān)聽的IP和端口,端口省略就是協(xié)議默認(rèn)端口,IP省略就監(jiān)聽全部的IP
        server_name  localhost; # server的name,就是域名;支持通配符比如*.aruforce.site;支持正則 `~`開頭
        # nginx 收到請求時:會先匹配IP和端口,如果有多個server匹配,則繼續(xù)匹配req_header_Host 與server_name
        # 如果還是有多server_name 匹配:
        # 首選 正常的域名  的server
        # 次選 *開頭的通配符域名 的server
        # 再次 *結(jié)尾的通配符域名 的server
        # 最后 選第一個出現(xiàn)的正則表達(dá)式域名 的server
        # 如果不存在匹配的域名則轉(zhuǎn)發(fā)到默認(rèn)的server(一般就是第一個)
        charset utf-8; #設(shè)定響應(yīng)頭
        access_log  logs/80-localhost.access.log  main;
        # 錯誤頁設(shè)置
        error_page  404              /404.html; # 當(dāng)http 404時返回/404.html 
        error_page  500 502 503 504  /50x.html; # 當(dāng)http 50x時返回/50x.html
        # 靜態(tài)文件
        location / {
            root   html; #root是指從本地文件目錄查找,結(jié)尾不允許有/,如果一個req的URI匹配到,將訪問文件系統(tǒng)的html$URI的文件,而URI starts with '/';
            index  index.html index.htm;
        }
        location =/app/index.html{ # 精確匹配,訪問http://localhost/app/index.html時,會訪問html/app/index.html
            root   html;
            index  index.html index.htm;
        }

        # 下面的 2 和 3 區(qū)別
        location ^~ /app/{ # 訪問http://localhost/app/index2.html時,會訪問./app/index2.html;即使是有開頭的^~但由于不是最長匹配,那么會進(jìn)行正則型的匹配
            root   html;
            index  index.html index.htm;
        }
        location  /app/index2.html{ # 訪問http://localhost/app/index2.html時,因?yàn)闆]有^~開頭,盡管是最長匹配項,但仍舊會進(jìn)行正則項的匹配,所以會訪問./app/index2.html;如果 開頭有^~則會訪問html2/app/index2.html
            root   html2; # 
            index  index.html index.htm;
        }
        location ~ /app/{ #正則型匹配
            root   ./; # './'是指nignx的目錄
            index  index.html index.htm;
        }
        location = /50x.html { # 精確匹配
            root   html;
        }
        location /app2{ # prefix型訪問http://localhost/app2 時,會被nginx 回復(fù)301 重定向到 http://localhost/app2/;如果代理的是app2這個文件,請在前面加上`=`
            root html;
            index  index.html index.htm;
        }
        #代理功能
        location /nexus/{ #proxy1
            proxy_pass http://localhost:8081; 
            # proxy_pass 的uri要不要以`/`結(jié)尾:
            # 結(jié)尾是`/`的,nginx會把原請求的URI截斷掉locationURI的部分然后拼接上proxy_pass的URI;
            # 結(jié)尾不是`/`的 就是proxy_pass_uri+req_uri;
        }
        # location /nexus/{ #proxy2
        #    proxy_pass http://localhost:8081/nexus/; # 這條配置和上面的是一個效果;
        # }
        # 這里以瀏覽器訪問http://localhost/nexus/index.html為例說明:
        # proxy1的配置 相當(dāng)瀏覽器訪問http://localhost:8081+/nexus/index.thml
        # proxy2的配置 相當(dāng)于瀏覽器訪問 http://localhost:8081/nexus/ + index.html;
    }

    # 一個 HTTPS服務(wù)器
    server {
        listen       443 ssl; #
        server_name  www.aruforce.site *.aruforce.site;
        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;
        }
    }
}

3. location的語法及匹配規(guī)則

3.1 語法

location [ = | ~ | ~* | ^~ ] uri{
    #config
}

3.1.1 前綴

  1. location的寫法分為兩個大類prefix型 和 正則表達(dá)式型;

  2. prefix型 必須以/ 開頭(最佳實(shí)踐,不寫也行,nginx默認(rèn)會加上);

  3. 正則表達(dá)式行型 開頭必須有~(表示區(qū)分大小寫的正則)或者~*(表示不區(qū)分大小寫的正則);

  4. = 代表精確匹配,只配合prefix型使用;

  5. ^~ 這個表示為最佳匹配,不再匹配正則型;

https://segmentfault.com/a/1190000013267839;要么這個文檔有點(diǎn)不太對要么是我對這個文檔理解錯誤了. 關(guān)于^~的解釋還有匹配執(zhí)行流程;

按照我對這個文檔的內(nèi)容理解,以上面的配置在訪問http://localhost/app/index2.html 應(yīng)該是訪問 html/app/index2.html 文件才對(location ^~ /app/ 生效),但是實(shí)際上是./app/index2.html( location ~ /app/生效了);

3.1.2 prefix型的uri結(jié)尾的/

  1. / 的,req會被正常處理

  2. 沒有 /的,nginx 會響應(yīng)301(永久重定向),重定向的URI = $req.uri+ /

  3. 如果要代理具體的URI ,請使用 = 精確匹配;

3.2 大體匹配流程(規(guī)則)

  1. nginx 首先會把req的URI正?;?解碼[%xx],解路徑[.或者..],盡可能壓縮相鄰重復(fù)的/后再開始匹配;

  2. nginx會先查找 prefix型location進(jìn)行匹配(從上到下),如果匹配項以=開頭的location則直接使用,否則繼續(xù)向下匹配,從所有的prefix型匹配項中選擇前綴最長A作為備用,如果A 以 ^~開頭,會直接使用A,否則繼續(xù)檢查正則型,;

  3. nginx從正則型的location從上往下進(jìn)行匹配,若不存在匹配項,則使用A,否則使用第一個正則匹配項B;

3.3 例子

location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}
  1. 當(dāng)req URI 為/ 時,使用A;

  2. 當(dāng)req URI 為/index.html 時,使用B

  3. 當(dāng)req URI 為/documents/index.html時,使用C

  4. 當(dāng)req URI為 /images/a.jpg時,使用D;

  5. 當(dāng)req URI 為 /docments/a.jpg時,使用 E;

4. http_core 提供的常用的變量(per request at runtime)(echo $*** 可以輸出取到的值到日志文件,windows版本不支持echo)

參數(shù)解釋
$arg_name獲取請求行中name對應(yīng)的值,比如http://localhost/index.html?token=123,$arg_token就是123
$args-$query_string獲取請求行參數(shù),對于/index.html?token=123&sign=321=>token=123&sign=321,同$query_string
$bytes_sent發(fā)送響應(yīng)的字節(jié)數(shù)
$connectionconnecttion的序號
$content_typerequest header 里面的Content-Type的值
$cookie_name請求中cookies 里面的name key對應(yīng)的value,和$arg_name有點(diǎn)類似
$document_root當(dāng)前request的root或者alias指令對應(yīng)的值
$uri-$document_urirequest header里面的URI. 不帶請求參數(shù),類似于/nexus/index.html
$host請求行的host參數(shù)值->請求頭的Host值-> server_name指令的value
$request_method獲取請求方法,GET、POST、HEAD、OPTIONS、PUT等等
$http_namerequest header 里面的name對應(yīng)的值,比如 $http_user_agent
$https返回當(dāng)前鏈接是否是使用SSL,是返回on,否則返回空串
$is_args返回當(dāng)前鏈接請求行是否參數(shù),是返回?,否則返回空串
$scheme使用的協(xié)議, 比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect
$connection_requests當(dāng)前conenction發(fā)送的請求的數(shù)量
$content_lengthrequest header 里面的Content-lenght的值
$binary_remote_addr獲取客戶端IP的二進(jìn)制表示
$body_bytes_sent發(fā)送響應(yīng)的字節(jié)數(shù),不包括response header
$remote_addr獲取Client的IP
$remote_port獲取Client的port
$proxy_protocol_addr返回走代理協(xié)議的客戶端在代理協(xié)議頭的地址,在listen指令后必須加上 proxy_protocol參數(shù)
$proxy_protocol_port返回走代理協(xié)議的客戶端在代理協(xié)議頭的端口,在listen指令后必須加上 proxy_protocol參數(shù)

5. http_rewrite 模塊

5.1 用途

rewrite 模塊主要用來修改每個request的URI;server 及l(fā)ocation context可以多次使用;提供了if,break,return,rewrite,set幾個指令;

5.2 執(zhí)行流程

  1. nginx 接收到了請求,確認(rèn)使用那個server context

  2. server context 內(nèi)的 rewrite模塊的指令按出現(xiàn)的順序執(zhí)行;

  3. 新的URI(如果有重寫的話)用來確定要使用的location context,該context 的指令執(zhí)行按順序執(zhí)行;

  4. 如果URI被改寫了,則重新確認(rèn)location context繼續(xù)執(zhí)行;(location 更改的最大次數(shù)為10次)

5.3 指令 及用法

5.3.1 if
if (condition) { # 注意if和 ( 之間有個空格

}
用法說明事項樣例
if($xxx)取值型if ( $cookie_bind) { return 403 ;} 如cookie里面帶bind參數(shù)直接403禁止訪問
if ($xxx = xxx)orif ($xxx != xxx)等于或者不等于if ($arg_token = "") {return 403;} 如果請求參數(shù)不帶token 也會被禁止訪問
if ($xxx ~ regx) or if ($xxx ~* regx) 正則型`if($user_agent ~*(baidu
if (-d|-f|-e|-x)根據(jù)文件夾或者文件是否存在或者可執(zhí)行做操作if (-!d /var/www/static){return 404;} 如果不目錄存在直接反404
5.3.2 break

停止處理當(dāng)前上下文內(nèi)的rewrite模塊的指令集,如果在location 上下文內(nèi),則跳過繼續(xù)執(zhí)行其他模塊的指令;

5.3.3 return

停止處理當(dāng)前請求,返回一個http狀態(tài)碼給客戶端;

301、302、303、307、308:還可以追加一個重定向的URL 比如return 302 $scheme://$host:$server_post/login.html

444 :這個不是標(biāo)準(zhǔn)的狀態(tài)碼,nginx 會斷開和客戶端的鏈接

5.3.4 rewrite

rewrite regex replacement [flag];

  1. regex:匹配請求URI的正則表達(dá)式,如果表示包含 } 或者 那么表達(dá)式需要用"" 或者 '' 包起來

  2. repalcement:用于替換的新URL:如果repalcement 以http://或者https://或者$scheme開頭 nginx 用 replacement 發(fā)起重定向;重定向時會加上原來的請求參數(shù),如果不需要可以在repalcement末尾加上?阻止;

  3. flag:

取值解釋
last停止處理當(dāng)前的上下文的rewrite模塊指令,并重新查找一個新的location contex
break停止處理當(dāng)前的上下文的rewrite模塊指令,繼續(xù)當(dāng)前l(fā)ocation的其他處理指令
redirect發(fā)起臨時重定向302, 要求repalcement 不以http://或者 https:// 或者 $scheme 開頭
permanent發(fā)起永久重定向301
5.3.5 rewrite_log

是否開啟rewrite 模塊處理的日志(level=notice),on |off ; 開啟的話會輸出到error_log里面

6. 負(fù)載均衡

6.1 http層負(fù)載均衡及Health check

http {
    upstream backend { # http 后端服務(wù)器組;需要處在 http context 內(nèi)
        least_conn;
        # 未指定均衡算法 則使用Round Robin  隨機(jī)轉(zhuǎn)發(fā)
        # least_conn:優(yōu)先向當(dāng)前連接數(shù)最小的服務(wù)器轉(zhuǎn)發(fā)
        # ip-hash; # 根據(jù)客戶端的ip進(jìn)行取模后轉(zhuǎn)發(fā),能保證同一個IP的被轉(zhuǎn)發(fā)到一個服務(wù)器(目的是session共享)
        # hash $request_uri ;# 通用Hash,根據(jù)請求的某個參數(shù)來Hash
        server 192.168.1.11 weight = 5 fail_timeout=30s max_fail = 10 slow_start=30s; 
        # 如果僅一個服務(wù)器后面這些參數(shù)會被忽略
        # weight(權(quán)重):接受5/7的請求;
        # health_ckeck(服務(wù)狀態(tài)檢查):如果30S內(nèi)有10次失敗,則這個服務(wù)器會被標(biāo)記為不可用
        # slow_start:服務(wù)剛啟動時處理請求可能比較慢(比如緩存是邊運(yùn)行邊加載的情況下),可能會被流量直接打死,可以設(shè)置slow_start 這段時間后,流量才會逐步上升到響應(yīng)的權(quán)重
        server 192.168.1.12 weight = 2; #接受 接受2/7的請求
        server 192.168.1.10 backup; # 備機(jī)當(dāng)前面兩個不可用時會把請求轉(zhuǎn)發(fā)到這里
    }
    server{
        location / {
            proxy_pass http://backend;
        }
    }
}

6.2 TCP|UDP 負(fù)載均衡( ./configure --with-stream 模塊) 及healthCheck

# 4層TCP負(fù)載均衡,均衡的是connection,也就說不用擔(dān)心一個ClientSocket的數(shù)據(jù)被負(fù)載到不同的機(jī)器去
# 在nginx.conf 引入啟動后(main context);mysql 連接 127.0.0.1:9000(當(dāng)然是可行的)
# 原本打算是試下UDP DNS的,但是手頭沒Linux環(huán)境所以...
stream { # 必須出現(xiàn)在main context,意思是說和http event等一個級別
    server{
        listen 127.0.0.1:9090;
        proxy_pass mysql;
    }
    upstream mysql{ #TCP負(fù)載均衡服務(wù)器組
        hash $remote_addr consistent; #
        server 127.0.0.1:3306 weight=5 max_fails=2 fail_timeout=30s slow_start=30s; 
        server 192.168.1.101:3306 ;
    }
    # DNS 服務(wù)
    #server{
    #   listen 127.0.0.1:53;
    #    proxy_pass remote_dns;
    #}
    #upstream remote_dns{ #TCP 負(fù)載均衡服務(wù)器組
    #   hash $remote_addr consistent; #
    #    server 192.168.1.1:53 ; # dns server
    #    server 192.168.0.1:53; # dns server
    #}
}

到此,相信大家對“Nginx相關(guān)命令有哪些”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI