Nginx配置负载均衡

简介: 一,Nginx常用命令           1) 启动Nginx:start nginx2) 停止Nginx:nginx -s stop3) 修改配置后重启:nginx -s reload二,以Tomcat服务器为例进行负载均衡设置    为了简单,我直接在windows解压了个nginx-1.10.0.zip。


一,Nginx常用命令

          

1)         启动Nginx:start nginx

2)         停止Nginx:nginx -s stop

3)         修改配置后重启:nginx -s reload



二,以Tomcat服务器为例进行负载均衡设置


    为了简单,我直接在windows解压了个nginx-1.10.0.zip。


    之后再myeclipse里面部署了同一个项目,为了方便,第一个项目我设置的起始页面为:


    

/*测试nginx*/
	@RequestMapping("/home")
	public String home() {
		//http://localhost:8091/itoo-basic-curriculumschedule/curriculumschedule/home
		return "home1";
	}

       第二个应用程序的起始页面设置的为:


/*测试nginx*/
	@RequestMapping("/home")
	public String home() {
		//http://localhost:8091/itoo-basic-curriculumschedule/curriculumschedule/home
		return "home2";
	}


服务器端口号及应用程序实例:





之后,打开nginx-1.10.0\conf\nginx.conf 这个文件,配置负载均衡的服务器列表及网站路径:


#使用用户和组,windows下不指定
#user  nobody;

#指定工作衍生进程数(一般等于CPU总和数或总和数的两倍,例如两个四核CPU,则总和数目等于8)
worker_processes  1;

#指定错误日志文件存放路径,错误日志级别可选项为【debug/info/notice/warn/error/crit】
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定pid存放路径
#pid        logs/nginx.pid;

#event段,定义全局属性
	#工作模式及连接数上限
events {
	#使用网络I/O默写,Linux系统推荐使用epoll,FreeBSD系统推荐使用kequeue;window下不指定
	#user epoll;
	#允许的连接数
    worker_connections  1024;  #设置单个进程最大连接数

}

#设定http服务器,利用他的反向代理功能提供负载均衡支持(还可以反向代理mail)
http {
	#设定mine类型
    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"';

    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;

    #设定access log
    access_log  logs/access.log  main;

    
    client_header_timeout 3m;

    client_body_timeout 3m;

    send_timeout 3m;

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay on;

    #keepalive_timeout  0;

    keepalive_timeout  65;


    #开启gzip模块
    	gzip  on;

   	 	gzip_min_length 1100;

    	gzip_buffers 4 8k;

    	gzip_types text/plain application/x-javascript text/css application/xml;

    	output_buffers 1 32k; 

   	 	postpone_output 1460;

    	server_names_hash_bucket_size 128; 

    	client_max_body_size 8m;

    	fastcgi_connect_timeout 300; 

    	fastcgi_send_timeout 300; 

    	fastcgi_read_timeout 300; 

    	fastcgi_buffer_size 64k; 

    	fastcgi_buffers 4 64k; 

    	fastcgi_busy_buffers_size 128k; 

    	fastcgi_temp_file_write_size 128k; 

    	gzip_http_version 1.1; 

    	gzip_comp_level 2; 

		gzip_vary on;

    #定义负载均衡的服务器列表
    upstream Mysite{
    	#ip_hash;
		#同一机器在多网情况下,路由切换,ip可能不同 #weigth参数表示权值,权值越高被分配到的几率越大;

    	server localhost:8091;
    	server localhost:8080;

    }

    #设定虚拟主机
    server {
        listen       80;
        server_name  localhost;
        charset UTF-8;


        
        #设置本虚拟主机的访问日志
        #access_log  logs/host.access.log  main;

        #定义一个URI的特性
        #location中可以嵌套location
        location / {
        	#‘/’表示对“/”启用负载均衡
        	 	#定义服务器的默认网站根目录位置
            	#root  F:\JavaServer\apache-tomcat-7.0.69-windows-x64\apache-tomcat-7.0.69\webapps\itoo-basic-curriculumschedule;

            	#index  index.jsp;  #定义首页索引文件的名称

            	proxy_pass http://Mysite/itoo-basic-curriculumschedule/curriculumschedule/home;

            #保留用户真实信息
            	proxy_set_header Host $host; 

            	proxy_set_header X-Real-IP $remote_addr; 

            	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

			#允许客户端请求的最大单个文件字节数
				client_max_body_size 10m; 
			#缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户
            	client_body_buffer_size 128k;
			#跟后端服务器连接超时时间 发起握手等候响应超时时间
            	proxy_connect_timeout 12;
			#连接成功后 等待后端服务器响应时间 其实已进入后端的排队之中等候处理
            	proxy_read_timeout 90;
			#代理请求缓存区 这个缓存区间会保存用户的头信息一共Nginx进行规则处理 一般只要能保存下头信息即可
            	proxy_send_timeout 90;
			#同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
            	proxy_buffer_size 4k;
            	proxy_buffers 4 32k;
			#如果系统很忙的时候可以申请国内各大的proxy_buffers 官方推荐 *2
            	proxy_busy_buffers_size 64k;
			#proxy 缓存临时文件的大小
            	proxy_temp_file_write_size 64k; 
            	proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; 
            	proxy_max_temp_file_size 128m;
            	

        }

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

}


先在浏览器里面刷一下,然后我们再说具体配置:







多刷新几次,会发现出来的页面不一样。



三,负载均衡配置详解


首先使用upstream配置服务器列表:




注意,这里的名字为Mysite,这个名字之后会使用到。


之后,设置我们的访问地址及访问路径:


       使用location来定义匹配规则:




感觉Mysite起到了一个占位符的作用。


如果有问题,可以去查看log:



  try it.





相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
42 0
|
1月前
|
弹性计算 负载均衡 容灾
slb配置后端服务器组
配置阿里云SLB后端服务器组涉及四个主要步骤:创建服务器组、添加ECS实例、关联监听规则和设定负载均衡策略。这使得流量根据业务需求和服务器特性进行转发,便于应用架构的灵活管理和扩展,支持蓝绿部署、灰度发布,并通过多可用区提升系统可用性和容灾能力。
26 3
|
15天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
31 0
|
22天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
91 0
|
3天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
12 0
|
5天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
6天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
6天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
20 2
|
16天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
24 0