天天看點

nginx 基礎配置項詳解nginx.conf檔案中的預設配置資訊nginx.conf檔案的規範結構

nginx.conf檔案中的預設配置資訊

worker_process		1;		#此配置項全局生效

events{						#以下配置隻在events部分中生效
	worker_connections;		1024		
}

http{						#以下配置隻在http部分中生效
	include		mime.types;
	default_type		application/octet-stream;
	sendfile		on;
	keepalive_timeout		65;
	server{					#以下配置隻在http部分的server部分中生效
		listen 80;
		server_name  localhost;
		location /{			#以下配置隻在http/server部分的location部分中生效
			root  html;
			index index.html  index.htm;
		}
		error_page  500 502 503  504  /50x.html
		location = /50x.html{
			root html;
		}
	}
}
           

nginx.conf檔案的規範結構

... 		#全局塊
events{}	#events塊
http{		#http塊
	server{ #server塊
		location [pattern] {}  #location塊
		location [pattern] {}  #location塊
	}
	server{}
}
           

全局塊

主要配置:pid檔案位置、允許的工作程序數、日志檔案位置、引入其他配置檔案。

  • pid pidfile_path:設定存儲nginx服務程序号的檔案路徑
  • worker_processes number:設定nginx服務啟動後開啟的工作程序數,工作程序是用來處理用戶端連接配接請求及url請求的
  • user username groupname:授權給特定使用者運作nginx服務的權限。如果沒有設定則預設授權所有使用者,或者user nobody nobody也是授權所有使用者
  • error_log log_file_path | stderr :設定存儲錯誤日志的檔案路徑,或者将錯誤日志輸出到标準錯誤
  • include other_configfile_path:引入其他配置檔案

events塊

此塊中的配置主要影響用戶端與nginx的網絡連接配接,對nginx的性能影響較大,需要根據實際情況靈活調整。

  • worker_connections number:每個工作程序可以同時保持的最大連接配接數。預設500.
  • access_mutex on | off :防止
  • multi_access on | off :是否允許工作線程并發地接收用戶端連接配接請求

http塊

代理、緩存、日志定義等絕大多數的功能和第三方子產品的配置。

  • keepalive_timeout number1 number2 :當工作程序與某個用戶端建立會話後,此會話的保持時間。number2将被添加到響應頭中的"Keep-Alive: timeout=number2"中
  • keepalive_requests:當工作程序與客戶單建立會話後,在單個會話上允許處理的最大請求數。預設100。
  • sendfile on | off :是否調用sendfile()方式傳輸檔案
  • sendfile_max_chunk size:每次調用sendfile()時傳輸資料最大位元組數。如果size小于等于0,則無限制。

server塊

配置虛拟主機名

基于名稱的虛拟主名配置

server_name name1 name2 …:配置虛拟主機名,這裡的“虛拟主機”就是server塊對外提供的虛拟主機。當設定了虛拟主機名後,必須為其配置好DNS,然後用戶端就可以向這個虛拟主機名發請求了。

基于IP的虛拟主機配置

server_name ip1:需要先為每台虛拟主機(即server塊所代表的虛拟主機)邏輯地設計一個不同的IP位址,然後将nginx服務所在機器的網卡設定為能同時監聽多個IP位址。

使用指令

ifconfig ens33:0 ip1 netmask 255.255.255.0 up

來為網卡設定更多IP,其中up參數表示立即生效。

這種方式配置後,機器重新開機後就配置就沒有了。要想讓這個配置永久生效,可以将此指令添加到rc.local中:

echo "ifconfig ens33:0 ip1 netmask 255.255.255.0 up" >> /etc/rc.local
           

linux啟動後會執行rc.local中的所有指令。

使用ifconfig工具為同一塊網卡添加多個IP别名,首先檢視目前使用的網卡的監聽的IP位址:

ifconfig			#列印網絡配置資訊
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.174.200  netmask 255.255.255.0  broadcast 192.168.174.255
        inet6 fe80::20c:29ff:fe7f:f753  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:7f:f7:53  txqueuelen 1000  (Ethernet)
        RX packets 9919  bytes 878654 (858.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6606  bytes 982272 (959.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 224  bytes 19448 (18.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 224  bytes 19448 (18.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

           

可以看到ip為192.168.174.200,然後我再為此網卡添加要監聽的兩個IP

ifconfig ens33:0 192.168.174.211 netmask 255.255.255.0 up
ifconfig ens33:1 192.168.174.212 netmask 255.255.255.0 up
           

再次檢視網卡資訊:

ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.174.200  netmask 255.255.255.0  broadcast 192.168.174.255
        inet6 fe80::20c:29ff:fe7f:f753  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:7f:f7:53  txqueuelen 1000  (Ethernet)
        RX packets 9887  bytes 875982 (855.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6579  bytes 976520 (953.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.174.211  netmask 255.255.255.0  broadcast 192.168.174.255
        ether 00:0c:29:7f:f7:53  txqueuelen 1000  (Ethernet)

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.174.212  netmask 255.255.255.0  broadcast 192.168.174.255
        ether 00:0c:29:7f:f7:53  txqueuelen 1000  (Ethernet)
           

location塊

location [ = | ~ | ~* | ^~ ] uri {...}

nginx會對使用者請求字元串"/server_name/uri"中的uri與location後的資料做比對,如果根據規則,某個請求的url字元串比對到了目前location,則伺服器就會使用此location中的配置對請求進行處理。

location後的uri有兩種,一種是标準比對,一種是正則比對。

當uri前的[]中的選項為空時,規則如下:

當一個請求到來,會先找到并記錄标準比對中比對度最高的location。然後,進行正則比對,找到第一個正則比對的,即結束搜尋,根據第一個正則比對的location處理請求。如果正則比對全部失敗,則使用标準比對度最高的location處理請求。

[]中的可選項,是用來改變比對方式的,其中的每個可選項的含義如下:

  • =,用于标準uri前,如果使用者請求的uri與此uri标準比對,則立即使用此location處理請求,結束搜尋。
  • ~,用于表示uri包含正規表達式,并且區分大小寫
  • ~*,用于表示uri包含正規表達式,并且不區分大小寫
  • ^~,如果找到了标準比對度最高的location,則直接使用此location處理請求,不再進行正則比對的搜尋。

注意:如果uri包含正規表達式,則其前必須加上可選項~*或者~。

配置根目錄

root path

# path為Nginx伺服器接收到請求後查找資源的根目錄路徑。

此指令可以在http、server、location中配置,一般配置在location中。

location /data/
{
	root  /locationtest1;		# 當location接收到uri為"/data/index.html"請求時,将在/locationtest1/data/目錄下查找index.htm來響應。
}
           

将uri路徑替換成别名

location ~^/data/(.+\.(htm|html))$
{
	alias  /locationtest1/other/$1
}
           

當location塊接收到uri為"/data/index.html"的請求時,會到/locationtest1/other/目錄下查找index.html檔案

設定網站的預設首頁

index file...

# 如果沒有設定,則預設為index.html。如果設定了多個file,則首先找到了哪個file,就用哪個file響應請求。

設定預設首頁可以有兩個作用:使用者通路網站時,請求位址可以不寫首頁名稱;二是,對一個請求,可以根據其請求内容設定不同的首頁。

設定錯誤頁面

error_page code1 code2 [=[response]] uri

code1 code2是指錯誤代碼

response 是指将code指定的錯誤代碼轉化為新的錯誤代碼response

uri:響應給用戶端的錯誤頁面的路徑或者網址,如果是一個網址,nginx會通路該網址擷取頁面并響應給用戶端。

error_page 404 /404.html

# 當遇到404時,nginx會去找 nginx安裝路徑/html/404.html 檔案,然後響應給前端。

如果錯誤頁面404.html不是放置在 nginx安裝路徑/html/ 目錄中的,可以在erro_page指令下加上一個location塊,來指定404頁面的位置:

location /404.html

{

root /myserver/errorpages/

}

繼續閱讀