nginx反向代理、负载均衡 + session共享

前几天我因为工作的需要,自己学习了下找了下各种文档,自己给自己挖了一堆坑,然后又自己慢慢的填!下面我就先介绍下我自己的安装方式。


一:准备环境:

1.服务器:centos6.6

2.nginx版本nginx-1.8.0                                             nginx 下载地址

3.下载pcre-8.02.tar.gzwget http://exim.mirror.fr/pcre/pcre-8.02.tar.gz

cd pcre-8.02

./configure && make  && make install

如果发现没安装gcc 等 则安装gcc等依赖

yum install gcc gcc-c++ -y

然后再执行 ./configure && make  && make install

4下载zlib-1.2.8.tar.gz wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz 

cd zlib-1.2.8

./configure && make && make install

5.下载openssl   wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz

tar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c

yum install perl

./config

make

make install

6.解压nginx tar -zxvf nginx-1.8.0.tar.gz 

 将nginx目录名修改为nginxmv nginx-1.8.0 nginx

7.最重要的一步,我在这卡里半天,找不到解决方案,很多博客没有备注明白,这里是开启nginx权限的(也就是开启nginx的一些别的功能的步骤)

--sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx/nginx.pid

--with-http_ssl_module --with-pcre=/usr/local/nginx/pcre-8.02 --with-zlib=/usr/local/nginx/zlib-1.2.8 --with-openssl=/usr/local/nginx/openssl-1.0.1c

--with-http_stub_status_module

7.编译安装

make

make install

二:nginx配置文件

nginx的负载均衡和反向代理主要就是通过配置文件来完成,那么下面我们就来说说nginx的配置文件。

废话先不说,先上完整的配置文件:

#运行用户
#user  nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志及PID文件
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  1024;  #单个后台worker process进程的最大并发链接数
    # multi_accept on; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
     #设定mime类型,类型由mime.type文件定义
    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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    #开启gzip压缩
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    #设定请求缓冲
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    #设定负载均衡的服务器列表
      upstream mysvr {
     # weigth参数表示权值,权值越高被分配到的几率越大
          #本机上的Squid开启3128端口
#      server 192.168.122.105:8088;
      server 192.168.39.207:80;
      server 192.168.39.208:80;

#      ip_hash;
       }

    server {
    listen  80;
    server_name 127.0.0.1;

    rewrite ^(.*)$  https://$host$1 permanent;
    }

    server {
        #侦听443端口
        listen       443 ;

        server_name  127.0.0.1;

        ssl on;

        ssl_certificate /usr/local/nginx/nginx/conf/server.crt;

        ssl_certificate_key /usr/local/nginx/nginx/conf/server.key;

        ssl_session_timeout 5m;

        ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;


        #charset koi8-r;
        #设定本虚拟主机的访问日志
#        access_log  logs/host.access.log;

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

        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
             proxy_pass http://mysvr;
      #       index  index.html index.htm; 
                      }
      #

        #默认请求
        location / {
                 proxy_pass http://mysvr;
                 proxy_set_header   Host    $host;
                 proxy_set_header   X-Real-IP   $remote_addr;
                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          #  root   html; #定义服务器的默认网站根目录位置
          #  index  index.html index.htm; #定义首页索引文件的名称
        }

#       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {

#        proxy_pass http://mysvr; 
#        } 

#       location /Tomcat {
#                 proxy_pass http://192.168.122.105:8080/;
#                 proxy_set_header   Host    $host;
#                 proxy_set_header   X-Real-IP   $remote_addr;
#                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          #  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
        #
        #设定查看Nginx状态的地址
        location /nginx_status {
        stub_status on;
        # I do not need logs for stats
        access_log /usr/local/nginx/logs/status.log;
        auth_basic "NginxStatus";
        #deny all;
        }

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

       # ssl on;

       # ssl_certificate /usr/local/nginx/nginx/conf/server.crt;

        #ssl_certificate_key /usr/local/nginx/nginx/conf/server.key;

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

}
好了 ,那么下面我就说说主要功能的配置了:(大神勿喷)

由于我用的是实现的是https来做的请求加密那么我还监听80端口,然后把80端口转向443(HTTPS默认端口443)

具体的写法就是:

server {
    listen  80;
    server_name 127.0.0.1;

    rewrite ^(.*)$  https://$host$1 permanent;
    }

    server {
        #侦听443端口
        listen       443 ;

        server_name  127.0.0.1;

        ssl on;

        ssl_certificate /usr/local/nginx/nginx/conf/server.crt;

        ssl_certificate_key /usr/local/nginx/nginx/conf/server.key;

        ssl_session_timeout 5m;

        ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

              ······
上面的例子就是把原来的HTTP80端口的请求重定向到https443端口,强制实现LLS加密传输。

下面我们来说说主要的负载均衡的配置(其实很简单,但是我当初两个后台主机一台部署tomcat,一台部署apache ,结果发现有一个CSS、JS、图片一致获取不到,后来才发现服务不一样,而存放静态文件的路径也不对,静态文件再另一台主机上未必有啊。 ),见笑了大家。

 #设定负载均衡的服务器列表
      upstream mysvr {
     # weigth参数表示权值,权值越高被分配到的几率越大
          #本机上的Squid开启3128端口
#      server 192.168.122.105:8088;
      server 192.168.39.207:80;
      server 192.168.39.208:80;

#      ip_hash; 
       }
上面的代码我配置的是轮询访问,就是这一次访问的是207服务,那么下次请求来了访问的是208的服务。当然你也可以通过配置权值来让哪个服务访问的几率增大或减少。

具体还有很多的配置方式,大家可以百度或者google下,网上的例子很多的,我就不废话了。还有一个就是

<pre name="code" class="html">ip_hash;

 

它的作用就是能够将当前某个ip的请求定向到同一台后端,这样一来这个ip和这个后端服务器就建立了稳定的session。

我们要负载均衡还缺少一步,那么就是配置什么样的请求去找那几个后台服务器。

 #charset koi8-r;
        #设定本虚拟主机的访问日志
#        access_log  logs/host.access.log;

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

        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
             proxy_pass http://mysvr;
      #       index  index.html index.htm; 
                      }
      #

        #默认请求
        location / {
                 proxy_pass http://mysvr;
                 proxy_set_header   Host    $host;
                 proxy_set_header   X-Real-IP   $remote_addr;
                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          #  root   html; #定义服务器的默认网站根目录位置
          #  index  index.html index.htm; #定义首页索引文件的名称
        }

上面的例子很明显,我配置了2个有效请求,一个是xxx.xxx.xxx.xxx:80/index.html 那么另外一个就是默认请求,我们来主要看看默认请求的例子:

 location /
表示xxx.xxx.xxx.xxx 或者 https://xxx.xxx.xxx.xxx:443这样的请求,后面没有任何路径,那么我想xxx.xxx.xxx.xxx:80/test 去访问刚才的负载均衡服务器,那么我就直接写成:

 location /test {
这样你的xxx.xxx.xxx.xxx:80/test 请求对应的就是你里面配置的后台服务器请求路径。
 proxy_pass http://mysvr;
这个是主要的,大家应该也发现了,它的http 后面跟着的是我们上面配置的负载均衡的一样。那么如果你上面配置了3个不同的负载均衡服务集群。那么你在这里写3个不同的请求方式,然后proxy_pass 对应不同的负载均衡集群就完成了。至于里面的另外几个参数请大家去百度吧。嘿嘿(偷个懒!~~~~~)

再来说说反向代理:

其实你懂了负载均衡那么反向代理也就不难理解了只是吧原来本该指向负载集群的路径指向了具体的IP+端口不就完了?最后再附上一个反向代理的例子:

 location /Tomcat {
#                 proxy_pass http://192.168.122.105:8080/;
#                 proxy_set_header   Host    $host;
#                 proxy_set_header   X-Real-IP   $remote_addr;
#                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          #  root   html; #定义服务器的默认网站根目录位置
          #            #  index  index.html index.htm; #定义首页索引文件的名称
#         }
好了,大家可以参考我的配置去自己搭建下,其实没那么难,刚开始我听着也是感觉很高大上的样子。自己搭建一次后感觉也就是那。后面我应该会写一个关于多个负载均衡服务器共同访问而出现的一个session无法统一这样的问题,那么下一个我会用redis 来做session共享服务器,包括相关的java代码。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值