Nginx负载均衡搭建(Window与Linux)

时间:2023-03-10 01:09:30
Nginx负载均衡搭建(Window与Linux)

windows上搭建nginx负载均衡

1.准备几台http服务器软件,这里选用一台apache一台tomcat

apache(windows)下载链接:https://www.apachehaus.com/cgi-bin/download.plx

tomcat(windows)下载链接: http://tomcat.apache.org/

2.启动apache 设置端口为8002,启动tomcat设置端口为8001

Nginx负载均衡搭建(Window与Linux)

Nginx负载均衡搭建(Window与Linux)

3.分别访问localhost:8001   localhost:8002,看能否有猫和ok

Nginx负载均衡搭建(Window与Linux)

Nginx负载均衡搭建(Window与Linux)

4.成功之后,到官网下载nginx(http://nginx.org/en/download.html) stable version

5.解压到文件夹之后,修改conf/nginx.conf配置文件(只给出核心部分)

upstream myproject{
server 127.0.0.1:8001 weight=3;
server 127.0.0.1:8002;
}
server{
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://myproject;
}
}

  

6.到 解压目录的bin目录下,检查配置是否正确 nginx -t,如果正确直接运行,然后地址栏localhost看是否会出现2个不同页面

Linux(Ubuntu14)搭建Nginx负载均衡

1.还是采用apache和tomcat2台服务器,搭建apache时,可选用 apt-get install apache2,也可以自行下载源码包进行编译安装

tomcat的话可以自行下载linux版本的然后解压。在linux上分别以8081,8082两个端口运行服务器。

2.安装Nginx,第一种方式,apt方式安装 apt-get install nginx  默认安装在/etc 目录下

第二种方式,源码安装:

1.依赖c,则安装c++运行环境

apt-get install build-essential
apt-get install libtool

 centos平台编译环境使用如下指令

  安装make:

yum -y install gcc automake autoconf libtool make
安装g++
yum install gcc gcc-c++

2.安装依赖pcre库
    2.1 sudo apt-get install libpcre3 libpcre3-dev
2.2
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
    tar -zxvf pcre-8.39.tar.gz
    cd pcre-8.39
    ./configure --prefix=path
    make
    make install

3.安装zlib库

3.1 sudo apt-get install zlib1g-dev
3.2
    wget http://www.zlib.net/zlib-1.2.3.tar.gz
    tar -zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure --prefix=path
make
make install

  4.安装ssl依赖

  

4.1  apt-get install build-essential
4.2
wget http://zlib.net/zlib-1.2.5.tar.gz
tar -zxvf zlib-1.2.5.tar.gz
cd zlib-1.2.5/
./configure --prefix=path
   make
   make install

  

5.安装nginx

wget http://nginx.org/download/nginx-1.1.10.tar.gz
tar -zxvf nginx-1.1.10.tar.gz
cd nginx-1.1.10
./configure --prefix=path
make
make install

  

到安装目录下  vim conf/nginx.conf

修改核心配置

    upstream myproject{
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081;
}
server {
listen 8090;
server_name 127.0.0.1;
location / {
proxy_pass http://myproject;
}

进入 安装目录下的sbin目录,cd ../sbin

检查配置是否正确 ./nginx -t

开启nginx :    ./nginx

关闭nginx方式:

  方式一,进入sbin 运行:./nginx -s stop

  方式二,ps -ef | grep nginx 查看对应PID(也可以在logs 目录下nginx.pid文件找到)

      kill   pid 杀死进程

这样在Ubuntu上已经搭建好了nginx负载均衡的环境了,访问localhost:8090就会看到有时8080端口对应的服务器页面频率更高一下

安装nginx官方说明: http://nginx.org/en/docs/configure.html

中文网:http://www.nginx.cn/