最近公司出于安全考虑,需要将登录页做成https访问,其他页面仍采用http访问,环境是Linux平台,web服务器采用Tomcat + Nginx。之前没接触过nginx,这两天网上查资料,试了好多,终于有点小成果,特此做一下记录。目前还存在一些问题,希望各位多多指教。下面说一下我的具体做法:

1.将nginx解压到C盘根目录,重命名文件夹为Nginx(版本:1.3.5)。

2.生成自签名证书(采用OpenSSL生成),生成工具下载:绿色版OpenSSL工具.rar自签名测试证书工具.rar

3.将生成的证书文件server.cer和server.key分别都拷贝到Tomcat和Nginx的conf目录下,双击生成的root.cer根证书,然后安装证书,将其安装到受信任的根证书颁发机构(如不安装,访问时浏览器会提示证书错误)。

4.配置Tomcat的server.xml文件和web工程的web.xml文件:

https配置:

1 <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
2         SSLEnabled="true" maxThreads="150"
3         scheme="https" secure="true" disableUploadTimeout="true"
4         enableLookups="false" acceptCount="100" clientAuth="false"
5         SSLCertificateFile="C:/Program Files/Tomcat 6.0/conf/server.cer"
6         SSLCertificateKeyFile="C:/Program Files/Tomcat 6.0/conf/server.key"
7         SSLVerifyClient="none" sslProtocol="TLS" />

虚拟目录配置:

1 <Host name="localhost"  appBase="C:\nginx\html"
2             unpackWARs="true" autoDeploy="true"
3             xmlValidation="false" xmlNamespaceAware="false">

web.xml文件中加入如下配置:

01      <!-- 登录页采用https访问 -->
02 <security-constraint>
03     <web-resource-collection>
04         <web-resource-name>SSL</web-resource-name>
05         <url-pattern>/index/*</url-pattern>
06     </web-resource-collection>
07     <user-data-constraint>
08         <transport-guarantee>CONFIDENTIAL</transport-guarantee>
09     </user-data-constraint>
10 </security-constraint>

5.配置Nginx的nginx.conf文件:

01     server {
02         listen       80;
03         server_name  localhost;#域名,可以多个
04  
05         #charset koi8-r;
06  
07         #access_log  logs/host.access.log  main;
08          
09        #配置规则
10          location / {
11               if (!-f $request_filename){
12                 rewrite ^/pages/common/(.*)$ /error.jsp;
13             }
14             root   yddweb;
15             #index  index.jsp;
16               proxy_pass http://localhost:8080;
17               proxy_set_header Host $host:80;
18             proxy_set_header X-Real-IP $remote_addr;
19             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
20             proxy_set_header Via "nginx";
21        }
22  
23          location ^~ /pages/$ {
24             root   yddweb;
25             #index  index.jsp;
26               proxy_pass http://localhost:8080;
27               proxy_set_header Host $host:80;
28             proxy_set_header X-Real-IP $remote_addr;
29             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
30             proxy_set_header Via "nginx";
31         }
32  
33         #location ~ \.(gif|jpg|png|js|css)$ {
34             #规则
35         #}
36     }
37  
38     # HTTPS server
39     #
40     server {
41         listen       443;
42         server_name  localhost:443;
43  
44         ssl                  on;
45         ssl_certificate      server.cer;#
46         ssl_certificate_key  server.key;
47  
48         ssl_session_timeout  5m;
49  
50         #ssl_protocols  SSLv2 SSLv3 TLSv1;
51         #ssl_ciphers  HIGH:!aNULL:!MD5;
52         #ssl_prefer_server_ciphers   on;
53  
54         #配置规则
55         location ^~ /index/.jsp$ {
56             root   yddweb;
57             index  login.jsp;
58               proxy_pass https://localhost:8443;
59               proxy_set_header Host $host:443;
60             proxy_set_header X-Real-IP $remote_addr;
61             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
62             proxy_set_header Via "nginx";
63         }
64         location ~ ^/(WEB-INF)/ {
65             deny all;
66         }
67     }
68 }

6.web工程的截图:

LoginServlet的代码:

1 HttpSession session = request.getSession(true);
2         String name = request.getParameter("name").trim();
3         session.setAttribute("curuser", name);
4         String url = "http://"+request.getServerName()+request.getContextPath()+"/pages/system/myinfo.jsp";     response.sendRedirect(url);

目前存在的问题(希望各位多多指教):

1.在本机访问https正常,其他机器访问浏览器提示证书错误。

2.location规则的配置,由于本人水平有限,对location规则的配置不是很了解,所以location目前不太会配置(配置要求:index目录下的页面采用https访问,其他页面全部采用http访问)。

参考资料:

证书生成与配置:http://www.ert7.com/install/sslinstall/1244.html

Windows下Tomcat+nginx配置证书实现登录页https访问的更多相关文章

  1. windows下tomcat+nginx+openssl配置双向认证

    1. 基础知识 CA证书:https://blog.csdn.net/yangyuge1987/article/details/79209473 SSL双向认证原理:https://blog.csdn ...

  2. windows下用nginx配置https服务器

    1.安装nginx 先到nginx官网下在nginx http://nginx.org/en/download.html 将下载好的文件解压出来修改文件名为 nginx ,然后拷贝到C盘下,目录如下: ...

  3. Windows下用Nginx配置遇到的问题

    Nginx是一款轻量级的web服务器/反向代理服务器,更详细的释义自己百度了.目前国内像新浪.网易等都在使用它.先说下我的服务器软件环境: 系统:Windows Server + IIS + ngin ...

  4. windows下使用nginx配置tomcat集群

    转自:https://blog.csdn.net/csdn15698845876/article/details/80658599

  5. 烂泥:Windows下安装与配置Nginx web服务器

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 前几篇文章,我们使用nginx都是在linux环境下,今天由于工作的需要.需要在windows环境也使用nginx搭建web服务器. 下面记录下有关ng ...

  6. windows下基于IIS配置ssl证书遇到的坑

    前几天配置windows下基于IIS配置ssl证书 完全按照步骤执行 绑定https网址后,一直显示:无法访问此网站 检查了443端口,还有防火墙限制,没发现什么 足足困扰了我好几天 后来突然想到前不 ...

  7. 环境变量(windows下tomcat问题);shh连接虚拟机网络配置

    环境变量(windows下tomcat问题) 有tomcat有jdk 再配置环境变量:参考 提示:若选择“用户变量”,则本次配置的变量只对该用户有效          若选择“系统变量”,则对所有用户 ...

  8. windows下安装和配置nginx

    下载nginx 到官网下载window版的nginx http://nginx.org/ 配置环境变量 解压到本地的某个路径下, 打开cmd窗口,cd到nginx的目录下 这里要注意cd的时候要加/d ...

  9. Windows下tomcat进程监控批处理程序

    在Windows下tomcat进程监控批处理程序脚本如下: @echo off ::tomcat安装目录 set _tomcatDir=E:\myFiles\apache-tomcat-8.5.31 ...

随机推荐

  1. docker介绍与安装

    .docker是什么 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的cgroup,namespace,Union FS等技术,对应用进程进行封装隔离,并且独立于宿主机与其 ...

  2. mac 安装 maven 配置

    前面的话: 记录 在 Mac 下 安装配置 maven 1. 下载 Maven, 并解压到某个目录.例如/Users/robbie/apache-maven-3.3.3 2. 打开 Terminal, ...

  3. MVP MVVM MVC

    上一篇得到大家的关注,非常感谢.由于自己对于这些模式的理解也是有限,对于MVC,MVP,MVVM这些模式的比较,是结合自己的理解,一些地方不一定准确,需要的朋友可以参考下 上一篇得到大家的关注,非常感 ...

  4. [论文]Coordination of Cluster Ensembles via Exact Methods

    作者:Ioannis T. Christou, Member, IEEE IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE, ...

  5. cmd窗口情况下:windows下cmd默认的编码是GBK

    想在windows下查看sqlite的utf-8中文需要先 执行chcp 65001把当前页换为utf-8编码 chcp 命令: chcp 65001  就是换成UTF-8代码页,在命令行标题栏上点击 ...

  6. Educational Codeforces Round 37 A B C D E F

    A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...

  7. android的布局-----GridLayout(网格布局)

    学习导图 (一)简介 网格布局由GridLayout所代表,在android4.0之后新增加的布局管理器,因此需要android4.0之后的版本中使用,如果在更早的平台使用该布局管理器,则需要导入相应 ...

  8. vim的语法高亮及配置文件说明

    本文主要针对那些刚刚入门的菜鸟,老手请自动忽略,谢谢. 一.安装vim: sudo pacman -S vim 随后根据提示输入超级用户密码即可完成安装 二.配置自己的语法高亮文件,主要是修改-/.v ...

  9. 剖析CPU温度监控技术【转】

    转自:http://blog.csdn.net/hunanchenxingyu/article/details/46476545 迄今为止还没有一种cpu散热系统能保证永不失效.失去了散热系统保护伞的 ...

  10. 【原创】SSO-Javascript模拟IE登录,不让IIS弹出登录窗口

    解决方案: 用JS模拟IE用户登录,再跳转到对应的系统. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...