【簡介】
Nginx是一款輕量級的文本反向代理服務器及電子郵件代理服務器,其特點是內存少,并發高。
【工作原理】
Nginx由內核和模塊組成,完成工作是通過查找配置文件將客戶端請求映射到一個location block(location是用于URL匹配的命令),location配置的命令會啟動不同模塊完成工作。
Nginx模塊分為核心模塊,基礎模塊和第三方模塊。
(1) 核心模塊:HTTP模塊、EVENT模塊(事件)、MAIL模塊。
(2)基礎模塊:HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊、HTTP Rewrite模塊。
(3) 第三方模塊:HTTP Upstream Request Hash模塊、Notice模塊、HTTP Access Key模塊。
【用途】
反向代理,負載均衡,動靜分離
【性能優化】
web服務器,處理靜態文件、索引文件以及自動索引效率高。
(1):代理服務器,快速高效反向代理,提升網站性能。
(2):b負載均衡器,內部支持Rails和PHP,也可支持HTTP代理服務器,對外進行服務。同時支持簡單容錯和利用算法進行負載均衡。
(3):性能方面,Nginx專門為性能設計,實現注重效率。采用Poll模型,可以支持更多的并發連接,并在大并發時占用很低內存。
(4):穩定性方面,采用分階段資源分配技術,使CPU資源占用率低。
(5):高可用性方面,支持熱備,啟動迅速。
【安裝】
準備:nginx.tar.gz
環境:nginx需要依賴gcc,pcre和zlib環境
yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zilb-devel
注意:如果在本機安裝centos虛擬機無網絡,請將網絡模式調節為NET模式,并將System eth0 的IPV4 Settings模式調節為Automatic模式
開始安裝nginx
解壓安裝
tar -xzvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx
make&&make install
啟動
cd /usr/local/nginx/sbin
./nginx
./nginx -s stop
./nginx -s reload
訪問:
本虛擬機訪問:localhost/
win訪問:ip/
【目錄】
【配置】
nginx的配置在nginx的conf目錄下(主要關注nginx.conf文件即可)
這里也模擬一個服務配置:
server {
listen 8888;
server_name hadoop;
location / {
root html;
index lvfang.html;
}
}
確保要有相應的資源,這里確保html下有lvfang.html文件
配置后訪問8888端口的結果
【日志】
nginx的日志存放在logs文件下
access.log 成功日志
error.log 失敗日志
nginx.pid nginx的id
日志配置也是在conf下的nginx.conf文件中
在nginx.conf中的server配置項中有一個access_log配置內容如下:
ccess_log logs/host.access.log main;
表示成功日志的輸出位置為 logs/host.access.log,日志格式為main格式(全局的)
這里可以查看并放開nginx.conf上被注釋的main格式內容
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
兩個server的配置

兩個server的日志輸出
main格式配置:
【日志切分及定時備份】
第一步:如何實現日志切分,編寫shell腳本
第二步:定時任務對腳本進行調度 crontab -e
shell腳本內容
#!/bin/sh
BASE_DIR=/usr/local/nginx
BASE_FILE_NAME=access_80.log
CURRENT_PATH=$BASE_DIR/logs
BAK_PATH=$BASE_DIR/datalogs
CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME
BAK_TIME=`/bin/date -d yesterday +%Y%m%d%H%M`
BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME
echo $BAK_FILE
$BASE_DIR/sbin/nginx -s stop
mv $CURRENT_FILE $BAK_FILE
$BASE_DIR/sbin/nginx
以上shell腳本是每分鐘備份一下日志
定時調度:
crontab -e
*/1 * * * * sh /usr/local/nginx/sbin/log.sh #*/表示一分鐘定時執行一次log.sh腳本
crontab定時執行格式
基本格式 :
* * * * * command
分 時 日 月 周 命令
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要運行的命令
具體的 crontab -e時間格式執行命令查看
http://www.jb51.net/LINUXjishu/19905.html
定時結果:
【反向代理配置】
server {
listen 9999;
server_name localhost;
access_log logs/access_9999.log main;
location / {
root html;
index index.html;
}
location ~ \.jsp$ {
proxy_pass http://192.168.22.128:8080;
}
}
反向代理案例:訪問nginx,轉向tomcat
修改nginx配置:
添加location
location ~ \.jsp$ {
proxy_pass http://192.168.22.128:8080;
}
tomcat/webapps/ROOR下添加測試頁面 test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
</head>
<body>
host:192.168.22.128<br/>
port:8080<br/>
Test1 Page!!!<br/>
remote ip : <%-- <%=request.getHeader("X-real-ip") %> --%> <br/>
nginx server ip : <%=request.getRemoteAddr()%>
</body>
</html>
重啟nginx ./nginx -s reload
啟動tomcat
正常訪問tomcat
192.168.22.128:8080
正常訪問測試頁面192.168.22.128:8080/test.jsp
nginx反向代理訪問192.168.22.128:9999/test.jsp
【負載均衡配置】
注意:這里upstream與server是平級
upstream myapp {
server 192.168.22.128:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.22.131:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8888;
server_name hadoop;
access_log logs/access_8888.log main;
location / {
proxy_set_header X-real-ip $remote_addr;
proxy_pass http://myapp;
root html;
index index.html;
}
}
【具體案例】
兩臺機器:192.168.22.128,192.168.22.131.實現訪問nginx時第一次訪問128,第二次訪問131,相互間隔訪問,負載均衡
準備:兩個tomcat
給tomcat/webapps/ROOT下放測試頁面test.jsp
test.jsp內容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
</head>
<body>
host:192.168.22.131<br/>
port:8080<br/>
Test1 Page!!!<br/>
remote ip : <%-- <%=request.getHeader("X-real-ip") %> --%> <br/>
nginx server ip : <%=request.getRemoteAddr()%>
</body>
</html>
分別開啟兩個tomcat并訪問:
修改nginx配置為以上負載均衡配置
upstream myapp {
server 192.168.22.128:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.22.131:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8888;
server_name hadoop;
access_log logs/access_8888.log main;
location / {
proxy_set_header X-real-ip $remote_addr;
proxy_pass http://myapp;
root html;
index index.html;
}
}
重啟nginx:./nginx -s reload
測試負載均衡:http://192.168.22.128:8888/test.jsp 重復訪問
你會發現訪問的地址完全一樣,但是訪問的tomcat在來回切換