nginx配置负载均衡

来源:互联网 发布:中国云计算大会 2017 编辑:程序博客网 时间:2024/05/18 01:54
修改nginx配置文件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;

   #配置tomcat集群 并添加集群名称  这里集群名称就用的是ip,当然也可以自定义别的名称
    upstream 120.24.33.3333{    
server 120.24.33.3333:8080 weight=2;  //配置服务器权重,访问时nginx会根据你配的权重比访问相应的tomcat服务器
server 120.24.33.3333:8088 weight=1;   //比如这里是2:1,访问时前两次请求访问会在8080服务器上,第三次会转到8088服务器,然后再转到8080,以此类推
    }


    server {
        listen       80;                //设置对外访问端口
        server_name  120.24.33.3333;   //设置对外访问ip或者域名


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            proxy_pass http://120.24.33.3333;   注意这里http://  后面填ip还是别的什么不重要,但是要和上面配置tomcat集群upstream **{ } 这个地方上面填入的名称相同
        }


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

#文件访问权限  这里要注意配置文件格式,要不然网页加载不出来你的文件,图片什么的不可以显示
location ~* \.(gif|jpg|jpeg|png|bmp|html|htm|flv|swf|ico)$ {
           proxy_pass http://120.24.33.3333;
           add_header Last-Modified $date_gmt;
           add_header Via $server_addr;
           expires 30d;
        }

#js /css文件访问权限   必须配置,不配置js和css文件会被nginx拦截
        location ~ .*\.(js|css)?$
        {
         proxy_pass http://120.24.33.3333;
         add_header Last-Modified $date_gmt;
         add_header Via $server_addr;
         expires      1h;
         }

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


}