nginx负载均衡+tomcat集群,配置ssl证书

Nginx上启用HTTPS,而Nginx 和 Tomcat 之间走普通的 HTTP 连接。
nginx负载均衡+tomcat集群,配置ssl证书_第1张图片

(一) Nginx

  1. 配置ssl环境
    (1) [root@linux ~]# cd /usr/local/src/nginx-1.1.15
    进入nginx安装目录
    (2) [root@linux ~]# ./configure --with-http_ssl_module
    当执行上面语句,报错:./configure: error: SSL modules require the OpenSSL library,则执行:
    [root@linux ~]# yum -y install openssl openssl-devel
    [root@linux ~]#./configure
    [root@linux ~]#./configure --with-http_ssl_module
    (3) [root@linux ~]# make
    切记不能make install 会覆盖
    (4) [root@linux ~]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
    把原来nginx备份
    (5) [root@linux ~]# cp objs/nginx /usr/local/nginx/sbin/nginx
    新的nginx覆盖旧的
    若出现错误cp: cannot create regular file
    ‘/usr/local/nginx/sbin/nginx’: Text file busy
    使用命令:cp -rfp objs/nginx /usr/local/nginx/sbin/nginx解决
    (6) [root@linux ~]# /usr/local/nginx/sbin/nginx -t
    测试nginx是否正确
    正确时,提示:
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  2. /usr/local/nginx/conf目录下添加2个文件:
    abc.com.key
    abc.com_bundle.crt

  3. 修改nginx.conf,整体替换如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
    proxy_headers_hash_max_size 51200;
    proxy_headers_hash_bucket_size 6400;
	  #服务器的集群  
    upstream  abc.com{ 
        server    192.168.58.141:18080 max_fails=3 fail_timeout=10s weight=1;  
        server    192.168.58.141:28080 max_fails=3 fail_timeout=10s weight=1 backup;  	
    }
	  client_header_buffer_size 128k;
	  large_client_header_buffers 4 128k;
    
    server {
        listen       443 ssl;
        server_name  192.168.58.141;

        ssl                  on;
        ssl_certificate      abc.com_bundle.crt;
        ssl_certificate_key  abc.com.key;

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

        location / {
            proxy_set_header X-Forwarded-Proto https;
            proxy_pass http://abc.com;  
            proxy_redirect off;
	          client_max_body_size 300M;
	          proxy_buffering    off;
	          proxy_buffer_size  128k;
	          proxy_buffers 100  128k;	  
        }
        error_page   500 502 503 504 404  /index.html;#找不到页面跳转到index.html
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen     80;
        server_name  192.168.58.141;
        rewrite ^(.*)$  https://$host$1 permanent;
    }
}

(二) Tomcat

  1. 修改tomcat目录下的conf/server.xml


变成





上面的value是tomcat自带的,下面的是我们要添加的
         

(三) 效果
浏览器访问
1.http://abc.com,自动跳转到https://abc.com,页面正常访问。
2.http://192.168.58.141:18080/project, 自动跳转到https://192.168.58.141:18080/project, 但是访问的时候存在如下2个问题:
2.1.由于页面静态资源为http访问,所以显示的很丑。
工程web.xml里添加如下代码能够解决:

  
	
		SSL
		/*
	
	
		CONFIDENTIAL
	

2.2.现在域名为https://192.168.58.141:18080/project,action请求头地址为https://abc.com,所以在向后台发送请求的时候报跨域问题错误。

参考:https://feitianbenyue.iteye.com/blog/2056357
https://blog.csdn.net/qq_33182756/article/details/80780632
https://www.cnblogs.com/wbq1113/p/9357332.html

你可能感兴趣的:(https)