部落法师a怪升级路线:nginx 负载均衡配置

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 23:05:42
Nginx 简介
   Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。 Igor 将源代码以类 BSD 许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

使用 Nginx 前必须了解的事项

  1. 目前官方 Nginx 并不支持 Windows,您只能在包括 Linux、UNIX、BSD 系统下安装和使用;
  2. Nginx 本身只是一个 HTTP 和反向代理服务器,它无法像 Apache 一样通过安装各种模块来支持不同的页面脚本,例如 PHP、CGI 等;
  3. Nginx 支持简单的负载均衡和容错;
  4. 支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有
在 Linux 下安装 Nginx
     为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE ./configure
 make
 make install
安装完成之后 安装 nginx
./configure --with-http_stub_status_module –prefix=/usr/local/nginx
make
make install
就这样nginx 安装完成 启动nginx 命令如下 ./nginx & 如果通过http://localhost 能够访问的话 表示安装成功 /如图:

安装成功后 /usr/local/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 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 {
    use epoll;#linux
    worker_connections  2048;
}
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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
     upstream mao{
           server 192.168.0.208:8080 weight=2;
           server 192.168.0.235 weight=3;
     }
    server {
        listen       80;
        server_name  www.today.com;

        charset utf-8;

        #access_log  logs/host.access.log  main;
  location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
  access_log   off; # po co mi logi obrazków :)
  expires      30d;
}
     location ~ ^/(WEB-INF)/ {
        deny all;
     }
    location /NginxStatus/ {
        stub_status on; #Nginx 状态监控配置
        access_log off;
        
     }
    location /they{
        proxy_pass http://mao;# 反向代理
        #include proxy.conf;
        }

    # location / {
    #proxy_pass  http://192.168.0.208:8080;
    #proxy_set_header  X-Real-IP  $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;
        #}
    }
    # 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  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
      ssl_prefer_server_ciphers   on;

      location / {
          root   html;
          index  index.html index.htm;
      }
    #}

}

动态页面请求处理:
Nginx 本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,例如 Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx 直接处理的一些静态文件请求后,其他所有的请求通过 proxy_pass 指令传送给后端的服务器(在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:
    location / {
         proxy_pass http://localhost:8080;
         proxy_set_header X-Real-IP $remote_addr;
 }
在本例中我们配置的是2台机器的负载均衡 所以我们的配置是 :
location /they{
        proxy_pass http://mao;# 反向代理
        #include proxy.conf;
        }
其中 mao 是我们用upstream 来定义的 例如:
upstream mao{
           server 192.168.0.208:8080 weight=2;
           server 192.168.0.235 weight=3;
 

     }
在 Nginx 的集群配置中,Nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时,或者重新起效时,Nginx 都会非常及时的处理状态的变化,以保证不会影响到用户的访问。