Nginx + Tomcat实现负载均衡

特此声明:文章参考自https://www.imooc.com/article/34866https://blog.csdn.net/xlgen157387/article/details/49781487

1. 工具

  • nginx/Windows-1.14.0
  • Apache Tomcat 8.5.33

2. 目标

实现高性能负载均衡的Tomcat集群:

3. 步骤

1. 首先下载Nginx和Tomcat

Nginx下载地址:http://nginx.org/en/download.html

Tomcat下载地址:http://tomcat.apache.org/

2. 然后解压三个Tomcat,分别命名为apache-tomcat-8.5.33-server1、apache-tomcat-8.5.33-server2和apache-tomcat-8.5.33-server3,入下图所示:

3. 然后修改这三个Tomcat的端口,以其中一台为例,打开Tomcat的conf目录下的server.xml,共修改三 处,如下所示:

<Server port="8005",shutdown="SHUTDOWN"><Server>

<Connector port-"8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>

4. 然后启动这三个Tomcat,并访问,看是否正常

5.然后修改上面三个Tomcat的默认页面,为这三台Tomcat区分一下,如下图所示:

修改后,进行访问,如下如图所示:

6.以上完成后,就可以配置Nginx来实现负载均衡,只需要将Nginx的配置文件nginx.conf给配置好就可以,如下所示(只进行简单的配置,实际生产环境可以进行更为详细的完善配置):

# Nginx全局参数
#user  nobody;   1.  # 运行Nginx的用户的用户组
worker_processes  1; # Nginx启动你那个多个线程,最好跟计算机的CPU核数一致


# 全局的错误日志类型
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 进程号的保存文件
#pid        logs/nginx.pid;

# Nginx等待事件参数
events {  # 等待事件
    worker_connections  1024; # 每个进程的最大连接数(总连接数 = worker_connections * worker_processes)
}

# Nginx的HTTP参数 
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;   # 高效文件传输模式,指定是否调用sendfile函数来传输文件,一般的应用都建议启用
    #tcp_nopush     on;

    #keepalive_timeout  0; # 长连接超时时间,单位秒
    keepalive_timeout  65;

    #gzip  on; # 是否启用压缩传输
	
	# 服务器的集群
	upstream netitcast.com {
		server 127.0.0.1:18080 weight=1; #服务器配置,weight是权重的意思,权重越大,分配的概率越大
		server 127.0.0.1:18081 weight=2;
		server 127.0.0.1:18082 weight=3;
        # 上面三行是集群的服务器列表,IIS,最终请求会被转发到这里执行
	}

	# 当前的Nginx的配置
    server {
        listen       80; # 端口
        server_name  localhost; # 主机名
        # 如果请求为localhost:80,则交给名称为netitcast.com的Nginx集群来处理
 
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
            #root   html;
            #index  index.html index.htm;
        #}
		
		location / {
			proxy_pass http://netitcast.com; # 要与上面的Nginx的名称保持一致
			proxy_redirect default;
		}

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

}

到此配置完成,下面演示负载均衡。

7. 首先启动Nginx

8. 然后输入127.0.0.1或localhost

 

 

 

 

 

 

基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值