zoukankan      html  css  js  c++  java
  • nginx负载均衡

    一、环境

    四台机子

    1、vim /etc/hosts

          192.168.40.132   web2        192.168.40.211   web1     192.168.40.155   web3 (静态)
          192.168.40.129   nginx        192.168.40.200  php(动态)
    2、安装nginx, 防火墙,selinux全关

    3、vim /var/www/html/{index.html,index.php}

    二、192.168.40.129  nginx端 配置

    jspgou时的配置如下

    [root@nginx2 ~]# cat  /etc/nginx/nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    events {
        worker_connections 1024;
    }
    
    http {
        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  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;
    upstream  html {
            server 47.108.85.113:8080;
            server 47.108.160.75:8080;
    }
    
        server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
            
            location / {
            add_header backendIP $upstream_addr;
               add_header backendCode $upstream_status;
            proxy_pass http://html;
               }
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
     }
    }
    

    在容器云下配置

    [root@docker-0330-2 nginx]# cat nginx.conf 
    worker_processes 4;
    worker_rlimit_nofile 40000;
    
    events {
        worker_connections 8192;
    }
    
    stream {
        upstream rancher_servers_http {
            least_conn;
            server 192.168.10.12:80 max_fails=3 fail_timeout=5s;
            server 192.168.10.13:80 max_fails=3 fail_timeout=5s;
            server 192.168.10.14:80 max_fails=3 fail_timeout=5s;
        }
        server {
            listen     80;
            proxy_pass rancher_servers_http;
        }
        upstream rancher_servers_https {
            least_conn;
            server 192.168.10.12:443 max_fails=3 fail_timeout=5s;
            server 192.168.10.13:443 max_fails=3 fail_timeout=5s;
            server 192.168.10.14:443 max_fails=3 fail_timeout=5s;
        }
        server {
            listen     443;
            proxy_pass rancher_servers_https;
        }
    }
    

    [root@localhost ~]# vim /etc/nginx/nginx.conf

    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    include /usr/share/nginx/modules/*.conf;
    events {
        worker_connections 1024;
    }
    http {
        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  /var/log/nginx/access.log  main;

        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;

        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
       

    include /etc/nginx/conf.d/*.conf;
    upstream  html {
            server 192.168.40.132:80;
            server 192.168.40.211:80;
            server 192.168.40.155:80;
    }

    upstream  php {
            server 192.168.40.200:80;
    }
        server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;
            root         /usr/share/nginx/html;

            include /etc/nginx/default.d/*.conf;
            location / {
    proxy_pass http://html;
            }

           location  ~ .php$ {
        proxy_pass http://php;
    }
            error_page 404 /404.html;
                location = /40x.html {
            }

            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
    }
    三、结果测试

    结果如图所示

     

     

    注意:nginx启动不了,考虑端口被占用情况

    [root@haproxy ~]# netstat   -tunlp  |   grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15606/haproxy    

    [root@haproxy ~]# systemctl stop haproxy
    [root@haproxy ~]# netstat -tunlp|grep 80

    四、Nginx+Tomcat动静分离及Nginx优化

    https://blog.51cto.com/lizhenliang/1343497

    [root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
    user  nginx;
    worker_processes  1;
    error_log  logs/error.log;
    pid       logs/nginx.pid;
    events {
        use epoll;
        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;
        keepalive_timeout  65;
        #gzip压缩功能设置
        gzip on;
        gzip_min_length 1k;
        gzip_buffers    4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascripttext/css application/xml;
        gzip_vary on;
        server {
            listen       80;
            server_name www.test.com;
            location / {
        #jsp网站程序根目录,一般nginx与tomcat在同一个目录
                root  /usr/local/tomcat/webapps/ROOT;
                index  index.html index.jsp index.html;
            }
            location ~ .*.jsp$ {
                index index.jsp;
                proxy_pass http://127.0.0.1:8080;   #来自jsp请求交给tomcat处理
                proxy_redirect off;
                proxy_set_header Host $host;    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                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 90;   #nginx跟后端服务器连接超时时间
                proxy_read_timeout 90;      #连接成功后,后端服务器响应时间
                proxy_buffer_size 4k;       #设置代理服务器(nginx)保存用户头信息的缓冲区大小
                proxy_buffers 6 32k;        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
                proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
                proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
            }
            location ~ .*.(gif|jpg|png|bmp|swf)$   #由nginx处理静态页面
            {
                expires 30d;   #使用expires缓存模块,缓存到客户端30天
            }
            location ~ .*.(jsp|js|css)?$
            {
                expires 1d;
            }
                error_page  404         /404.html;   #错误页面
                error_page  500 502 503 504  /50x.html;
                location = /50x.html {
                root html;
            }
        }
    }
    
  • 相关阅读:
    android ART hook
    Bind Enum to ListControl
    注意WPF中绑定使用的是引用类型
    Android开发第2篇
    Android开发第1篇
    Extension method for type
    DB2实用命令记录
    TDD三大定律
    【InstallShield】 为什么卸载后有的文件没有删掉
    GAC write failed when upgrade with InstallShield
  • 原文地址:https://www.cnblogs.com/zjz20/p/11544881.html
Copyright © 2011-2022 走看看