前言
搜索引擎越来越重视https网站(有ssl证书的网站),这也许是一种大的趋势吧。但是,给网站添加了ssl证书后,也存在着一些麻烦。配置好后,网站通过https://xxx.com这样的域名形式可以访问了,而https://12.23.43.32这样的IP也是可以访问的,这就有点头痛了,如果没有对服务器IP做禁止限制,就有可能被黑客所利用。
这不,前端时间我的阿里云网站就被黑客入侵过,通过https://12.23.43.32这种类似的IP访问,没有打开我的网站,而是直接跳转到一个澳门赌博网站。所以阿里云一直在给我发短信,说我的网站发布违法违规信息,叫我赶快整顿。
怎么办呢?只有禁止https通过ip访问了。下面就是禁止IP访问的方法。
代码实现
我的服务器是linux下的nginx,所以配置如下:
# 禁止IP直接访问网站 http
server {
listen 80 default;
server_name _;
return 403;
}
#https禁止IP直接访问网站
server {
listen 443 ssl default_server;
server_name _;
ssl_certificate cert/xxx.pem; # 允许的网站 ssl证书
ssl_certificate_key cert/xxx.key; #允许的网站ssl证书钥匙
return 500;
}
注:如果不添加 红色区域的证书和密钥,不仅通过IP不能访问,而且通过域名也不能访问。红色区域是我们https网站的证书和钥匙在服务器中的文件路径。这个在我们配置网站ssl时的代码中有。如下是阿里云网站的ssl配置代码:
server {
listen 443;
server_name xxx.com *.xxx.com;
ssl on;
root /xxx/www/xxx;
index index.html index.htm index.php;
ssl_certificate cert/xxx.pem;
ssl_certificate_key cert/xxx.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
}
当然,服务器只有一个网站,也可以把2段代码整合在一起。
本文暂时没有评论,来添加一个吧(●'◡'●)