Linux搭建nginx负载均衡服务器(手动编译)

  1. nginx下载

官网:http://nginx.org/en/download.html

  1. 上传到服务器

可以选择rz命令上传,也可以选择SFTP上传都可以。

  1. 安装相关依赖
[root@dbnewyouth ~]# yum install gcc-c++
[root@dbnewyouth ~]# yum install -y pcre pcre-devel
[root@dbnewyouth ~]# yum install -y zlib zlib-devel
[root@dbnewyouth ~]# yum install -y openssl openssl-devel
  1. 解压nginx安装包
[root@dbnewyouth ~]# tar -zxvf nginx-1.15.1.tar.gz
  1. 进行配置
首先要进入解压后的nginx目录,然后复制下面一段即可
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
  1. 编译并安装
[root@dbnewyouth ~]# make && make install
  1. 启动nginx
进入sbin目录
[root@dbnewyouth sbin]# ./nginx
关闭nginx:
[root@dbnewyouth sbin]# ./nginx -s stop
推荐使用:
[root@dbnewyouth sbin]# ./nginx -s quit
  1. 配置虚拟主机

配置文件位置:/usr/local/nginx/conf/nginx.conf 通过端口划分

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-81;
            index  index.html index.htm;
        }
    }
}

通过域名划分

#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;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-81;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.taobao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-taobao;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.baidu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-baidu;
            index  index.html index.htm;
        }
    }
}

  1. 更改配置文件后,重新加载配置文件生效
[root@dbnewyouth sbin]# nginx -s reload
  1. 配置反向代理服务器 第一步:安装两个Tomcat,端口号分别为8080,8090(端口号自己随意) 第二步:分别启动两个Tomcat 第三步:反向代理服务器的配置
upstream tomcat1 {
	server 192.168.25.148:8080;
    }
    server {
        listen       80;
        server_name  www.sina.com.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
    upstream tomcat2 {
	server 192.168.25.148:8090;
    }
    server {
        listen       80;
        server_name  www.sohu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

第四步:重新加载配置文件(如10) 第五步:在host文件中加入ip和对应域名,测试可以,正式环境请路过这里

  1. 配置负载均衡 分配权重即可
upstream tomcat2 {
	server 192.168.25.148:8081 weifht=3;
	server 192.168.25.148:8082 weight=2;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×