下載nginx
官網(wǎng):http://nginx.org/
上傳并解壓nginx
tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/src
編譯nginx
進(jìn)入到nginx源碼目錄
cd /usr/local/src/nginx-1.8.1
檢查安裝環(huán)境,并指定將來要安裝的路徑
./configure --prefix=/usr/local/nginx
缺包報(bào)錯(cuò)
./configure: error: C compiler cc is not found
使用YUM安裝缺少的包
yum -y install gcc pcre-devel openssl openssl-devel
編譯安裝
make && make install /usr/local/src/nginx-1.8.1
安裝完后測(cè)試是否正常:
/usr/local/nginx/sbin/nginx --啟動(dòng)進(jìn)程
查看端口是否有ngnix進(jìn)程監(jiān)聽
netstat -ntlp | grep 80
配置nginx
配置反向代理
修改nginx配置文件
server {
listen 80;
server_name nginx-01.itcast.cn; #nginx所在服務(wù)器的主機(jī)名
#反向代理的配置
location / { #攔截所有請(qǐng)求
root html;
proxy_pass [http://192.168.0.21:8080;](http://192.168.0.21:8080;) #這里是代理走向的目標(biāo)服務(wù)器:tomcat
}
}
啟動(dòng)nginx-01上的nginx
./nginx
重啟:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid `
nginx 服務(wù)器重啟命令,關(guān)閉
cd usr/local/nginx/sbin
./nginx 啟動(dòng)
./nginx -t 判斷配置文件是否正確
./nginx -s reload 修改配置后重新加載生效
./nginx -s reopen 重新打開日志文件
nginx -t -c /path/to/nginx.conf 測(cè)試nginx配置文件是否正確
./nginx -s stop 關(guān)閉nginx :
./nginx quit 快速停止
ps -ef | grep nginx 完整有序的停止nginx
其他的停止nginx 方式
kill -QUIT 主進(jìn)程號(hào) 從容停止Nginx
kill -TERM 主進(jìn)程號(hào) 快速停止Nginx
pkill -9 nginx 強(qiáng)制停止Nginx
nginx -c /path/to/nginx.conf 啟動(dòng)nginx
kill -HUP 主進(jìn)程號(hào) 平滑重啟nginx
動(dòng)靜分離
動(dòng)態(tài)資源 index.jsp
location ~ .*\.(jsp|do|action)$ {
proxy_pass http://tomcat-01.itcast.cn:8080;
}
靜態(tài)資源
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
負(fù)載均衡
在http這個(gè)節(jié)下面配置一個(gè)叫upstream的,后面的名字可以隨意取,但是要和location下的proxy_pass http://后的保持一致。
http {
是在http里面的, 已有http, 不是在server里,在server外面
upstream tomcats {
server shizhan02:8080 weight=1;#weight表示多少個(gè)
server shizhan03:8080 weight=1;
server shizhan04:8080 weight=1;
}
#卸載server里
location ~ .*\.(jsp|do|action) {
proxy_pass [http://tomcats;](http://tomcats;) #tomcats是后面的tomcat服務(wù)器組的邏輯組號(hào)
}
}