首页 > 代码库 > Nginx负载均衡实现https访问

Nginx负载均衡实现https访问

整体流程:1.搭建tomcat项目集群(默认完成) 2.安装nginx需要的库 3.安装Nginx并修改配置文件 4.启动测试

1.1.1. 安装Nginx

1.1.1.1. 安装环境:

安装pcre库

yum -y install pcre-devel

安装zlib库

yum install -y zlib-devel

安装openssl库

yum install -y openssl openssl-devel   或者  编译安装

编译安装openssl:

1.上传openssl压缩包

 

按alt+p进入上传界面,上传openssl-1.0.1t.tar.gz

2.解压压缩包

         [root@itcast-01 ~]# tar –zxvf openssl-1.0.1t.tar.gz

         [root@itcast-01 ~]#cd openssl-1.0.1t

3.编译安装

         设置安装参数

[root@itcast-01 openssl-1.0.1t]# ./config

         编译并安装

            [root@itcast-01 nginx-1.7.7]#make

    [root@itcast-01 nginx-1.7.7]#make install

准备安装Nginx:

上传Nginx

按alt+p进入上传界面,上传Nginx

1.1.1.2. 解压

解压

[root@itcast-01 ~]# tar -zxvf nginx-1.10.2.tar.gz

进入解压文件夹

[root@itcast-01 ~]# cd nginx-1.10.2

1.1.1.3. 编译安装

设置安装参数

[root@itcast-01 nginx-1.10.2]#./configure --prefix=/usr/local/nginx --with- http_ssl_module

编译并安装

[root@itcast-01 nginx-1.10.2]# make

[root@itcast-01 nginx-1.10.2]# make install

openssl生成测试CA证书:详见参考资料

1.1.1.4. 修改nginx.conf文件,实现负载均衡:

需求:1.用户通过https访问,通过nginx反向代理实现http内部跳转2.实现页面压缩gzip 3.记录用户真实ip地址 4.使用ip-hash方式创建集群信息,解决session粘滞问题 5.访问地址为DNS注册域名 6.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"‘;

    #配置集群信息

    upstream xxx {

      ip_hash;

      server xxx.xx.xxx.xx:8083;

      server xxx.xx.xxx.xx:8085;

    }

 

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

        server_name  www.xxx.com;

 

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

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

 

        ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5000m;

 

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers  on;

          location ~ \.png$

 {

            root /home;

 

  }

 

        location / {

            #root html;

            #index  index.html index.htm;

           #配置用户真实ip

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           #使用之前配置的集群

            proxy_pass http://xxx;

        }

    }

 

}

注意:1.location块root的路径问题:例如:location块配置如下

         location ~ \.png$

 {

            root /home;

 

  }

项目图片访问路径为/images/a.png,那么匹配正则后路径变为/home/images/a.png

以上配置文件location正则uri仅为示例。

1.1.1.5. 启动Nginx

查看安装文件,conf是配置文件,sbin是启动目录

[root@itcast-01 nginx-1.10.2]# cd /usr/local/nginx/

 

 

进入启动文件目录,启动Nginx

[root@itcast-01 nginx]# cd sbin/

[root@itcast-01 sbin]# ./nginx

查看启动进程

 

 

关闭防火墙

[root@itcast-01 sbin]# service iptables stop

访问测试

 

1.1.1.6. Nginx相关扩充:

访问流程:在不添加‘可选匹配规则’模块时,Nginx服务器首先在server块的多个location块中搜索是否有标准uri和请求字符串匹配,如果有多个可以匹配,就记录匹配度最高的一个。然后,服务器再用location块中的正则uri和请求字符串匹配,当地一个正则uri匹配成功,就不再进行搜索,并使用这个location块处理此请求,如果正则匹配全部失败,就使用刚才记录的匹配度最高的location块处理此请求。

命令:

                     ./nginx

                     ./nginx -s stop

                     ./nginx -s quit

                     ./nginx -s reload

                     ./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。

                     ./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

                    

                     查询nginx进程:

 

                     ps aux|grep nginx

                    

       8.2.6 重启服务:

             1.先停止再启动(推荐):

                     对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

 

                     ./nginx -s quit

                     ./nginx

                     2.重新加载配置文件:

                     当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效,如下:

                     ./nginx -s reload

       8.2.7 访问: http://xx.xx.xx.xxx/ 是否成功加载nginx欢迎页面,如果看到Welcome to nginx!字样则证明安装成功

       8.2.8 查看nginx 安装路径 whereis nginx

Nginx负载均衡实现https访问