一、安装
1.下载 Nginx 安装文件到你的目录下载地址
2.解压安装文件 tar -zxvf nginx-1.x.x.tar.gz
3.cd 到nginx-1.x.x 目录,开始安装Nginx
如下命令依次执行:
首先:./configure
其次: make
然后:make install
4.启动Nginx
命令: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5.安装的时候报错处理
安装 gcc& gc++: yum -y install gcc gcc-c++ autoconf automake
安装 pcre: yum -y install pcre pcre-devel
安装 zlib: yum -y install zlib zlib-devel
二、简单配置
1.首先我的服务器上跑了2个tomcat
一个跑在8080端口,一个跑在8089端口,2个index.html略不同
2.备份 nginx.conf 文件为新文件 nginx.conf.base (防止修改出错无法还原)
命令: cp/usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.base
3.修改nginx.conf
在http节点下添加upstream节点
a.配置1:按照请求到达时序按权重进行负载均衡(如下:8080端口的服务收到请求数量是8089的两倍)
upstream fzjh{
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8089 weight=1;
}
b.配置2,按照IP进行负载均衡(可以解决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.测试配置文件并启动或者重启Nginx
测试配置文件:
/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
启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重启:/usr/local/nginx/sbin/nginx -s reload
4.访问你的服务器80端口:http://xxxx.xxxx.xxxx.xxxx/
刷新之后显示的页面不同,说明负载均衡成功了(我服务器上的2个tomcat的index.html略不同)
网友评论