nginx配置文件詳解

Nginx的主配置文件由指令與指令塊構成,指令塊以{ }大括號將多條指令組織在一塊兒php

· 每條指令以;分號結尾,指令與參數間用空格分隔html

· 支持include語句組合多個配置文件,提高可維護性node

· #表示註釋,$表示變量,部分指令的參數支持正則表達式nginx

三、編譯安裝Nginx,經常使用選項: 正則表達式

--prefix=path:設置Nginx的安裝路徑,不寫的話默認是在/usr/local/nginx
--sbin-path=path:設置Nginx的可執行文件路徑,默認路徑是prefix/sbin/nginx
--conf-path=path:設置Nginx配置文件路徑,默認路徑是prefix/conf/nginx.conf
--pid-path=path:設置Nginx pid文件路徑,默認路徑是prefix/logs/nginx.pid
--error-log-path=path:設置錯誤日誌存放路徑,默認路徑是prefix/logs/error.log
--http-log-path=path:設置訪問日誌存放路徑,默認路徑是prefix/logs/access.log
--user=name:設置運行Nginx的用戶,默認用戶是nobody
--group=name:設置運行Nginx的用戶組,默認用戶組是nobody
  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module瀏覽器

五、Nginx主配置文件(/usr/local/nginx/conf/nginx.conf)詳解:緩存

events
│
http
├── server1
│   ├── location1 標籤是作匹配    
│   ├── location2
│   
├── server2

每一個主機都配置在server標籤裏bash

Location標籤的匹配規則:
服務器

=  #精確匹配,優先級最高
^~  #普通字符串匹配,禁止正則表達式,當匹配成功後中止其餘location匹配,優先級高於正則
~  #區分大小寫的正則匹配
~*  #不區分大小寫的正則匹配

未作任何配置的配置文件:cookie

#user  nobody;    #默認運行Nginx的用戶名
worker_processes  1; #開啓的進程數,一般和cpu個數相等 events { worker_connections 1024; #每一個進程的併發數 } http { include mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #長鏈接超時時間爲65秒 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

 

user nginx;  #運行Nginx的用戶
worker_processes  2;    #開啓的進程數,一般和cpu個數相等或者設置爲auto
worker_cpu_affinity auto;  #自動進行CPU親和設置
#worker_cpu_affinity 0000000000000001 000000000000010 #手動進行CPU親和設置
error_log  logs/error.log warn;  #Nginx服務的錯誤日誌路徑與記錄級別
pid  /var/run/nginx.pid;
worker_rlimit_nofile 65535;  #設置Nginx進程文件句柄數
events {
    worker_connections  10240;    #每一個進程的併發數
}

http {
  include       mime.types;
  default_type  application/octet-stream;
  charset utf-8;
  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;  #訪問日誌存放路徑與日誌記錄格式,這裏main就是上一步log_format所定義的main
  sendfile        on;
  tcp_nopush     on;  #一次傳輸多個數據包,提升傳輸效率
  #tcp_nodeley    off  #與tcp_nopush相反,實時性要求比較高的場景會打開這個
  keepalive_timeout  65;    #長鏈接超時時間爲65秒
  gzip  on;  #打開gzip後經過瀏覽器開發者工具-網絡功能能夠看到size大小被壓縮了,對文本類型的文件壓縮效率最高,可做用於location中
  include /etc/nginx/conf.d/*.conf  #conf.d目錄下的配置文件也會生效
  server {
      listen       80;
      server_name  localhost;
      #charset koi8-r;
      access_log  logs/access.log  main;  #單獨對主機記錄日誌
      location ~ .*\.(jpg|gif|png)$ {
          gzip  on;
          expires 24h;  #開啓緩存,若是是取的緩存數據,瀏覽器開發者工具中返回狀態是304
          root   html;
          index  index.html index.htm;
      }
      #error_page  404              /404.html;
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
        }

1.rewrite的介紹

nginx的rewrite規則就是使用正則匹配請求的url,而後根據定義的規則進行重寫和改變,需ngx_http_rewrite_module模塊來支持url重寫功能,該模塊是標準模塊,默認已經安裝。

url和uri的區別:

URI:Universal Resource Identifier ,通用資源標識符,用於對網絡中的各類資源進行標識,由存放資源的主機名、片斷標誌符和相對的URI三部分組成。存放資源的主機名通常由傳輸協議(Scheme)、主機和資源路徑三部分組成;片斷標識符指向資源內容的具體元素、相對URI表示資源在主機上的相對路徑。通常格式爲:Scheme://[用戶名][:密碼]@主機名[:端口號][/資源路徑]

URL:Uniform Resource Location,統一資源定位符,是用於在Internet中描述資源的字符串,是URI的子集,主要包括傳輸協議(Scheme)、主機(IP、端口號或者域名)和資源集體地址(目錄或文件名)等三部分,通常格式爲:scheme://主機名[:端口號]/[資源路徑]

2.rewrite涉及的指令

執行順序:

1.執行server塊的rewrite指令(這裏的塊指的是server關鍵字後{}包圍的區域,其它xx塊相似)
2.執行location匹配
3.執行選定的location中的rewrite指令
若是其中某步URI被重寫,則從新循環執行1-3,直到找到真實存在的文件

若是循環超過10次,則返回500 Internal Server Error錯誤

1)if指令

    語法:if(condition){...}

默認值:無

做用域:server,location

對給定的條件condition進行判斷。若是爲真,大括號內的rewrite指令將被執行。

if條件(conditon)能夠是以下任何內容:

 

一個變量名;false若是這個變量是空字符串或者以0開始的字符串;

使用= ,!= 比較的一個變量和字符串,true/false

使用~, ~*與正則表達式匹配的變量,若是這個正則表達式中包含右花括號}或者分號;則必須給整個正則表達式加引號

使用-f ,!-f 檢查一個文件是否存在

使用-d, !-d 檢查一個目錄是否存在

使用-e ,!-e 檢查一個文件、目錄、符號連接是否存在

使用-x , !-x 檢查一個文件是否可執行

if指令實例

 if ($http_user_agent ~ MSIE) {             

     rewrite ^(.*)$ /msie/$1 break;

 }

 if ($http_cookie ~* "id=([^;]+)(?:;|$)") {

     set $id $1;

 }

 if ($request_method = POST) {

     return 405;

 }

 if ($slow) {

     limit_rate 10k;

 }

2)return指令

用於完成對請求的處理,直接給客戶端返回狀態碼,改指令後全部的nginx配置都是無效的,

語法:return code;

return code URL;

return URL;

默認值:無

做用域:server,location,if

3)set指令

 

語法:set variable value;

默認值:none

做用域:server,location,if

定義一個變量並賦值,值能夠是文本,變量或者文本變量混合體。

 

4)uninitialized_variable_warn指令

語法:uninitialized_variable_warn on | off;
默認值:uninitialized_variable_warn on
做用域:http,server,location,if
控制是否輸出爲初始化的變量到日誌

5)rewrite指令

該指令經過正則來改變url,能夠同時存在一個或者多個指令

語法:rewrite regex replacement [flag];

默認值:無

做用域:server,location,if

regex :用於匹配uri的正則表達式。使用括號()標記要截取的內容

replacement 匹配成功後用於替換uri中被截取內容的字符串,默認狀況下,若是該字符串是由http://或者https://開頭的,則不會繼續向下對uri進行其餘處理,而是直接將重寫後的uri返回給客戶端

flag 用來設置rewrite對uri的處理行爲,經常使用的有

    last 中止處理後續rewrite指令集,而後對當前重寫的新URI在rewrite指令集上從新查找。

break 中止處理後續rewrite指令集,並不在從新查找,可是當前location內剩餘非rewrite語句和location外的的非rewrite語句能夠執行。

redirect 若是replacement不是以http:// 或https://開始,返回302臨時重定向

permant 返回301永久重定向

 補充:last和break標記的區別在於,last標記在本條rewrite規則執行完後,會對其所在的server { … } 標籤從新發起請求,而break標記則在本條規則匹配完成後,中止匹配,再也不作後續的匹配。另外有些時候必須使用last,好比在使用alias指令時,而 使用proxy_pass指令時則必須使用break。

注意:rewrite 規則優先級要高於location,在nginx配置文件中,nginx會先用rewrite來處理url,最後再用處理後的url匹配location

 

6)經常使用的變量

$args : #這個變量等於請求行中的參數,同$query_string

$content_length : 請求頭中的Content-length字段。

$content_type : 請求頭中的Content-Type字段。

$document_root : 當前請求在root指令中指定的值。

$host : 請求主機頭字段,不然爲服務器名稱。

$http_user_agent : 客戶端agent信息

$http_cookie : 客戶端cookie信息

$limit_rate : 這個變量能夠限制鏈接速率。

$request_method : 客戶端請求的動做,一般爲GET或POST。

$remote_addr : 客戶端的IP地址。

$remote_port : 客戶端的端口。

$remote_user : 已經通過Auth Basic Module驗證的用戶名。

$request_filename : 當前請求的文件路徑,由root或alias指令與URI請求生成。

$scheme : HTTP方法(如http,https)。

$server_protocol : 請求使用的協議,一般是HTTP/1.0或HTTP/1.1。

$server_addr : 服務器地址,在完成一次系統調用後能夠肯定這個值。

$server_name : 服務器名稱。

$server_port : 請求到達服務器的端口號。

$request_uri : 包含請求參數的原始URI,不包含主機名,如:」/foo/bar.php?arg=baz」。

$uri : 不帶請求參數的當前URI,$uri不包含主機名,如」/foo/bar.html」。

$document_uri : 與$uri相同。

7)經常使用正則:

. : 匹配除換行符之外的任意字符

? : 重複0次或1次

+ : 重複1次或更屢次

* : 重複0次或更屢次

\d :匹配數字

^ : 匹配字符串的開始

$ : 匹配字符串的介紹

{n} : 重複n次

{n,} : 重複n次或更屢次

[c] : 匹配單個字符c

[a-z] : 匹配a-z小寫字母的任意一個

小括號()之間匹配的內容,能夠在後面經過$1來引用,$2表示的是前面第二個()裏的內容。正則裏面容易讓人困惑的是\轉義特殊字符。

 

配置案例

1. 在URL結尾添加斜槓

在虛擬主機中這麼添加一條改寫規則:

  1. rewrite ^(.*[^/])$ $1/ permanent;

2. 刪除URL結尾的斜槓

在虛擬主機中這麼添加一條改寫規則:

  1. rewrite ^/(.*)//$1 permanent;

 

-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" \ -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \ -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \ -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \ < man/nginx.8 > objs/nginx.8 make[1]: 離開目錄「/usr/local/nginx-1.14.2」

 

test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \ || mkdir -p '/usr/local/nginx/sbin' test ! -f '/usr/local/nginx/sbin/nginx' \ || mv '/usr/local/nginx/sbin/nginx' \ '/usr/local/nginx/sbin/nginx.old' cp objs/nginx '/usr/local/nginx/sbin/nginx' test -d '/usr/local/nginx/conf' \ || mkdir -p '/usr/local/nginx/conf' cp conf/koi-win '/usr/local/nginx/conf' cp conf/koi-utf '/usr/local/nginx/conf' cp conf/win-utf '/usr/local/nginx/conf' test -f '/usr/local/nginx/conf/mime.types' \ || cp conf/mime.types '/usr/local/nginx/conf' cp conf/mime.types '/usr/local/nginx/conf/mime.types.default' test -f '/usr/local/nginx/conf/fastcgi_params' \ || cp conf/fastcgi_params '/usr/local/nginx/conf' cp conf/fastcgi_params \ '/usr/local/nginx/conf/fastcgi_params.default' test -f '/usr/local/nginx/conf/fastcgi.conf' \ || cp conf/fastcgi.conf '/usr/local/nginx/conf' cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default' test -f '/usr/local/nginx/conf/uwsgi_params' \ || cp conf/uwsgi_params '/usr/local/nginx/conf' cp conf/uwsgi_params \ '/usr/local/nginx/conf/uwsgi_params.default' test -f '/usr/local/nginx/conf/scgi_params' \ || cp conf/scgi_params '/usr/local/nginx/conf' cp conf/scgi_params \ '/usr/local/nginx/conf/scgi_params.default' test -f '/usr/local/nginx/conf/nginx.conf' \ || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf' cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs'
相關文章
相關標籤/搜索