5

Linux+Nginx/Apache/Tomcat新增SSL证书,开启https访问教程

 3 years ago
source link: https://zhang.ge/4861.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

上上篇文章《nginx平滑升级&新增模块》提到了公司的https访问需求。当我新增了SSL模块之后,却发现以前还真没部署过https访问。

下面整理我的部署过程,并收集了一下Apache和Tomcat这2种Linux下常用的WEB软件配置SSL的简单简单步骤,以便回头翻阅。

一、下载证书

成功申请SSL证书之后,就可以下载到配置SSL的证书了!一般情况下,都可以选择下载相应WEB服务器的不同证书,或者直接打包下载主流WEB服务器的证书,如图所示:

下载后,就可以根据不同的WEB服务器来选择相应的证书了。

二、Nginx

先确认nginx安装时已编译http_ssl模块,也就是执行如下命令查看是否存在--with-http_ssl_module参数:

linux-test:~ # /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.6.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.1/

如果没有这个参数,说明没有编译SSL模块,那么请参考上上篇文章自行解决,此处就不赘述了。

①、准备证书

Nginx需要用到2个证书文件:

I.  证书公钥 (crt格式)

II. 证书私钥(key格式)

拿到证书后,将其上传到nginx下的ssl目录(也可自定义位置)。

②、修改配置

A. http和https全局共存

在原server模块新增监听443端口,然后新增如下代码(具体看注释)。

server {
listen 80;
#新增监听443端口,并指定443为ssl:
listen 443 ssl;
server_name yourdomain.com;
#新增ssl配置---开始:
ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #证书公钥文件路径
ssl_certificate_key /usr/local/nginx/ssl/yourdomain.key; #证书私钥文件路径
ssl_session_timeout 5m; #5分钟session会话保持
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
#新增ssl配置---结束:
location / {
#其他规则保持不变

保存配置之后,先执行如下命令测试配置是否正确:

linux-test:~ # /usr/local/nginx/sbin/nginx -t
#如下显示则为正确无误:
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

确认无误之后,执行如下命令重载nginx,让配置生效:

linux-test:~ # /usr/local/nginx/sbin/nginx -s reload

如无错误,现在应该可以顺利访问https://yourdomain.com/了!值得说明的是,这样配置后,http和https是全局共存的,你能http访问到的页面,https也可以访问得到。

B. 全局强制https

如果是全局https访问,那么额外写一个监听80的server,让http访问跳转到https上即可,下面是参考模板:

server{
listen 80;
server_name yourdomain.com;
root /path/for/yourdomain.com;
location / {
rewrite (.*) https://yourdomain.com$1 permanent;
server {
listen 443;
server_name yourdomain.com;
ssl on;
ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #证书公钥文件路径
ssl_certificate_key /usr/local/nginx/ssl/yourdomain.key; #证书私钥文件路径
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
location / {
#其他规则维持不变

C. 部分强制https,部分强制http

可能有部分强迫症会有这样的需求:我只要部分页面强制https访问,比如后台及登陆页面,其他常规页面强制http访问,我该如何设置?

思路:和B方案一样,分别2个server模块,并新增判断规则,指定部分页面http访问,部分页面https访问。

具体可以参考一下张戈博客的配置主要修改中文注释部分,其他配置保持不变):

#监听httpserver
server
listen 80;
server_name zhang.ge m.zhang.ge;
index index.html index.htm index.php default.html default.htm default.php;
root /home/web/zhang.ge;
include zhangge.conf;
location ~ /uploads/.*\.(php|php5)?$ {
deny all;
#若是匹配到wp-login.php登陆,则跳到https
location ~ /(wp-login\.php(.*)$) {
rewrite ^(.*)$ https://zhang.ge$1 permanent;
break;
#wordpress后台强制跳到https
location /wp-admin {
rewrite ^(.*)$ https://zhang.ge$1 permanent;
location ~ [^/]\.php(/|$)
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
expires 30d;
location ~ .*\.(js|css)?$
expires 30d;
access_log /home/logs/zhang.ge.log access;
#监听https
server
listen 443;
server_name zhang.ge m.zhang.ge;
ssl on;
ssl_certificate /usr/local/nginx/ssl/zhang.ge.crt;
ssl_certificate_key /usr/local/nginx/ssl/zhang.ge.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
index index.html index.htm index.php default.html default.htm default.php;
root /home/web/zhang.ge;
#有偿服务付款页面使用https访问
location /wp-content/plugins/alipay {
try_files $uri $uri/ /index.php?$args;
#若没有匹配到wp-admin或wp-includes,则跳到http访问(反向逻辑:即只允许指定页面开启https)
location / {
if ($request_uri !~* "wp-admin|wp-includes") {
rewrite (.*) https://zhang.ge$1 permanent;
location ~ /uploads/.*\.(php|php5)?$ {
deny all;
location ~ [^/]\.php(/|$)
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
expires 30d;
location ~ .*\.(js|css)?$
expires 30d;
access_log /home/wwwlogs/zhang.ge.log access;

二、Apache

同样,先确认Apache安装时已添加SSL支持模块。如果没有请自行搜索搞定,本文不再赘述。

①、准备证书

Apache需要用到三个证书文件:

I. 根证书:root_bundle.crt

II. 证书公钥:yourdomain.com.crt

III. 证书私钥:yourdomain.com.key

将下载好的三个证书文件,上传到apache下的ssl目录中(可自定义位置)。

②、修改配置

I. 编辑httpd.conf文件,取消以下内容的#注释符号:

#LoadModule ssl_module modules/mod_ssl.so
#Include conf/extra/httpd-ssl.conf

II. 编辑http-ssl.conf文件,如下修改:

#找到如下行,并替换为证书公钥的实际路径:
SSLCertificateFile /usr/local/apache/ssl/public.cer
#找到如下行,并替换为证书私钥的实际路径:
SSLCertificateKeyFile /usr/local/apache/ssl/private.key
#找到如下行,取消行首注释符,并替换为根证书实际路径:
#SSLCertificateChainFile /usr/local/apache/ssl/ca.cer

III. 保存退出,并重启Apache即可。

三、Tomcat

①、准备证书

Tomcat只需要用到一个jks格式的证书文件,比如yourdomain.com.jks。

拿到文件后,将其上传到Tomcat下的conf目录中。

②、修改配置

打开conf目录下的server.xml文件,找到以下内容:

<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

去掉前后的注释,并如下修改(或者直接其后添加以下代码亦可):

<Connector
port="443"
protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
maxThreads="150"
scheme="https"
secure="true"
keystoreFile="conf\yourdomain.jks" <!-- 此处填写你上传的证书的实际路径 -->
keystorePass="password"
clientAuth="false"
sslProtocol="TLS"
/>

退出并保存,最后重启Tomcat即可。

四、解决警告

如果网页中存在不带https的资源,比如http协议的js、css或图片,那么访问这个https页面,某些浏览器(比如IE)就会发出警告,提示页面中存在不安全的内容,并且不会加载这些http协议的资源,导致页面错乱等问题:

解决办法:

方法①、使用相对地址

只要将这些http的资源链接,改为相对地址。比如原链接是<img src="http://yourdomain.com/images/demo.png">那么改成<img src="/images/demo.png">即可。

方法②、修改网站代码

如果是全局https访问,那么你将网站代码中的链接均改为https好了。如果是http和https混合的,那么准备2套网站文件也行。然后在nginx当中设置不同的root路径。

为了省事,我推荐方法①。

好了,本文就写到这,希望能解您的燃眉之急!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK