点击数:43
WordPress站点的Nginx服务配置https
生成证书
参考《SSL证书生成》,生成适合于自己的免费证书。也可购买商业SSL证书。
如果只想生成CSR,用CSR在CA机构换取CRT证书,可以执行下面命令即可:
openssl req -new -nodes -newkey rsa:2048 -keyout server.key -out server.csr
配置Nginx
# 配置80端口跳转到443端口
server {
listen 80;
server_name www.abc.zyx;
# rewrite ^ https://$server_name$request_uri permanent;
rewrite ^(.*)$ https://$host$1 permanent;
}
# 配置WordPress的https服务
server {
#listen 80;
#listen [::]:80;
listen 443 ssl;
server_name www.abc.zyx;
charset utf-8;
ssl_certificate ssl/server.crt;
ssl_certificate_key ssl/server.key;
ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:CAMELLIA256-SHA:CAMELLIA128-SHA256";
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html/abc;
index index.html index.htm index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
try_files $uri $uri/ /index.php?$args;
}
#rewrite /wp-admin$ $scheme://$host$uri/ permanent;
#try_files $uri $uri/ /index.php?q=$uri&$args;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
root /var/www/html/abc;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
try_files $uri =404;
}
location ~ /\.ht {
deny all;
}
}
注意:ssl_protocols配置项,不添加TLSv1会导致部分IE浏览器打不开,如果加上TLSv1用站长工具检测会提示PCI DSS 不合规。
重新加载配置文件:nginx -s reload
。
如果是阿里云ECS服务器,需要在控制台的安全组中增加入网的443端口,具体配置可以参照其他的配置。
如果这时候https还不能访问,则需要查看Ubuntu服务器是否开启了443端口,如果无,则需要开启。
查看443端口是否开启:
iptables -L -n | grep 443
开启443端口,并让其生效:
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
iptables-save
WordPress后台改成https访问
WordPress后台改成https访问,有下面三种方式:主题支持改动、SQL UPDATE、wp-config.php文件中添加。
主题支持改动
如果主题中有修改网站URL的入口,则直接在admin管理后台修改即可。
如果使用的 WP Editor.md
插件,也需要修改思维导图库资源路径,一般情况下可以使用//
替换http://
,这样资源就可以根据站点的scheme自动切换了。
SQL UPDATE
update scat_posts
set post_content = replace(post_content, 'http://www.abc.xyz', '//www.abc.xyz')
where post_content like '%http://www.abc.xyz%';
update scat_posts
set guid = replace(guid, 'http://www.abc.xyz', 'https://www.abc.xyz')
where guid like '%http://www.abc.xyz%';
通过找到当前主题下的 function.php 文件
function replacehttp($content){
if( is_ssl() ){
$content = str_replace('http://www.abc.xyz', 'http://www.abc.xyz', $content);
}
return $content;
}
add_filter('the_content', 'replacehttp');
wp-config.php文件中添加
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);