加强网络安全
之前去公安备案的时候,警察给我讲了关于网络安全的事情,把我说慌了,但也确实要加强。
一方面是加强网络安全防护,另一个要做到日志备份,正好最近也通过了公安备案,所以加强一下以上两点。
网络安全防护
加入SSL
很简单,就是http变成https,这个操作很简单,跟着网络上的教程走一下就行了,在nginx里面设置一下,就行了。
设置CDN
加一层CDN吧,尽管CDN是拿来加速的,但也有一定的网络安全防护的作用。虽然没钱用SCDN,但是普普通通的一层简单CDN还是消费的起的,我趁促销买了9.9一年100GB,虽然看起来很小吧,但是对于我的博客正常运行还是非常够的,甚至刚开通的时候不要买套餐,会送6个月,每个月20G,用都用不完,主要我现在还没开启SEO,所以顶得住。
我买的腾讯云,去CDN网络分发中心——域名管理——自己的域名——HTTPS设置里,把所有配置都给拉满,什么http2.0,tls1.3,全给拉满。自己服务器的nginx也可以设置相同的配置。
然后去访问控制里面按照自身情况设置一下安全防护,我就新增了一下IP访问频现配置,当然,也可以直接拉黑别人。
然后去回源配置里设置回源HTTP请求头配置,新增X-Forward-For的,这个很重要!!!,因为现在客户端访问域名都会只能获得CDN的ip,而不是客户端真实的IP,所以需要开启这个,获得真实客户端的IP。
ufw防火墙
然后开启ufw防火墙,不过腾讯云自带一层防火墙,在你买的服务器面板上设置,不过为了以防万一,我还是开启自带的防火墙好了,具体命令如下:
#查看防火墙的状态
sudo ufw status
#关闭防火墙
sudo ufw disable
#开启防火墙
sudo ufw enable
#开启或者关闭端口
sudo ufw allow/deny 9090
日志备份
nginx配置
因为设置了CDN,所以需要获取到真实的IP地址,下面这步其实不用做的,因为CDN已经返回header了,下面这步的目的是排除某部分的ip,我就把我的服务器ip给排除掉,这样子log就不会出现了。
在server里面设置,或者查看nginx网站设置。
set_real_ip_from 我的服务器ip;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
然后设置主配置,在http里面设置log的格式,就是新增$http_x_forwarded_for,这样子日志就能看到真实IP了(注意日志要在后面加上main,表示使用这个配置,否则不会生效,记得重加载nginx配置)。
log_format main '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" ';
access_log /var/log/nginx/access.log main;
nginx日志
nginx的日志在/var/log/nginx,但默认只有15天,但网安的警察和我说日志至少需要半年,所以15天的不够,于是要重新设置。设置nginx日志分隔的配置文件在 /etc/logrotate.d/nginx,根据自己的需要更改,我更改为每周转储一次,然后保留26个,正好半年。
/var/log/nginx/*.log {
weekly
missingok
rotate 26
dateext
dateformat -%Y-%m-%d
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
}
然后重新加载一下。
logrotate /etc/logrotate.d/nginx
wordpress设置
这个比较简单,因为都有相应的插件,这里我安装两个插件,一个是WP Activity Log,还有一个Website File Changes Monitor。
因为之前CDN的问题,这里评论审核的IP也不是正确的,需要修改一下wp-config.php,加入下面的代码,然后就会显示正确的IP了。
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];
}
拓展
设置nginx白名单
参考链接:http://www.wyasw.com/webdoc/view/Pub8a80808a7124caaf01712643b9400227.html
因为套了一层CDN,nginx不能获得真实IP,虽然上面能够打印出真实ip,但是却不能使用allow和deny,因为allow和deny针对的是直接访问的ip,也就是说还是CDN的ip。(烦死了CDN,打又打不掉)
因此需要自己写一个方法判断是不是真实ip。为了不是阿猫阿狗都能够访问我的wp-login.php,我直接把这个界面给限制ip,只有白名单的能够通过。
在自己的网站配置文件里写。如果匹配不到自己白名单ip,那么就不能够登录,要不然就正常展示登录界面。
location = /wp-login.php {
if ($http_x_forwarded_for !~* "你的ip|另一个ip") {
return 411;
}
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
error_page 411 /wp-content/411/411.html;
区域控制访问
也是CDN的设置,在访问控制,不过目前腾讯云内测,免费,我先嫖一段时间,估计之后要收费了。我只设置成只有中国才能够访问,正经人谁用外网访问我的网页。
附件
nginx网站设置
upstream php {
server unix:/run/php/php8.0-fpm.sock;
#server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name szx.life;
## Your only path reference.
root /usr/share/nginx/html/wordpress;
## This should be in your http block and if it is, it's not needed here.
index index.php;
# open SSL
listen 443 ssl http2;
#Certificate file name
ssl_certificate 1_szx.life_bundle.crt;
#Private key file name
ssl_certificate_key 2_szx.life.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
client_max_body_size 50m;
fastcgi_buffers 256 4k;
set_real_ip_from 你的服务器IP;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
rewrite ^/images$ /wp-content/random-image/images.php last;
rewrite ^/images/(.*?)$ /wp-content/random-image/images.php?folder=$1 last;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|webp)$ {
expires max;
log_not_found off;
}
location = /wp-login.php {
if ($http_x_forwarded_for !~* "你的ip") {
return 411;
}
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
error_page 411 /wp-content/411/411.html;
}
server {
listen 80;
#填写绑定证书的域名
server_name szx.life,www.szx.life;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
nginx主设置
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
log_format main '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" ';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}





