简单的nginx负载均衡配置

阅读: 评论:0

简单的nginx负载均衡配置

简单的nginx负载均衡配置

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

一、安装nginx

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
useradd -g wuyue -M -s /sbin/nologin co_nginx
cp -rfv /root/install/nginx-1.4. /tmp/
chmod 555 /tmp/nginx-1.4.
tar -xzvf /tmp/nginx-1.4.
rm -rf /tmp/nginx-1.4.
cd /tmp/nginx-1.4.1/
./configure --prefix=/usr/local/nginx --user=co_nginx --group=wuyue
make && make install
rm -rf /tmp/nginx-1.4.1/
vi /usr/local/nginx/f
#user  co_nginx;/usr/local/nginx/sbin/nginx -t # 测试启动
/usr/local/nginx/sbin/nginx
ps -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/,打开f。指令如下:

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

#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       pes;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   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/f;
}
http以上的内容我们几乎不会动,大家如果想知道都是什么意思可以谷歌查一下。我们只说http里边的内容。你会看到他默认的配置文件给了三个server例子。我第一次配置,为了尽量降低影响,就没有动三个server例子的任何东西,大家会看到倒数第二行多了这么一句
include /usr/local/nginx/f;
表示我引入了另一个conf的配置文件,对了,我所有的配置都放在了这个引入的配置文件里。

 upstream  test.kangban {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 ;#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   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;#}}
跟默认的配置文件f所不同的就是,我没有http,因为我引入f的路径是在server同一级目录的,所以不需要http以上的东西。这里多了一个upstream的配置,这个理论上就是用来做分发的,比如我这里配置了192.168.1.8:8080 和192.168.1.9:8080,也就是我会把请求test.kangban的所有请求都分发到这两个服务上去。这里又牵涉到了nginx的分发机制,我先拉下来,具体的大家自己看:

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

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

nginx version: nginx/1.4.2
Usage: 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: f)-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/f 
你看一下,问题解决了,OK了。

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





本文发布于:2024-02-05 06:40:04,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170726484263956.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:负载均衡   简单   nginx
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23