Nginx簡(jiǎn)介
Apache
Apache仍然是時(shí)長(zhǎng)占用量最高的web服務(wù)器锡溯,據(jù)最新數(shù)據(jù)統(tǒng)計(jì)赶舆,市場(chǎng)占有率目前是50%左右。主要優(yōu)勢(shì)在于一個(gè)是比較早出現(xiàn)的一個(gè)Http靜態(tài)資源服務(wù)器祭饭,同時(shí)又是開(kāi)源的芜茵。所以在技術(shù)上的支持以及市面上的各種解決方案都比較成熟。Apache支持的模塊非常豐富倡蝙。
Nginx
Nginx是俄羅斯人編寫(xiě)的一款高性能的HTTP和反向代理服務(wù)器九串,在高連接并發(fā)的情況下,它能夠支持高達(dá)50000個(gè)并發(fā)連接數(shù)的響應(yīng)寺鸥,但是內(nèi)存猪钮、CPU等系統(tǒng)資源消耗卻很低,運(yùn)行很穩(wěn)定析既。目前Nginx在國(guó)內(nèi)很多大型企業(yè)都有應(yīng)用躬贡,據(jù)最新統(tǒng)計(jì),Nginx的市場(chǎng)占有率已經(jīng)到33%左右了眼坏。而Apache的市場(chǎng)占有率雖然仍然是最高的拂玻,但是是呈下降趨勢(shì)。而Nginx的勢(shì)頭很明顯宰译。選擇Nginx的理由也很簡(jiǎn)單:第一檐蚜,它可以支持5W高并發(fā)連接;第二沿侈,內(nèi)存消耗少闯第;第三,成本低缀拭,如果采用F5咳短、NetScaler等硬件負(fù)載均衡設(shè)備的話填帽,需要大幾十萬(wàn)。而Nginx是開(kāi)源的咙好,可以免費(fèi)使用并且能用于商業(yè)用途
架構(gòu)中的作用
介紹nginx在系統(tǒng)架構(gòu)(網(wǎng)關(guān)入口)中的作用篡腌,總結(jié)如下:
1、路由功能(與微服務(wù)對(duì)應(yīng)):域名/路徑勾效,進(jìn)行路由選擇后臺(tái)服務(wù)器
2嘹悼、負(fù)載功能(與高并發(fā)高可用對(duì)應(yīng)):對(duì)后臺(tái)服務(wù)器集群進(jìn)行負(fù)載
3、靜態(tài)服務(wù)器(比tomcat性能高很多):在mvvm模式中层宫,充當(dāng)文件讀取職責(zé)
總結(jié):實(shí)際使用中杨伙,這三項(xiàng)功用,會(huì)混合使用萌腿。比如先分離動(dòng)靜限匣,再路由服務(wù),再負(fù)載機(jī)器
正向代理與反向代理
1毁菱、代理:客戶端自己請(qǐng)求出現(xiàn)困難膛腐。客戶請(qǐng)了一個(gè)代理鼎俘,來(lái)代自己做事,就叫代理辩涝。
比如代理律師贸伐,代購(gòu),政府機(jī)關(guān)辦事的代理人等等怔揩。
2捉邢、反向代理,服務(wù)端推出的一個(gè)代理招牌商膊。
安裝make:
yum -y install autoconf automake make
安裝g++:
yum -y install gcc gcc-c++
安裝nginx依賴的庫(kù)
#正則表達(dá)式模塊
yum -y install pcre pcre-devel
#原始庫(kù)的
yum -y install zlib zlib-devel
#HTTP服務(wù)的包
yum install -y openssl openssl-devel
安裝方式1:源碼安裝
步驟1:下載源碼
wget http://nginx.org/download/nginx-1.9.15.tar.gz
tar -zxvf nginx-1.9.0.tar.gz
cd nginx-1.9.0
步驟2:
編譯伏伐,指定目錄,和安裝模塊
--prefix指定安裝目錄
--with-http_ssl_module安裝https模塊
creating objs/Makefile 代表編譯成功
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
步驟3:安裝
make && make install
whereis nginx
目錄結(jié)構(gòu):
?conf 配置文件
?html 網(wǎng)頁(yè)文件
?logs 日志文件
?sbin 二進(jìn)制程序
啟動(dòng)
sbin/nginx
輸入nginx 服務(wù)器地址:http://106.52.69.141/
啟停命令:
./nginx -c nginx.conf的文件晕拆。如果不指定藐翎,默認(rèn)為NGINX_HOME/conf/nginx.conf
./nginx -s stop 停止
./nginx -s quit退出
./nginx -s reload 重新加載nginx.conf
發(fā)送信號(hào)的方式
kill -QUIT 進(jìn)程號(hào) 安全停止
kil -TERM 進(jìn)程號(hào) 立即停止
nginx模型概念:
Nginx會(huì)按需同時(shí)運(yùn)行多個(gè)進(jìn)程:
一個(gè)主進(jìn)程(master)和幾個(gè)工作進(jìn)程(worker),配置了緩存時(shí)還會(huì)有緩存加載器進(jìn)程(cache loader)和緩存管理器進(jìn)程(cache manager)等实幕。
所有進(jìn)程均是僅含有一個(gè)線程吝镣,并主要通過(guò)“共享內(nèi)存”的機(jī)制實(shí)現(xiàn)進(jìn)程間通信。
主進(jìn)程以root用戶身份運(yùn)行昆庇,而worker末贾、cache loader和cache manager均應(yīng)以非特權(quán)用戶身份(user配置項(xiàng))運(yùn)行。
主進(jìn)程主要完成如下工作:
- 讀取并驗(yàn)正配置信息整吆;
- 創(chuàng)建拱撵、綁定及關(guān)閉套接字辉川;
- 啟動(dòng)、終止及維護(hù)worker進(jìn)程的個(gè)數(shù)拴测;
- 無(wú)須中止服務(wù)而重新配置工作特性乓旗;
- 重新打開(kāi)日志文件;
worker進(jìn)程主要完成的任務(wù)包括:
- 接收昼扛、傳入并處理來(lái)自客戶端的連接寸齐;
- 提供反向代理及過(guò)濾功能;
- nginx任何能完成的其它任務(wù)抄谐;
查看進(jìn)程
ps -ef | grep nginx
配置文件:
nginx.conf配置文件結(jié)構(gòu)
原始配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# 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;
# }
#}
}
配置文件解析
#user nobody; #主模塊命令渺鹦, 指定Nginx的worker進(jìn)程運(yùn)行用戶以及用戶組,默認(rèn)由nobody賬號(hào)運(yùn)行蛹含。
worker_processes 1; #指定Nginx要開(kāi)啟的進(jìn)程數(shù)毅厚。
#用來(lái)定義全局錯(cuò)設(shè)日志文件的路徑和日志名稱。
# 日志輸出級(jí)別有debug浦箱,info吸耿,notice,warn酷窥,error咽安,crit
#可供選擇,其中debug輸出日志最為詳細(xì)蓬推,面crit(嚴(yán)重)輸出日志最少妆棒。默認(rèn)是error
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #用來(lái)指定進(jìn)程id的存儲(chǔ)文件位置。
#event 設(shè)定nginx的工作模式及連接數(shù)上限沸伏, 其中參數(shù)use用來(lái)指定nginx的工作模式(
#這里是epoll糕珊,epoll是多路
#復(fù)用IO(I/O Multiplexing)中的一種方式)
#nginx支持的工作模式有select ,poll,kqueue,epoll,rtsig,/dev/poll。
#其中select和poll都是標(biāo)準(zhǔn)的工作模式毅糟,
#kqueue和epoll是高效的工作模式红选,對(duì)于linux系統(tǒng),epoll是首選
/**
worker_connection是設(shè)置nginx每個(gè)進(jìn)程最大的連接數(shù)姆另,默認(rèn)是1024喇肋,所以nginx最大的連接數(shù)max_client=worker_processes * worker_connections。
進(jìn)程最大連接數(shù)受到系統(tǒng)最大打開(kāi)文件數(shù)的限制蜕青,需要設(shè)置ulimit
*/
events {
worker_connections 1024;
}
/**
http
include mime.types;主模塊命令苟蹈,對(duì)配置文件所包含文件的設(shè)定,減少主配置文件的復(fù)雜度右核,相當(dāng)于把部分設(shè)置放在別的地方慧脱,然后在包含進(jìn)來(lái),保持主配置文件的簡(jiǎn)潔
default_type application/octet-stream; 默認(rèn)文件類(lèi)型贺喝,當(dāng)文件類(lèi)型未定義時(shí)候就使用這類(lèi)設(shè)置的菱鸥。
**/
http {
include mime.types;
default_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"';
#access_log logs/access.log main;
sendfile on; #開(kāi)啟高效文件傳輸模式(zero copy 方式)宗兼,避免內(nèi)核緩沖區(qū)數(shù)據(jù)和用戶緩沖區(qū)數(shù)據(jù)之間的拷貝。
#tcp_nopush on; #開(kāi)啟TCP_NOPUSH套接字(sendfile開(kāi)啟時(shí)有用)
#keepalive_timeout 0; #客戶端連接超時(shí)時(shí)間
keepalive_timeout 65;
# 瀏覽器解壓氮采,nginx 壓縮殷绍,要統(tǒng)一格式
#gzip on;
server {
listen 80; #虛擬主機(jī)的服務(wù)端口
server_name localhost; #用來(lái)指定ip或者域名,多個(gè)域名用逗號(hào)分開(kāi)
#charset koi8-r;
#access_log logs/host.access.log main;
/** #地址匹配設(shè)置鹊漠,支持正則匹配主到,也支持條件匹配,這里是默認(rèn)請(qǐng)求地址躯概,用戶可以location命令對(duì)nginx進(jìn)行動(dòng)態(tài)和靜態(tài)網(wǎng)頁(yè)過(guò)濾處理**/
location / {
root html; #虛擬主機(jī)的網(wǎng)頁(yè)根目錄
index index.html index.htm; #默認(rèn)訪問(wèn)首頁(yè)文件
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# 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;
# }
#}
}
/usr/local/nginx/sbin/nginx -t
#重啟nginx
/usr/local/nginx/sbin/nginx -s reload
Nginx日志描述
通過(guò)訪問(wèn)日志登钥,你可以得到用戶地域來(lái)源、跳轉(zhuǎn)來(lái)源娶靡、使用終端牧牢、某個(gè)URL訪問(wèn)量等相關(guān)信息;通過(guò)錯(cuò)誤日志姿锭,你可以得到系統(tǒng)某個(gè)服務(wù)或server的性能瓶頸等塔鳍。因此,將日志好好利用呻此,你可以得到很多有價(jià)值的信息轮纫。
日志格式
打開(kāi)nginx.conf配置文件:vi /usr/local/nginx/conf/nginx.conf
日志部分內(nèi)容:
access_log logs/access.log main;
日志生成的到Nginx根目錄logs/access.log文件,默認(rèn)使用“main”日志格式焚鲜,也可以自定義格式蜡感。
查看日志命令
tail -f /usr/local/nginx/logs/access.log
日志配置和及切割
/etc/init.d/rsyslog start #系統(tǒng)日志,如不開(kāi)啟恃泪,看不到定時(shí)任務(wù)日志
/etc/rc.d/init.d/crond start #定時(shí)任務(wù)開(kāi)啟
步驟1:編寫(xiě)sh:logcat.sh
#!/bin/bash
#設(shè)置日志文件存放目錄
LOG_HOME="/usr/local/nginx/logs/"
#備分文件名稱
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)"
#重命名日志文件
mv ${LOG_HOME}/access.log ${LOG_HOME}/access.${LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log ${LOG_HOME}/error.${LOG_PATH_BAK}.log
kill -USR1 'cat ${LOG_HOME}/nginx.pid'
#向nginx主進(jìn)程發(fā)信號(hào)重新打開(kāi)日志
步驟2:添加執(zhí)行權(quán)限
chmod +x logout.sh
#查看是否安裝了自動(dòng)任務(wù)crontab
rpm -qa |grep crontab
crontab -e
#重啟定時(shí)任務(wù)
service crond restart
#查看定時(shí)任務(wù)的日志
tail -f /var/log/cron
來(lái)自享學(xué) -peter