服务器迁移之后,突然想起之前在 NameCheap
买域名的时候顺便买了一年的 SSL,于是决定花些时间配置到 WordPress 里,因为 SSL的速度比较慢,所以只有在登录,注册和管理控制台才使用 SSL。下面记录下我配置的过程:

准备证书
因为Nginx的SSL模块使用的是 OpenSSL,所以要通过 OpenSSL 来生成公钥和私钥。
在终端(Terminal)里执行如下命令:
openssl req -new -newkey rsa:2048 -nodes -out www_nealmi_com.csr -keyout www_nealmi_com.key -subj "/C=cn/ST=Beijing/L=Beijing/O=SlientWalker Inc/OU=IT/CN=www.nealmi.com"
如果你觉得看着头大,可以使用在线生成工具来生成命令(https://www.digicert.com/easy-csr/openssl.htm)

然后复制到终端(Terminal)里去执行。
执行之后会生成一个公钥(*.csr)和一个私钥(*.key)
接下来到 NameCheap 的网站激活 SSL, 依次选菜单 Manage Domains -> SSL Certificates(左侧)- > Activate Now(每一个SSL产品后面) 就到了下面这个页面了。
打开公钥,复制里面的内容到上图的csr框里,Web Server 选择other (因为我使用 Nginx)。下一步之后,选择邮箱激活。之后你会收到一封授权邮件(基本上和网站的Email账户激活流程差不多),打开email里的链接,输入email里的激活码。之后不久你就会收到一封带有附件的邮件。里面有四个文件,其中一个是你域名的证书(www_xxx_com.crt), 另外三个是Root证书和中级证书。
证书已经准备完成,开始配置服务器。
Nginx SSL 配置
因为 Nginx 不像Apache那样可以单独指定你的证书和认证链证书(Root和中级),所以你要合并他们(*.crt)的内容到一个文件里(*.csr)。
文件内容必须为如下顺序:(你的证书-中级证书2-中级证书1-Root证书)
# Your SSL Certificate – www_xxx_com.crt
# Intermediate CA Certificate 2 – PositiveSSLCA.crt
# Intermediate CA Certificate 1 – UTNAddTrustSGCCA.crt
# Root CA Certificate – AddTrustExternalCARoot.crt
你可以直接使用文本编辑器合并,也可以使用cat命令
cat www_xxx_com.crt PositiveSSLCA.crt UTNAddTrustSGCCA.crt AddTrustExternalCARoot.crt > www_xxx_com_chain.csr
为了避免出错,建议你下载合并好的(https://support.comodo.com/index.php?_m=downloads&_a=viewdownload&downloaditemid=31&nav=0,1,4)认证链证书(Root和中级),然后简单和你自己的证书合并即可。
cat www_xxx_com.crt PositiveSSL.ca-bundle > www_xxx_com_chain.csr
终于到Nginx的SSL配置了,路途坎坷啊。
server {
listen 80;
listen 443 default_server ssl;#这个是 Nginx 0.7.14 之后的写法
# 路径根据你的实际情况修改
ssl_certificate /webserver/nginx/conf/www_nealmi_com_chain.csr;
ssl_certificate_key /webserver/nginx/conf/www_nealmi_com.key;
#下面注释参数根据你自己的需要设置,一般不设置也正常运行。
#keepalive_timeout 70;
#ssl_session_cache shared:SSL:10m;
#ssl_session_timeout 10m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
server_name www.nealmi.com;
index index.html index.htm index.php;
root /wwwroot/wordpress;
# 省略其他配置....
}
这不是结束,远远不是结束。
路漫漫其修远兮,吾将上下而求索。
WordPress相关的配置:
根据官方的介绍:http://codex.wordpress.org/Administration_Over_SSL,Wordpress本身需要修改 wp-setting.php:
在
require_once(ABSPATH . 'wp-settings.php');
之前,加入
define('FORCE_SSL_ADMIN', true); // 管理控制台和登录都使用SSL,我使用这种。或者
define('FORCE_SSL_LOGIN', true); // 仅登录使用SSL另外,为了一些插件能正常识别https,需要将 WordPress 地址(URL)修改为 https://www.nealmi.com, 站点地址(URL)不变(还是:http://www.nealmi.com)
官方还介绍了一些Apache的配置方法,只是因为我使用的是 Nginx + Apache 事情就稍稍复杂了一些。
在之前的文章我介绍了如何让 Nginx 和 Apache 使用相同的端口 :迁移网站到阿里社区云全过程。
还是沿用这个方案:
Nginx 监听公网IP的 80 和 443 端口
Apache 监听内网(本地)的 80 和 443 端口
修改后的Nginx配置
server {
listen 公网IP:80;
listen 公网IP:443 default_server ssl;#这个是 Nginx 0.7.14 之后的写法
# 路径根据你的实际情况修改
ssl_certificate /webserver/nginx/conf/www_nealmi_com_chain.csr;
ssl_certificate_key /webserver/nginx/conf/www_nealmi_com.key;
#下面注释参数根据你自己的需要设置,一般不设置也正常运行。
#keepalive_timeout 70;
#ssl_session_cache shared:SSL:10m;
#ssl_session_timeout 10m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
server_name www.nealmi.com;
index index.html index.htm index.php;
root /wwwroot/wordpress;
location / {
try_files $uri /index.php ;
}
#所有 php 请求由Apache处理
location ~ .*\\.(php)$ {
proxy_pass $scheme://127.0.0.1:$server_port; # 使用内置变量转发80请求到Apache的80,443到Apache的443
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#静态文件由Nginx处理
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
}
修改后Apache的配置:
Listen 127.0.0.1:80
Listen 127.0.0.1:443
#...省略无关内容
NameVirtualHost 127.0.0.1:80
NameVirtualHost 127.0.0.1:443
#...省略无关内容
ServerAdmin admin@admin.com
ServerName www.nealmi.com
DocumentRoot /wwwroot/main_site
SSLEngine on
SSLCertificateFile /server/nginx/conf/secure_nealmi_com.crt
SSLCertificateKeyFile /server/nginx/conf/secure_nealmi_com.key
SSLCertificateChainFile /server/nginx/conf/PositiveSSL.ca-bundle
ErrorDocument 404 /404.php
DirectoryIndex index.html index.php index.htm
Options +Includes
AllowOverride FileInfo
Order allow,deny
Allow from all
ServerAdmin admin@admin.com
DocumentRoot //wwwroot/main_site
ServerName www.nealmi.com
ErrorDocument 404 /404.php
DirectoryIndex index.html index.php index.htm
Options +Includes
AllowOverride FileInfo
Order allow,deny
Allow from all
目前为止,Nginx 和 Apache 已经配置好了,WordPress也已经修改完成。胜利在望啦!!
接下来要做的就是写RewriteRules,目的就是
1.当在 80 端口访问 admin 和 login 的时候,重写到 443 端口处理(http://www.nealmi.com/wp-admin -> https://www.nealmi.com/wp-admin)
2.当在 443 端口访问 非 admin 和 login 的时候, 重写到 80 端口处理 (https://www.nealmi.com/2011/08/ -> http://www.nealmi.com/2011/08/)
在wordpress根目录下的 .htaccess(没有就新建一个)里添加如下内容(网址根据你的实际情况修改):
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^https://www.nealmi.com$ http://www.nealmi.com [L]
RewriteRule ^https://nealmi.com$ http://www.nealmi.com [L]
# For a site running on port 443 or else (http over ssl)
# 供 443 端口上使用, 对应上文描述的 RewriteRules 目的 2
RewriteCond %{SERVER_PORT} !^80$
RewriteRule !^wp-(admin|login|register)(.*) - [C]
RewriteRule ^(.*)$ http://www.nealmi.com/$1 [L]
# For a site running on port 80 (http)
# 供 80 端口上使用,对应上文描述的 RewriteRules 目的 1
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^wp-(admin|login|register)(.*) https://www.nealmi.com/wp-$1$2 [L] #关键步骤
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
花了我近一天时间,反复的调试,修改,终于完成所有的配置。你可以试试我网站的登录。