nginx:
誕生背景:
prefork機制不能支持過大的并發請求,
C10K問題的解決
官方站點:
二次開發版:
tengine,openresty
特性:
模塊化設計,較好的拓展性
高可靠性:master/worker架構
支持熱部署:不停機更新配置文件,更換日至文件,更新服務器版本
低內存消耗:10000個keep-alived連接模式下的非活動鏈接僅消耗2.5M內存
event-driven機制,支持事件驅動
基本功能:
靜態資源的web服務器
作為httd反向代理服務器
pop3/imap4協議反向代理服務器(郵件存取服務)
FastCGI(LNMP),uWSGI等協議
模塊化機制(非DSO動態模塊拓展機制),著名的模塊例如:zip,ssl..
web相關功能:虛擬主機,keepalive,訪問日志,url重寫,路徑別名,基于ip及用戶的訪問控制,速率限制和并發數限制
##代理和反向代理的概念
正向代理:snat代理本地用戶訪問互聯網
反向代理:dnat代理服務器負責從后端服務器請求數據
程序架構:
master/worker架構
一個master進程可以生成一個或者多個worker進程
master:加載配置文件,管理worker進程,平滑升級
worker:http服務,http代理,fastcgi代理
模塊類型:
核心模塊:core modules
標準模塊:
standard http modules
optional http modules
mail modules
3rd party modules:第三方模塊
用來做什么:
靜態資源服務器
http協議反向代理
一、安裝nginx
系統環境和軟件選擇:
centos7
yum源:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
編譯選項:
–prefix=/usr/local #安裝位置
–sbin-path=/usr/sbin/nginx #啟動腳本定義位置
–conf-path=/etc/nginx/nginx.conf #配置文件定義位置
–error-log-path=/var/log/nginx/error.log #錯誤日志定義位置
–http-log-path=/var/log/nginx/access.log #訪問日志位置
–pid-path=/var/run/nginx.pid #pid文件位置
–lock-path=/var/run/nginx.lock #lock文件位置
–http-client-body-temp-path=/var/cache/nginx/client_temp #客戶端緩存位置
–http-proxy-temp-path=/var/cache/nginx/proxy_temp #代理緩存位置
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp #fastcgi緩存位置
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #uwsgi緩存位置
–http-scgi-temp-path=/var/cache/nginx/scgi_temp #scgi緩存位置
–user=nginx #所屬用戶
–group=nginx #所屬組
–with-http_ssl_module #安裝ssl模塊
–with-http_realip_module #realipmo模塊
–with-http_addition_module #附加模塊
–with-http_sub_module #其他子模塊
–with-http_dav_module #http_dav模塊
–with-http_flv_module #flv文件模塊
–with-http_mp4_module #mp4模塊
–with-http_gunzip_module #壓縮模塊
–with-http_gzip_static_module #靜態壓縮模塊
–with-http_random_index_module #隨機index模塊
–with-http_secure_link_module #安全連接模塊
–with-http_stub_status_module #http狀態模塊
–with-http_auth_request_module #http認證請求模塊
–with-threads #線程模塊
–with-stream #stream模塊
–with-stream_ssl_module #stream_ssl模塊
–with-http_slice_module #slice模塊
–with-mail #郵件模塊
–with-mail_ssl_module #郵件加密模塊
–with-file-aio #aio模塊(異步io模塊)
–with-http_v2_module #http2模塊
編譯安裝nginx:
yum install openssl-devel pcre-devel zlib-devel -y
yum groupinstall "Development Tools" "Server Platform Development" -y #基本環境安裝
useradd -s /sbin/nologin -M nginx
下載好源碼編譯安裝包:
tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1/
./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_gzip_static_module –with-http_stub_status_module #開始編譯
make && make install
cd /usr/local/nginx/ #去安裝路徑下
/usr/local/nginx/sbin/nginx -t #配置文件測試
/usr/local/nginx/sbin/nginx -h #幫助
/usr/local/nginx/sbin/nginx -s stop, quit, reopen, reload #四種狀態
/usr/local/nginx/sbin/nginx #啟動
/usr/local/nginx/sbin/nginx -s stop #停止
ps -ef | grep nginx #查進程
cd /etc/nginx/ #配置文件目錄
cp nginx.conf nginx.conf.bak #初始文件備份
##配置文件的組成部分:
大型服務的配置文件片段化:
vim /etc/nginx/nginx.conf
include conf.d/*.conf #片段化配置文件
fastcgi,scgi,uwscgi相關配置
配置指令(必須以分號結尾):
directive value1 [value2];
支持變量使用:
內置變量:由模塊引入,可直接調用
自定義變量:set variables_name value;
引用變量:$value_name
##配置文件結構:
main block :對http以及mail模塊均有效
event{
…
} :事件驅動類型配置
http{
…
} :http相關配置
mail{
…
} :郵件相關配置
***http相關配置:
http{
…
…
server {
listen port;
root path; #根文件位置
server_name hostname; #主機名
alias #別名配置
location / { #location配置
…..
}
}
server{
…
}
}
###main配置端的解析
main block:
配置指令的類別:
正常運行必備的配置;
優化性能的配置;
用于調試,定位問題的配置
(1)正常運行必備的配置:
user nginx nginx; #用戶配置
pid /path/to/PID_FILE; #指定pid 文件位置
worker_rlimit_nofile number; #單個worker進程能夠打開的最大文件數
(2)性能優化方面的配置 :
worker_processes auto; #指明工作進程數能使用auto(1.8以上)cpu核心數-1
worker_cpu_affinity auto; #cpu_mask就是cpu個數的二進制(二核心:0001 0010)cpu親緣性綁定
worker_priority nice ; #進程優先級的指定(nice(-20~19對應(100-139)))
(3)調試定位問題的配置:
deamon on|off; #是否以守護進程模式運行nginx(on)
master_process off; #啟動master進程(off)
error_log file 模式; #錯誤日志文件的記錄方式和級別(debug級別需要編譯的時候加載)
#方式:stderr:發送到錯誤輸出
syslog:server=address[,paraameter=value] #發送給syslog服務器
memory:size #計入到內存中(性能比較好,降低磁盤io)
#日志級別:debug 依賴于configure的debug模式的開啟
原創文章,作者:wanghui,如若轉載,請注明出處:http://www.www58058.com/53982
希望能加入一些自己的理解,排版可以在好一些的哈,加油!