1. 程式人生 > >Nginx的安裝與初步講解

Nginx的安裝與初步講解

可執行程序 存在 ssi cif evel 對應關系 是否 run source

Nginx的安裝與初步講解


1.2 Nginx的基本編譯安裝:
編譯安裝前的準備:
安裝Nginx編譯安裝需要依賴的一些包
[root@localhost ~]## yum install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel -y

切換到存放nginx源碼包的目錄:
使用源碼包中的configure腳本 生成makefile文件。
configure腳本支持以下選項:
--help print this message #打印這個信息

--prefix=PATH set installation prefix #設置安裝路徑

--sbin-path=PATH set nginx binary pathname #設置binary 路徑
--conf-path=PATH set nginx.conf pathname #設置nginx 配置文件路徑
--error-log-path=PATH set error log pathname #設置錯誤日誌路徑
--pid-path=PATH set nginx.pid pathname #設置nginx.pid 路徑
--lock-path=PATH set nginx.lock pathname #設置nginx.lock路徑

--user=USER set non-privileged user for #設置沒有特殊權限的用戶執行worker processes

worker processes
--group=GROUP set non-privileged group for #設置沒有特殊權限的用戶執行worker processes
worker processes

--builddir=DIR set build directory #設置編譯目錄

--with-rtsig_module enable rtsig module #開啟rtsig 模塊
--with-select_module enable select module #開啟select 模塊
--without-select_module disable select module #關閉 select 模塊

--with-poll_module enable poll module #啟用 poll 模塊
--without-poll_module disable poll module #關閉poll 模塊

--with-file-aio enable file AIO support #開啟file AIO支持
--with-ipv6 enable IPv6 support #開啟IPv6 支持

--with-http_ssl_module enable ngx_http_ssl_module #開啟ssl 模塊
--with-http_spdy_module enable ngx_http_spdy_module #開啟spdy模塊
--with-http_realip_module enable ngx_http_realip_module #開啟 realip 模塊
--with-http_addition_module enable ngx_http_addition_module #開啟 addition 模塊
--with-http_xslt_module enable ngx_http_xslt_module #開啟 xslt模塊
--with-http_image_filter_module enable ngx_http_image_filter_module #開啟 image_filter模塊
--with-http_geoip_module enable ngx_http_geoip_module #開啟geoip模塊
--with-http_sub_module enable ngx_http_sub_module #開啟 sub模塊
--with-http_dav_module enable ngx_http_dav_module #開啟dav 模塊
--with-http_flv_module enable ngx_http_flv_module #開啟 flv模塊
--with-http_mp4_module enable ngx_http_mp4_module #開啟 mp4模塊
--with-http_gunzip_module enable ngx_http_gunzip_module #開啟gunzip模塊
--with-http_gzip_static_module enable ngx_http_gzip_static_module #開啟 gzip_static模塊
--with-http_auth_request_module enable ngx_http_auth_request_module #開啟 auth_request(身份驗證請求)模塊
--with-http_random_index_module enable ngx_http_random_index_module #開啟random_index(隨機指數)模塊
--with-http_secure_link_module enable ngx_http_secure_link_module #開啟 secure_link(安全鏈接)模塊
--with-http_degradation_module enable ngx_http_degradation_module #開啟degradation 模塊
--with-http_stub_status_module enable ngx_http_stub_status_module #開啟 stub_status 模塊

--without-http_charset_module disable ngx_http_charset_module #關閉 charset(字符集) 模塊
--without-http_gzip_module disable ngx_http_gzip_module #關閉 gzip模塊
--without-http_ssi_module disable ngx_http_ssi_module #關閉 ssi 模塊
--without-http_userid_module disable ngx_http_userid_module #關閉 userid模塊
--without-http_access_module disable ngx_http_access_module #關閉 access模塊
--without-http_auth_basic_module disable ngx_http_auth_basic_module #關閉 auth_basic (基本身份認證)模塊
--without-http_autoindex_module disable ngx_http_autoindex_module #關閉 autoindex(自動變址)模塊
--without-http_geo_module disable ngx_http_geo_module #關閉 geo模塊
--without-http_map_module disable ngx_http_map_module #關閉 map 模塊
--without-http_split_clients_module disable ngx_http_split_clients_module #關閉 split_clients (劃分客戶端)模塊
--without-http_referer_module disable ngx_http_referer_module #關閉 referer (推薦人)模塊
--without-http_rewrite_module disable ngx_http_rewrite_module #關閉 rewrite (重寫)模塊
--without-http_proxy_module disable ngx_http_proxy_module #關閉 proxy (代理)模塊
--without-http_fastcgi_module disable ngx_http_fastcgi_module #關閉 fastcgi模塊
--without-http_uwsgi_module disable ngx_http_uwsgi_module #關閉 uwsgi 模塊
--without-http_scgi_module disable ngx_http_scgi_module #關閉 scgi 模塊
--without-http_memcached_module disable ngx_http_memcached_module #關閉 memcached 模塊
--without-http_limit_conn_module disable ngx_http_limit_conn_module # 關閉 limit_conn 模塊
--without-http_limit_req_module disable ngx_http_limit_req_module #關閉 limit_req模塊
--without-http_empty_gif_module disable ngx_http_empty_gif_module #關閉 empty_gif 模塊
--without-http_browser_module disable ngx_http_browser_module #關閉 browser 模塊
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module #關閉 ip_hash模塊
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module #關閉 least_conn 模塊
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module #關閉 keepalive 模塊

--with-http_perl_module enable ngx_http_perl_module #開啟perl 模塊
--with-perl_modules_path=PATH set Perl modules path #設置perl模塊路徑
--with-perl=PATH set perl binary pathname #設置perl 命令路徑

--http-log-path=PATH set http access log pathname #設置訪問日誌 路徑
--http-client-body-temp-path=PATH set path to store
http client request body temporary files #設置存儲路徑 http客戶端請求主體臨時文件
--http-proxy-temp-path=PATH set path to store
http proxy temporary files #設置存儲路徑 http代理臨時文件
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files #設置 fastcgi臨時文件存儲路徑
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files #設置 uwsgi 臨時文件存儲路徑
--http-scgi-temp-path=PATH set path to store
http scgi temporary files #設置 scgi 臨時文件存儲路徑

--without-http disable HTTP server #關閉 http server
--without-http-cache disable HTTP cache #關閉 http cache

--with-mail enable POP3/IMAP4/SMTP proxy module #開啟 POP3/IMAP4/SMTP 協議模塊
--with-mail_ssl_module enable ngx_mail_ssl_module #開啟 nginx mail ssl 模塊
--without-mail_pop3_module disable ngx_mail_pop3_module #關閉 mail pop3模塊
--without-mail_imap_module disable ngx_mail_imap_module #關閉 mail imap 模塊
--without-mail_smtp_module disable ngx_mail_smtp_module #關閉 mail smtp 模塊

--with-google_perftools_module enable ngx_google_perftools_module #開啟 nginx google_perftools (谷歌性能工具)模塊
--with-cpp_test_module enable ngx_cpp_test_module #開啟 cpp_test 模塊

--add-module=PATH enable an external module #開啟external(外部)模塊

--with-cc=PATH set C compiler pathname #設置C 編譯器 路徑
--with-cpp=PATH set C preprocessor pathname #設置 C預編譯器 路徑
--with-cc-opt=OPTIONS set additional C compiler options #設置額外的C編譯器選項
--with-ld-opt=OPTIONS set additional linker options #設置額外的鏈接器選項
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64 #建立指定的CPU 有效值:

--without-pcre disable PCRE library usage #關閉 PCRE 庫 使用
--with-pcre force PCRE library usage #強制使用指定的PCRE 庫
--with-pcre=DIR set path to PCRE library sources #指定 pcre 使用路徑
--with-pcre-opt=OPTIONS set additional build options for PCRE #為PCRE設置額外的構建選項
--with-pcre-jit build PCRE with JIT compilation support #構建PCRE JIT編譯的支持

--with-md5=DIR set path to md5 library sources #設置路徑md5庫資源
--with-md5-opt=OPTIONS set additional build options for md5 #為md5設置額外的構建選項
--with-md5-asm use md5 assembler sources #使用md5匯編源

--with-sha1=DIR set path to sha1 library sources #設置 sha1 庫的路徑
--with-sha1-opt=OPTIONS set additional build options for sha1 #為sha1設置額外的構建選項
--with-sha1-asm use sha1 assembler sources #使用sha1匯編源

--with-zlib=DIR set path to zlib library sources #指定 zlib 庫的路徑
--with-zlib-opt=OPTIONS set additional build options for zlib #為zlib 設置額外的構建選項
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro #使用 zlib匯編優化的來源

--with-libatomic force libatomic_ops library usage #強制 使用libatomic
--with-libatomic=DIR set path to libatomic_ops library sources #設置 libatomic 路徑

--with-openssl=DIR set path to OpenSSL library sources #設置 openssl 庫的路徑
--with-openssl-opt=OPTIONS set additional build options for OpenSSL #為 openssl 設置額外的構建選項

--with-debug enable debug logging #開啟debug 日誌

我在生成makefile文件時,使用以下編譯參數
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --with-debug

這些編譯參數指定了:
--prefix=/usr/local/nginx #nginx的安裝路徑
--conf-path=/etc/nginx/nginx.conf #指定nginx.conf的路徑
--user=nginx --group=nginx #指定nginx進程的屬主和屬組
--error-log-path=/var/log/nginx/error.log #指定錯誤日誌路徑
--http-log-path=/var/log/nginx/access.log #指定訪問日誌路徑
--pid-path=/var/run/nginx/nginx.pid #指定nginx.pid路徑
--lock-path=/var/lock/nginx.lock #指定nginx鎖文件路徑
--with-http_ssl_module #聲明啟用http的ssl模塊
--with-http_stub_status_module #聲明啟用Server Status頁
--with-http_gzip_static_module #聲明啟用http的gzip模塊
--with-http_flv_module #聲明啟用http的flv模塊
--with-http_mp4_module #聲明啟用http的mp4模塊
--http-client-body-temp-path=/var/tmp/nginx/client #設置存儲路徑 http客戶端請求主體臨時文件
--http-proxy-temp-path=/var/tmp/nginx/proxy #設置存儲路徑 http代理臨時文件
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi #設置 fastcgi臨時文件存儲路徑
--with-debug #聲明啟用nginx的調試日誌

編譯並安裝nginx
[root@localhost nginx-1.12.2]# make && make install

到此為止,我們我們的nginx程序就安裝好了。

Nginx的配置文件:
NGINX配置文件:
/etc/nginx/nginx.conf 主配置文件
/etc/nginx/conf.d/.conf (需要在主配置文件中 使用include conf.d/.conf)
/etc/nginx/fastcgi_params Fastcgi 配置文件
/etc/logrotate.d/nginx Nginx日誌輪轉,用於logrotate服務的日誌切割
/etc/nginx/mime.types 設置http協議的Content-Type與拓展名對應關系
/usr/lib64/nginx/modules /etc/nginx/modules Nginx模塊目錄
/var/cache/nginx Nginx的緩存目錄
/var/log/nginx Nginx的日誌目錄

Nginx服務的啟停控制
通過信號機制來實現:
Nginx服務在運行時,會保持一個主進程和一個或多個worker process工作進程。
我們通過給Nginx服務的主進程發送信號就可以控制服務的啟停了。
NGINX控制信號:
TERM,INT 快速關閉
QUIT 從容關閉
HUP 用新的配置開始新的工作進程,從容關閉舊的工作進程
USR1 重新打開日誌文件
USR2 平滑升級可執行程序
WINCH 從容關閉工作進程

2行

	使用語法:kill -信號 nginx主進程號

通過命令控制:
nginx -s [ stop|quit|reopen|reload]
stop:強制停止NGINX,不管工作進程當前是否在處理用戶請求,都會立即退出
quit:優雅地退出NGINX,工作進程會將當前正在處理的請求處理完畢後,再退出
reload:重載配置信息
reopen;重新打開日誌文件
nginx -t 不運行,僅僅測試配置文件
nginx -c /path/to/config 為nginx指定一個配置文件,來代替缺省的
nginx -v 查看NGINX版本
nginx -V 查看nginx編譯參數

Nginx主配置文件的配置釋義:
Nginx配置文件中,
全局生效的指令:
正常運行的必備配置:
正常運行的必備配置:
1、user USERNAME [GROUPNAME];
指定運行worker進程的用戶 和組,例如:
user nginx nginx;
只有被設置的用戶或組成員才有權限啟動Nginx進程。

43行

			如果希望所有用戶都可以啟動Nginx進程,有倆種方法:
				一種是將此行註釋掉;
				一種是將用戶和組設置為nobody;
				
				
				
			
		2、pid /path/to/pid_file;
			指定nginx的pid文件;
			
		3、worker_rlimit_nofile #;
			指定一個worker進程所能夠打開的最大文件句柄數;
			
		4、worker_rlimit_sigpending #;
			指定每個用戶能夠發往worker的信號的數量;
			
			
	優化性能相關的配置:
		1、worker_processes #|auto:
			worker線程的個數;通常應該為物理CPU核心個數減1;
			可設置為auto,表示自動分配
			
		2、worker_cpu_affinity cpumask ...;
			綁定worker進程至指定的CPU上;
				CPUMASK(cpu掩碼)
					0001
					0010
					0100
					1000 
				例如:
					worker_cpu_affinity 00000001 00000010 00000100;
				優點:提升緩存的命中率
					
		3、timer_resolution t; 計時器解析度
			gettimeofday(); 
			
		4、worker_priority nice;設定worker優先級範圍
			取值範圍:-20, 19  數字越大優先級越小,默認為0
			
配置文件的引入:
	include file;
	file表示要引入的配置文件。

event相關配置
events {
事件驅動相關配置
}
1、accept_mutex [on|off]
master調度用戶請求至各worker時用的負載均衡鎖;on表示能讓多個worker輪流地、序列化地與響應新請求;

20行

		2、lock_file /path/to/lock_file; 指定鎖文件的存放位置
		
		3、accept_mutex_delay #ms;
			
		4、use [epoll|rgsig|select|poll];
			定義使用的事件模型;建議讓Nginx自動選擇;
			
		5、worker_connections #;
			每個worker進程所能夠響應的最大並發請求數;
			
	用於調試、定位問題:
		1、daemon [off|on]
			是否以守護進程方式啟動nginx;調試時應設置為off
			
		2、master_process on|off;
			是否以master/worker模型來運行nginx; 調試時可以設置為off
			
		3、error_log /path/to/error_log level;
			錯誤日誌文件及其級別;出於調試的目的,可以使用debug級別,但此級別只有在編譯nginx時使用了--with-debug選項才有效;

http {
}

100行

http配置:http core 配置一個靜態web服務器
	ngx_http_core_module
	
	配置框架:
	http {
		upstream {
			.,..
		}
		
		server {
			listen IP:PORT;
			# 虛擬主機配置   NGINX沒有中心主機,只有虛擬主機
			location /URL {
				if ...{
					...
				}
				root "/path/to/somewhere";  #根目錄
				...
			}
		}
		server {
			,,.
		}
	}
	
		註意:與http配置相關的指令必須放在http、server、location、upstream、if塊中;
		
	虛擬主機相關的配置:
		1、server {}
			定義一個虛擬主機;
			
		2、listen
			監聽的端口
			完整格式 :listen address[:port] [default_server] [ssl] [spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
			
			listen address[:port] [default_server] ssl 
			ssl:用於限制只能通過ssl連接提供服務
			backlog=number: 指明TCP協議backlog隊列的大小。默認為-1,表示不設置;
			rcvbuf=size:設定監聽句柄的SO_RCVBUF參數;
			default_server:設定默認虛擬主機
			例如:
				listen 172.16.100.8:80 default_server;   默認虛擬主機
				
		3、server_name name [...];
			後可跟多個主機名;名稱還可以使用通配符和正則表達式(~);
			當存在多個主機名時,主機名的匹配優先順序:	
			(1) 先做精確匹配;www.magedu.com: 
			(2) 左側通配符匹配,例如:*.magedu.com; 
			(3) 右側通配符匹配,例如:www.*;
			(4) 正則表達式匹配,例如: ~^.*\.magedu\.com$  (使用正則表達式匹配,要使用"~"起頭)
			(5) default_server
			
		4、location [=|~|~*|^~] /uri {...}
			location @name
			功能:允許根據用戶請求的URI來匹配定義的各location,匹配到時,此請求將被相應的location塊中的配置所處理;
			      (為每個URI提供單獨配置)
				=: 精確匹配檢查;
				~: 正則表達式模式匹配,區分字符大小寫;
				~*:正則表達式模式 匹配,不區分字符大小寫;
				^~:URI的左半部分匹配,不檢查正則表達式;
				
			匹配優先級:精確匹配(=)、^~、~和~*、由不帶符號的URL進行左側匹配;
				
		5、root
			設置web資源路徑映射;用於指明請求的URL所對應的文檔的目錄路徑;
			
			root /PATH/TO/DOCUMENTROOT;

			http://www.magedu.com/images/logo.jpg--->/PATH/TO/DOCUMENTROOT/images/logo.jpg
			

			location /images/ {
				root /data/imgs/;  表示訪問/inages/路徑下的資源,均來自/data/imgs/
			}
             http://www.magedu.com/images/logo.jpg--->/data/imgs/images/logo.jpg

		6、alias path
			只能用於location配置段,定義路徑別名 
			
			location /images/ {
				alias /www/pictures/;
			}
			
			  /images/1.jpg--->/www/pictures/1.jpg
			註意:root表示指明路徑為對應location的"/"URL;alias表示路徑映射,即location中的URL是相對於alias所指明的路徑而言;
			
		7、index file
			默認主頁面
				index index.html; 
				
		8、error_page code [...] [=code] URI | @name
			根據http響應狀態碼重定向錯誤頁面
			error_page  404   /404.html
			error_page  410=301  /abc.gif  #Nginx服務器產生410的http消息時,使用Nginx安裝路徑下/html/abc.gif返回給用戶端301消息。
			=[code]: 以指定的響應碼進行響應;省略code表示以新資源的響應碼為響應碼;
			
		9、try_files
			try_files path1[,path2,...] URI
				嘗試查找第一個至N-1個文件,第一個即為返回給請求者的資源,如1至N-1個文件均不存在,則跳轉至最後一個uri(應該由其他location定義,不能匹配當前location,否則會導致死循環)

配置Nginx的訪問權限:

8行

ngx_http_access模塊的配置(基於IP的訪問控制)		
  allow address | CIDR | unix: | all;
  deny address | CIDR | unix: | all;
  
   註:Nginx在配置解析的過程中,遇到deny指或者allow指令是按照順序對當前客戶端的連接進行訪問權限檢查的,如果遇到匹配的配置時,則停止繼續向下搜索相關配置。

  應用上下文:http,server,location,limit_except

ngx_http_auth_basic_module模塊的配置(基於用戶的basic認證配置)

16行

	auth_basic  string | off  #開啟認證功能並配置指示信息,或者關閉認證功能
		使用http basic認證協議對用戶進行認證
	auth_basic_user_file file #定義認證文件(file為密碼文件的絕對路徑)
	 這裏的密碼文件支持明文或者密碼加密後的文件,明文的格式如下:
	    認證文件格式:
	    	name1:passwd1
	    	name2:passwd2[;註釋]

		可以htpasswd命令創建用戶賬號加密文件;

		範例:
			location /admin/ {
				auth_basic "Admin Area"
				auth_basic_user_file /etc/nginx/.nginxhtpasswd;
			}
			
			
			
			
			
			
			
			
			-----------------------------本文章整理自馬哥教育和<<Nginx 高性能web服務器詳解釋>> 版權歸原作者所有---------------------------------------------


Nginx的安裝與初步講解