Nginx负载均衡

2023-02-13 19:01:58 浏览数 (1)

前言

由于我springboot项目启动了两个docker应用实例 8080和8081端口,我就想做个nginx负载均衡

直接上配置文件nginx.conf(http配置)

代码语言:javascript复制
#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;
 
    limit_req_zone $binary_remote_addr zone=one:15m rate=3r/s;
    include ip.conf;#封ip的文件
 
 
 
    #tomcat_server是负载均衡器的名字
    upstream tomcat_server {
	server localhost:8080 weight=1;
	server localhost:8081 weight=1;
}
                     	              
 
 
 
    #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;
    
    limit_req_zone $binary_remote_addr zone=allips:10m rate=1r/s;
 
    
    limit_req_zone $uri zone=api_read:20m rate=50r/s;
 
 
 
    server {
        listen       80;
        server_name  localhost;
        limit_req zone=one burst=10 nodelay;
        include block.conf; #防护规则
        #¶ԁ
        limit_req zone=api_read burst=100;
 
        charset utf-8;
 
        #优化
        keepalive_timeout 60;
        tcp_nodelay on;
        client_header_buffer_size 4k;
        open_file_cache max=102400 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 1;
        client_header_timeout 15;
        client_body_timeout 15;
        reset_timedout_connection on;
        send_timeout 15;
        server_tokens off;
        client_max_body_size 10m;
 
 
 
 
 
        #access_log  logs/host.access.log  main;
 
 
 
                #̹前端文件nginx处理
                location ~.(gif|jpg|jpeg|png|ico|bmp|swf|html|css|js|mp3|mp4)$ {
 
 
 
                    #̹防盗链
                    valid_referers 47.106.67.99;
 
                    if ($invalid_referer) {
                       return 403;
                    }
 
 
                    expires 7d;#»º´丌
 
 
                    #开启压缩
                        gzip  on;
 
                        #压缩
                        gzip_types text/plain application/javascript   application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
 
                      
                        gzip_disable "MSIE [1-6].";
 
                        #压缩
                        gzip_min_length 1k;
 
                       
                        gzip_buffers 4 16k;
 
                       
                        gzip_comp_level 2;
                    proxy_cache_valid 404 1m; #¶Դ


	

0 人点赞