一鸥印、安裝
1.下載 Nginx 安裝文件到你的目錄下載地址
2.解壓安裝文件?tar -zxvf ?nginx-1.x.x.tar.gz
3.cd 到?nginx-1.x.x 目錄,開始安裝Nginx
如下命令依次執(zhí)行:
首先:./configure
其次: ?make
然后:make install?
4.啟動(dòng)Nginx
命令: ?/usr/local/nginx/sbin/nginx -c ?/usr/local/nginx/conf/nginx.conf
5.安裝的時(shí)候報(bào)錯(cuò)處理
安裝 gcc& gc++: yum -y install gcc gcc-c++ autoconf automake
?安裝 pcre: ?yum -y install pcre pcre-devel
安裝 zlib: ? yum -y install zlib zlib-devel
二、簡(jiǎn)單配置
1.首先我的服務(wù)器上跑了2個(gè)tomcat
一個(gè)跑在8080端口,一個(gè)跑在8089端口,2個(gè)index.html略不同
2.備份?nginx.conf 文件為新文件?nginx.conf.base? (防止修改出錯(cuò)無(wú)法還原)
命令?: cp/usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.base
3.?修改nginx.conf
?在http節(jié)點(diǎn)下添加upstream節(jié)點(diǎn)
a.配置1:按照請(qǐng)求到達(dá)時(shí)序按權(quán)重進(jìn)行負(fù)載均衡(如下:8080端口的服務(wù)收到請(qǐng)求數(shù)量是8089的兩倍)
?upstream fzjh{
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8089 weight=1;
}
b.配置2,按照IP進(jìn)行負(fù)載均衡(可以解決session共享問題)
upstream fzjh{
ip_hash;
server 127.0.0.1:8089;
server 127.0.0.1:8080;
}
?在server下的location下添加一行:
proxy_pass http://fzjh;?
最后配置文件變成這樣(去掉了部分注釋):
?#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;
sendfile ? ? ? ?on;
#tcp_nopush ? ? on;
#keepalive_timeout ?0;
keepalive_timeout ?65;
#gzip ?on;
upstream fzjh{
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8089 weight=1;
}
server {
listen ? ? ? 80;
server_name ?localhost;
location /{
root ? html;
index ?index.html index.htm;
proxy_pass http://fzjh;
}
error_page ? 500 502 503 504 ?/50x.html;
location = /50x.html {
root ? html;
}
}
}
?4.測(cè)試配置文件并啟動(dòng)或者重啟Nginx
測(cè)試?配置文件:
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
啟動(dòng):/usr/local/nginx/sbin/nginx -c ?/usr/local/nginx/conf/nginx.conf??
重啟:/usr/local/nginx/sbin/nginx -s reload
??4.訪問你的服務(wù)器80端口:http://xxxx.xxxx.xxxx.xxxx/
刷新之后顯示的頁(yè)面不同,說明負(fù)載均衡成功了(我服務(wù)器上的2個(gè)tomcat的index.html略不同)?