Nginx学习——反向代理与负载均衡(一)

一:环境配置

我本地是Windows系统,使用 Oracle VM VirtualBox 虚拟机安装一个虚拟的Linux系统,安装jdk1.8,nginx,两个tomcat

这里写图片描述
一般需要用到三台服务器,一台 nginx 服务器,两台正式部署项目的服务器。为了方便操作,我只在一台服务器上面安装了一个nginx和两个tomcat(端口不一样)

二:配置tomcat集群

首先在服务器上安装两个 tomcat:这个也是简单,不多说

安装 tomcat:上传解压即可使用,bin 目录下 startup.sh 启动,shutdown.sh 关闭

配置防火墙端口:vim /etc/sysconfig/iptables 编辑,开放 8080 端口,8081 端口,80 端口等一些常用端口,当然后边有用到一些端口都是需要配置开放的,不建议关闭防火墙

这里写图片描述

编辑好后 service iptables restart 重新加载防火墙配置
这里写图片描述

如果是自己测试嫌配置麻烦,关闭防火墙: service iptables stop 重启后防火墙打开,即在此次开机状态下有效,完全关闭再使用 chkconfig iptables off , 即会在重启后也关闭防火墙,注意有时候服务都起了但访问出错,可能就是防火墙问题哦

启动 tomcat 访问:192.168.43.3:8080,192.168.43.3:8081,打开 tomcat 首页即成功。

然后编写测试项目,部署到两台 tomcat 上,eclipse 新建 web 项目,项目名为 testproject,在 webapp 下新建一个 jsp 页面为 index.jsp, 添加如下内容
这里写图片描述

maven导出为 war 包,spring-demo.war,将该 war 包上传到服务器的两台 tomcat 的 webapps 中
这里写图片描述
这里写图片描述

此时,重新启动 tomcat,访问 192.168.43.3:8080,192.168.43.3:8081, 显示 index.jsp 内容:两台服务器访问显示如下
这里写图片描述
这里写图片描述

至此,两台 tomcat 服务器搭建完成。

三:Nginx的安装配置与测试

先使用 yum 命令安装 gcc,安装 pcre,zlib,openssl:

yum install -y gcc   
yum install -y pcre pcre-devel   
yum install -y zlib zlib-devel   
yum install -y openssl openssl-develplain

在 /root/wyj/tools 目录下新建 nginx 目录,将 nginx-1.17.7.tar.gz 放到此处,解压

tar -zxvf nginx-1.17.7.tar.gz

进入解压后目录
这里写图片描述
依次执行命令:

./configure   

make   

mkae install

此时 nginx 安装完毕,安装目录是 /usr/local/nginx,nginx 默认占用 80 端口
这里写图片描述
其中,sbin 目录为 nginx 执行命令,conf 目录下的 nginx.conf 为默认加载的配置文件

启动 nginx:

./nginx

关闭 nginx:

./nginx -s stop

重启

./nginx  -s reload

启动 nginx 后访问 192.168.50.133:80 即可访问 nginx:显示 nginx 欢迎页
这里写图片描述

至此,nginx 安装完毕。

四:反向代理与负载均衡配置

现有一台服务器,为 192.168.43.3,服务器上有一台 tomcat,端口为 8080 和 8081,经过配置 nginx,当访问 192.168.43.3:80 时,即可访问 192.168.43.3:8080,192.168.43.3:8081 中随机一台,此时 192.168.43.3:80 被 nginx 监听,当有请求时,代理到 192.168.43.3:8080,192.168.43.3:8081 随机一台即可,即为 nginx 反向代理功能,同时此时可以通过 nginx 将请求进行转发,保证了一个入口,将所有请求转发到两台服务器上也减轻了任何一台的负载压力,当有大量请求时,可以搭建大量服务器,在入口代理服务器上使用 nginx 进行转发,即是负载均衡功能。

配置即是配置 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;

    #配置被代理的服务器
    upstream blank {
        #ip_hash;
        server 192.168.43.3:8080 weight=2;
        server 192.168.43.3:8081 weight=1;
    }


    server {
        #nginx监听80端口,请求该端口时转发到真实目标
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #这里配置代理是指上面定义的两个被代理目标,blank名字必须一致
          proxy_pass http://blank;

            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

启动两台 tomcat,重新启动 nginx:

访问 192.168.43.3:80 将会随机访问 192.168.43.3:8080 和 192.168.43.3:8081 其中一台。(问题:每次刷新 nginx 服务器地址 sessionid 会变,session 不能共享。)

这里写图片描述
这里写图片描述

尝试不断刷新,两个页面会交替显示,即成功。
但发现SessionID并不相同,下一篇博文来通过Redis来实现Session的共享(同步)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值