wordpress/nginx安全设置

来自:互联网
时间:2019-05-08
阅读:

最近闲时研究了下博客的安全,除去系统的安全问题,主要关注点在登录这一块。

一、安装wordpress插件实现安全登录

Google Authenticator (使用谷歌两步验证加强 WordPress 登录安全)

我选择了这个真正能保证wp安全的这个插件,这个插件提供双重安全验证登录,跟谷歌帐户的“两步验证”是一样的原理。在wp上安装好这个插件并启用后,进入“用户”-》“我的个人资料”会看到设置界面:

选中“Active”,填写“站点描述”,保存。然后根据自己的手持设备 安装 Google 身份验证器(我在我的apple设备及黑莓设备上都安装了),添加令牌(填写站点描述及密钥)后就绑定了。然后就可以在登录界面( 亲,切记在手持设备上绑定后再登出啊 )看到如下情况了:

如果博客不只一个用户,其它用户也可以在他自己的资料里面启用这种两步验证的登录方式,如果用户没有启用,那么登录的时候不用填写“google authenticator code”。

当然了,通过搜索还有其它登录安全的插件,如 One Time Password (采用一次性密码登录你的 WordPress,应该类似于银行纸质的那种刮刮卡)、 WP Login Security (根据登录用户IP地址来保护你的 WordPress 安全)、 Login Lockdown (锁定无效登录)、 WP Firewall 2 。

二、设置SSL登录及SSL后台

HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容请看SSL。

它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。

首先要在wordpress中设置强制SSL登录及SSL后台,具体做法是:

在wordpress的根目录中找到“wp-config.php”

设置两个常量“FORCE_SSL_LOGIN”、“FORCE_SSL_ADMIN”为true。

我用得是Nginx反向代理,只有如上设置肯定是不行的。

要做下面的操作前,首先要保证安装nginx的时候安装了ssl的支持,通过命令“nginx -V”就能查看你在编译的时候添加没有添加“-with-http_ssl_module”这个参数。如果没加,重新编译安装一次吧(记得备份配置文件)。

nginx准备好后就开始设置了,我在这里就不说收费的受浏览器信任的证书了,我用了“自颁发不受浏览器信任的证书”。

输入以下命令:

openssl req -new -x509 -days 365 -newkey rsa:2048  -nodes -out nginx.pem -keyout nginx.key

会生成两个文件,当然也会让你填很多问题,

Generating a 1024 bit RSA private key
...................................++++++
..............................++++++
writing new private key to 'nginx.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New Jersey
Locality Name (eg, city) []:Absecon
Organization Name (eg, company) [Internet Widgits Pty Ltd]:SoftwareDev, LLC
Organizational Unit Name (eg, section) []:Web Services
Common Name (eg, YOUR name) []:squire.ducklington.org
EmAIl Address []:squire@ducklington.org

“Common Name”一定要是你的域名 ,然后在nginx.conf中设置:

server {
        listen       443;
        server_name  yourdomain;
 
        ssl                  on;
        ssl_certificate      nginx.pem;
        ssl_certificate_key  nginx.key;
 
        ssl_session_timeout  5m;
 
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
 
        add_header      Front-End-Https on;
        root /xxx;
 
        location / {
                rewrite ^ http://$host$request_uri permanent;
        }
        location /wp-admin {
                fastcgi_pass unix:/tmp/php-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~.*.(php|php5)?$ {
                fastcgi_pass unix:/tmp/php-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
}
#下面是80端口vhost中的
rewrite ^/wp-login.php(.*) https://$host/wp-login.php$1 permanent;
location ~ ^/(wp-admin)/* {
        rewrite ^/(.*) https://$host/$1 permanent;
}

然后重新加载nginx应该就可以ssl登录了。

当然了,我遇到个问题,就是https获取CSS文件,返回的MIME类型为text/html。如下:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "https://blog.ueder.info/wp-admin/css/wp-admin.css?ver=3.4.1". 
Resource interpreted as Stylesheet but transferred with MIME type text/html: "https://blog.ueder.info/wp-admin/css/colors-fresh.css?ver=3.4.1".

出错原因在于:上面的conf文件中location /wp-admin,将所有请求都传给fastcgi_pass处理了,php cgi中始终返回text/html,所以修改conf文件后,css、js文件nginx来处理就解决问题了。

server {
        listen       443;
        server_name  yourdomain;
 
        ssl                  on;
        ssl_certificate      nginx.pem;
        ssl_certificate_key  nginx.key;
 
        ssl_session_timeout  5m;
 
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
 
        add_header      Front-End-Https on;
        root /xxx;
 
        set $rewriteHttp "yes";
 
    location / {
        if ($request_uri ~* ^/wp-login.php) {
            set $rewriteHttp "no";
        }
        if ($request_uri ~* ^/wp-admin) {
            set $rewriteHttp "no";
        }
        if ($rewriteHttp ~ ^yes$) {
                        rewrite ^ http://$host$request_uri permanent;
        }
        index index.html index.htm index.php;
 
        }
 
        location ~.*.(php|php5)?$ {
                fastcgi_pass unix:/tmp/php-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
}

参考资料:

1、 SSL Certificates with Nginx

2、 Administration Over SSL

返回顶部
顶部