欢迎光临,了解微信小程序开发,就上易用通!

微信小程序配置服务端https协议《二》nginx+tomcat服务器配置

发布:2018-01-25 15:08浏览: 来源:网络 作者:tianshu

基本结构
首先要明白两个基本知识点:
  • 平时我们在地址栏里面输入https://www.domain.com 访问的是80端口,相当于https://www.domain.com:80
  • 而 https://www.domain.com 使用的是443端口
那么问题就来了,如果多个系统都要单独使用一个二级域名的话,直接修改tomcat的配置会导致端口冲突。
下面我的解决办法是在Internet和tomcat之间加一个nginx反向代理。
微信小程序配置服务端https协议《二》nginx+tomcat服务器配置(图1)

基本结构

https请求发送到nginx,nginx将请求代理到tomcat

nginx解决了单ip多域名的问题,多站点就需要tomcat来解决了
网上找到的最好的解决方案是多实例tomcat配置实现单机多站点
什么意思呢?
就是把tomcat拷贝多份,然后修改各个tomcat的server.xml中的shutdown,http以及AJP1.3的端口,然后将tomcat实例启动即可。
如果你们公司好比较重视技术基础设施最好是不要在一台server上部署太多的应用,这个方案对内存要求比较高,因为每个tomcat跑起来之后可能会占200M左右内存,这还是对并发量比较小的,如果实例数一多起来,内存会吃不消。

实操步骤
首先默认你有两个以上指向你的服务器的域名,顶级域名或二级域名都可以。
默认你的服务器上已经安装好了jdk环境。后文中使用的tomcat是8.5版本的。
有两个站点:a.domain.com 和 b.domain.com ,a.domain.com使用https访问,b.domain.com使用http访问
 
  1. 1.安装nginx
  2.  
  3. yum install nginx
  4. 2.下载tomcat,解压到你需要的路径下
  5.  
  6. 假定tomcat解压在/home/admin/app/tomcat 下
  7. 3.配置各独立站点
  8.  
  9. 为A、B站点各新建一个目录,分别为是/home/admin/app/a.domain.com 和 /home/admin/app/b.domain.com
  10. 将/home/admin/app/tomcat下的 conf、logs、temp、webapps、work分别拷贝一份到/home/admin/app/a.domain.com 和 /home/admin/app/b.domain.com下
  11. 建一个目录/home/admin/app/a.domain.com/https_certificate 存放ssl证书
  12. 分别修改两个站点目录下的conf/server.xml文件,修改原则就是:凡是涉及到端口的地方全都修改成唯一的
  13. /home/admin/app/a.domain.com/conf/server.xml
  14.  
  15. ...
  16. <Server port="8105" shutdown="SHUTDOWN">
  17.  
  18. ...
  19. <Connector port="8180" protocol="HTTP/1.1"
  20.                connectionTimeout="20000"
  21.                redirectPort="443"
  22.                proxyPort="443" />
  23.  
  24. ...
  25. <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />
  26. ...
  27. /home/admin/app/b.domain.com/conf/server.xml
  28.  
  29. ...
  30. <Server port="8205" shutdown="SHUTDOWN">
  31.  
  32. ...
  33. <Connector port="8280" protocol="HTTP/1.1"
  34.                connectionTimeout="20000"
  35.                redirectPort="8543" />
  36.  
  37. ...
  38. <Connector port="8209" protocol="AJP/1.3" redirectPort="8643" />
  39. ...
  40. 为各独立站点配置独立的启动脚本,实际上就是把tomcat原来的startup.sh做了一点修改
  41. /home/admin/app/a.domain.com/startup.sh
  42.  
  43. export CATALINA_BASE=/home/admin/app/a.domain.com
  44. export CATALINA_HOME=/home/admin/app/tomcat
  45.  
  46. #!/bin/sh
  47.  
  48. # Licensed to the Apache Software Foundation (ASF) under one or more
  49. # contributor license agreements.  See the NOTICE file distributed with
  50. # this work for additional information regarding copyright ownership.
  51. # The ASF licenses this file to You under the Apache License, Version 2.0
  52. # (the "License"); you may not use this file except in compliance with
  53. # the License.  You may obtain a copy of the License at
  54. #
  55. #     https://www.apache.org/licenses/LICENSE-2.0
  56. #
  57. # Unless required by applicable law or agreed to in writing, software
  58. # distributed under the License is distributed on an "AS IS" BASIS,
  59. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  60. # See the License for the specific language governing permissions and
  61. # limitations under the License.
  62.  
  63. # -----------------------------------------------------------------------------
  64. # Start Script for the CATALINA Server
  65. # -----------------------------------------------------------------------------
  66.  
  67. # Better OS/400 detection: see Bugzilla 31132
  68. os400=false
  69. case "`uname`" in
  70. OS400*) os400=true;;
  71. esac
  72.  
  73. # resolve links - $0 may be a softlink
  74. PRG="$0"
  75.  
  76. while [ -h "$PRG" ] ; do
  77.   ls=`ls -ld "$PRG"`
  78.   link=`expr "$ls" : '.*-> \(.*\)
  79. [/align]`
  80.   if expr "$link" : '/.*' > /dev/null; then
  81.     PRG="$link"
  82.   else
  83.     PRG=`dirname "$PRG"`/"$link"
  84.   fi
  85. done
  86.  
  87. PRGDIR=`dirname "$PRG"`
  88. EXECUTABLE=/home/admin/app/tomcat/bin/catalina.sh
  89.  
  90. # Check that target executable exists
  91. if $os400; then
  92.   # -x will Only work on the os400 if the files are:
  93.   # 1. owned by the user
  94.   # 2. owned by the PRIMARY group of the user
  95.   # this will not work if the user belongs in secondary groups
  96.   eval
  97. else
  98.   if [ ! -x "$EXECUTABLE" ]; then
  99.     echo "Cannot find $PRGDIR/$EXECUTABLE"
  100.     echo "The file is absent or does not have execute permission"
  101.     echo "This file is needed to run this program"
  102.     exit 1
  103.   fi
  104. fi
  105.  
  106. exec "$EXECUTABLE" start "$@"
  107. /home/admin/app/b.domain.com/startup.sh
  108.  
  109. export CATALINA_BASE=/home/admin/app/b.domain.com
  110. export CATALINA_HOME=/home/admin/app/tomcat
  111.  
  112. #!/bin/sh
  113.  
  114. # Licensed to the Apache Software Foundation (ASF) under one or more
  115. # contributor license agreements.  See the NOTICE file distributed with
  116. # this work for additional information regarding copyright ownership.
  117. # The ASF licenses this file to You under the Apache License, Version 2.0
  118. # (the "License"); you may not use this file except in compliance with
  119. # the License.  You may obtain a copy of the License at
  120. #
  121. #     https://www.apache.org/licenses/LICENSE-2.0
  122. #
  123. # Unless required by applicable law or agreed to in writing, software
  124. # distributed under the License is distributed on an "AS IS" BASIS,
  125. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  126. # See the License for the specific language governing permissions and
  127. # limitations under the License.
  128.  
  129. # -----------------------------------------------------------------------------
  130. # Start Script for the CATALINA Server
  131. # -----------------------------------------------------------------------------
  132.  
  133. # Better OS/400 detection: see Bugzilla 31132
  134. os400=false
  135. case "`uname`" in
  136. OS400*) os400=true;;
  137. esac
  138.  
  139. # resolve links - $0 may be a softlink
  140. PRG="$0"
  141.  
  142. while [ -h "$PRG" ] ; do
  143.   ls=`ls -ld "$PRG"`
  144.   link=`expr "$ls" : '.*-> \(.*\)
  145. [/align]`
  146.   if expr "$link" : '/.*' > /dev/null; then
  147.     PRG="$link"
  148.   else
  149.     PRG=`dirname "$PRG"`/"$link"
  150.   fi
  151. done
  152.  
  153. PRGDIR=`dirname "$PRG"`
  154. EXECUTABLE=/home/admin/app/tomcat/bin/catalina.sh
  155.  
  156. # Check that target executable exists
  157. if $os400; then
  158.   # -x will Only work on the os400 if the files are:
  159.   # 1. owned by the user
  160.   # 2. owned by the PRIMARY group of the user
  161.   # this will not work if the user belongs in secondary groups
  162.   eval
  163. else
  164.   if [ ! -x "$EXECUTABLE" ]; then
  165.     echo "Cannot find $PRGDIR/$EXECUTABLE"
  166.     echo "The file is absent or does not have execute permission"
  167.     echo "This file is needed to run this program"
  168.     exit 1
  169.   fi
  170. fi
  171.  
  172. exec "$EXECUTABLE" start "$@"
  173. 4. 修改nginx配置
  174.  
  175. 为两个站点分别准备一份nginx配置文件
  176. su - root
  177. cd /etc/nginx/conf.d
  178. cp default.conf a.domain.com.conf
  179. cp default.conf b.domain.com.conf
  180. 修改配置文件
  181. a.domain.com.conf
  182.  
  183. server {
  184.     listen  443;
  185.     server_name a.domain.com;
  186.  
  187.     ssl on;
  188.     ssl_certificate /home/admin/app/a.domain.com/https_certificate/Nginx/1_a.domain.com_bundle.crt;
  189.     ssl_certificate_key /home/admin/app/a.domain.com/https_certificate/Nginx/2_a.domain.com.key;
  190.     ssl_session_timeout 5m;
  191.     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
  192.     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
  193.     ssl_prefer_server_ciphers on;
  194.  
  195.     location / {
  196.         proxy_set_header       Host $host;
  197.         proxy_set_header  X-Real-IP  $remote_addr;
  198.         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  199.         proxy_set_header X-Forwarded-Proto  $scheme;
  200.  
  201.         # note, there is not SSL here! plain HTTP is used
  202.  
  203.         proxy_pass https://127.0.0.1:8180;
  204.     }
  205. }
  206. b.domain.com.conf
  207.  
  208. server {
  209.     client_max_body_size 2000M;  ##上传文件时body的最大值(如:2G 、200K)
  210.     listen  80;
  211.     server_name b.domain.com;
  212.  
  213.     location / {
  214.          proxy_pass https://127.0.0.1:8280;
  215.     }
  216. }
  217. 测试配置文件测正确性
  218.  
  219. nginx -t -c b.domain.com.conf
  220. nginx -t -c a.domain.com.conf
  221. 重启nginx
  222.  
  223. service nginx restart





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。