您的位置:首页 > 理论基础 > 计算机网络

nginx做SSL并配合tomcat实现HTTPS访问

2017-10-27 18:54 603 查看
1、确认已安装openssl服务,使用 rpm -qa | grep -i openssl 来检查是否安装,如果没有安装以及其相关如openssl-devel,那么就去http://rpm.pbone.net/搜索并下载,通过rpm -ivh xxx.rpm安装,当然如果能yum安装最好,yum安装就是在线安装,rpm理解为离线安装。

2、安装nginx,nginx可以用源码安装,记得./configure --prefix=/usr/local/nginx --with-http_ssl_module。--with-http_ssl_module表示支持SSL。

3、创建证书:

mkdir /etc/nginx/ssl

cd /etc/nginx/ssl

openssl genrsa -des3 -out server.key 1024

openssl req -new -key server.key -out server.csr

最重要的一行是“Common Name”。在这里输入您的官方网域名称,如果您还没有,请输入您网站的IP位址。将挑战密码和可选的公司名称留空。

删除密码:

cp server.key server.key.org

openssl rsa -in server.key.org -out server.key

签署证书

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

4、配置nginx配置文件:

upstream aaa {     
ip_hash;    #这个配置是nginx的一种策略,对于那种没有做session共享的应用使用,就比如浏览器一开始访问那个tomcat,那么该tomcat的sessionid就给浏览器,下次访问时,还会到这个tomcat上,只有当这个tomcat挂了,那么这个浏览器才会要求用户重新登录。
server 127.0.0.1:18080;    
server 127.0.0.1:28080;    

     }  

     server {

       listen       443 ssl;#开启HTTPS访问

       server_name  192.168.101.177;#这个是keepalived生成的虚拟IP
ssl_certificate /etc/nginx/ssl/server.crt;#前面生成的证书
ssl_certificate_key /etc/nginx/ssl/server.key; #前面生成的证书
location /xxx {#工程名,注意如果应用必须带工程名,那么工程名只能写到这,而不能写到127.0.0.1:18080/xxx那边,会报错

            proxy_pass   http://aaa; 这里的aaa就是上面upstream的aaa

#配置 Nginx 的转发选项
   proxy_set_header HOST $host;  #其中Host $host:$server_port; 非80端口 要port。用80端口时 不需要$server_port 

               proxy_set_header X-Real-IP $remote_addr;  

                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

                 proxy_set_header X-Forwarded-Proto $scheme; 

        }

       

    }

    server {

        listen       80;

        server_name  192.168.101.177;
rewrite ^(.*) https://$server_name$1 permanent;强制重定向到https服务

        location /xxx {

proxy_pass   http://aaa;
    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_set_header X-Forwarded-Proto $scheme;  

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        } 

    }

5、配置tomcat的server.xml

在Engine 模块下配置一个 Value:

<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>非80端口时,要再加一个httpsServerPort="xxx"

这种方式实现了外部通过https访问,但nginx和tomcat还是走http协议。
参考http://blog.csdn.net/woshizhangliang999/article/details/51861998 和http://blog.csdn.net/cangzihu/article/details/53488996以及http://blog.csdn.net/vfush/article/details/51086274、http://blog.csdn.net/hanshileiai/article/details/54579948
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: