简单的nginx负载均衡配置

来源:互联网 发布:sql 入门到精通 编辑:程序博客网 时间:2024/06/05 04:04

nginx很强大,同时兼容了反向代理和负载均衡。下面是我做的一个简单的测试例子:

一、安装nginx

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-develuseradd -g wuyue -M -s /sbin/nologin co_nginxcp -rfv /root/install/nginx-1.4.1.tar.gz /tmp/chmod 555 /tmp/nginx-1.4.1.tar.gztar -xzvf /tmp/nginx-1.4.1.tar.gzrm -rf /tmp/nginx-1.4.1.tar.gzcd /tmp/nginx-1.4.1/./configure --prefix=/usr/local/nginx --user=co_nginx --group=wuyuemake && make installrm -rf /tmp/nginx-1.4.1/vi /usr/local/nginx/conf/nginx.conf#user  co_nginx;/usr/local/nginx/sbin/nginx -t # 测试启动/usr/local/nginx/sbin/nginxps -ef | grep nginx
按照以上步骤就可以正常安装nginx,没有什么多说的。

二、简单的负载均衡配置

首先我有开发环境两台,192.168.1.8    192.168.1 .9 ,nginx装在了192.168.1 .9上面。在1.8 和1.9上同时装了tomcat,然后一个静态页面index.html,里边就一句描述:

hello world 192.168.1.8/9 。

我把niginx安装在了/usr/local/nginx 目录下,我们先进到/usr/local/nginx/conf/,打开nginx.conf。指令如下:

cd /usr/local/nginx/conf/vi nginx.conf
我们会看到nginx.conf的默认配置

#user co_nginx;#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;    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        #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;    #    server_name  localhost;    #    ssl                  on;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_timeout  5m;    #    ssl_protocols  SSLv2 SSLv3 TLSv1;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers   on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    include /usr/local/nginx/myconf/kangban.conf;}
http以上的内容我们几乎不会动,大家如果想知道都是什么意思可以谷歌查一下。我们只说http里边的内容。你会看到他默认的配置文件给了三个server例子。我第一次配置,为了尽量降低影响,就没有动三个server例子的任何东西,大家会看到倒数第二行多了这么一句
include /usr/local/nginx/myconf/kangban.conf;
表示我引入了另一个conf的配置文件,对了,我所有的配置都放在了这个引入的配置文件里。

 upstream  test.kangban.cn {              server   192.168.1.9:8080;              server   192.168.1.8:8080;      }    server {        listen       8077;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass http://test.kangban.cn;            #proxy_set_header   Host             $host;            #proxy_set_header   X-Real-IP        $remote_addr;            #proxy_set_header   REMOTE-HOST        $remote_addr;        }        #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;        #}    }
跟默认的配置文件nginx.conf所不同的就是,我没有http,因为我引入kangbn.conf的路径是在server同一级目录的,所以不需要http以上的东西。这里多了一个upstream的配置,这个理论上就是用来做分发的,比如我这里配置了192.168.1.8:8080 和192.168.1.9:8080,也就是我会把请求test.kangban.cn的所有请求都分发到这两个服务上去。这里又牵涉到了nginx的分发机制,我先拉下来,具体的大家自己看:

1)、轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。2)、weight :指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。2)、ip_hash :每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。3)、fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。 4)、url_hash(第三方) 
好了。然后我们看server里边的配置,首先看listen ,这个表示监听端口,后边随便写。server_name表示的域名,比如本地你就可以直接写localhost,同时你也可以写自己的www.test.com 这个没任何问题。那下面的location是什么了?他表示你监听的端口要转到哪里。nginx本身也是web容器,所以可以直接指到自己的项目下,这里我们不用nginx来发布项目,我们把转向地址指向tomcat项目。所以这里用到了代理proxy_pass,后面跟的地址表示的转向地址,比如这里我是test.kangban.cn。然后后面还有一个error的配置比较重要,他表示的就是nginx如果出现异常应该转向的地址。我这里没有配置,然后下面的一些东西其实都可以删掉了,留着也碍眼。

配置文件发送了修改,我们需要重启一下nginx,因为第一次不知道指令试着敲了下./nginx -h,然后还真出现了

nginx version: nginx/1.4.2Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]Options:  -?,-h         : this help  -v            : show version and exit  -V            : show version and configure options then exit  -t            : test configuration and exit  -q            : suppress non-error messages during configuration testing  -s signal     : send signal to a master process: stop, quit, reopen, reload  -p prefix     : set prefix path (default: /usr/local/nginx/)  -c filename   : set configuration file (default: conf/nginx.conf)  -g directives : set global directives out of configuration file
解释的很清楚了,我选择了./nginx -s stop,这是停止nginx。也都没有问题,然后我选择./nginx -s reopen ,出现异常了

nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
这句话表示你找不到nginx.pid这个文件,然后我就去自己创建了一个,异常发生了变化,问题没有解决。所以不得不返回来问谷歌,找到答案了,像这个异常一般都是因为启动nginx的时候没有指定默认配置文件路径,它又没找到导致的。然后我们只需要如此启动就可以了:

./nginx -c /usr/local/nginx/conf/nginx.conf 
你看一下,问题解决了,OK了。

这个之后你打开浏览器,地址http://192.168.1.9:8077 ,因为分发机制默认是轮询法,所以你刷一次,就换一个ip现实,先后如下:





原创粉丝点击