最近發(fā)現(xiàn)服務(wù)器被挖礦病毒侵害娜睛,Nginx里的網(wǎng)站源碼都是靜態(tài)的髓霞,只暴露了22和80,443接口畦戒,密碼長(zhǎng)度16位方库,實(shí)在不知道是怎么被人搞的。
思來想去障斋,懷疑Trojan一鍵搭建腳本里可能有什么不干凈的東西纵潦,于是決定自己手工用Docker搭建一個(gè)Trojan。
一垃环、安裝Docker
apt-get update
apt-get upgrade
apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
bionic \
stable"
apt-get install docker-ce docker-ce-cli containerd.io
二邀层、獲取Https證書
申請(qǐng)https證書,在Docker下晴裹,為了方便被济,用DNS的方式驗(yàn)證網(wǎng)站的所有權(quán)。
domain=""
Ali_Key=""
Ali_Secret=""
output="$(pwd)/out"
docker run --rm -it \
-v "$output":/acme.sh \
-e Ali_Key="$Ali_Key" \
-e Ali_Secret="$Ali_Secret" \
neilpang/acme.sh --issue --log --dns dns_ali -d "$domain"
mkdir -p /etc/trojan/
ln -sf "$output"/"$domain"/fullchain.cer /etc/trojan/fullchain.crt
ln -sf "$output"/"$domain"/"$domain".key /etc/trojan/private.key
續(xù)期
docker run --rm -it \
-v "$output":/acme.sh \
-e Ali_Key="$Ali_Key" \
-e Ali_Secret="$Ali_Secret" \
neilpang/acme.sh --renew --force --log --dns dns_ali -d "$domain"
三涧团、Nginx
mkdir -p /var/www/html
mkdir -p /opt/nginx/logs
#拷出默認(rèn)配置只磷,便于定制化
docker pull nginx
docker run --name tmp-nginx -d nginx
docker cp tmp-nginx:/etc/nginx/nginx.conf /opt/nginx/
docker cp tmp-nginx:/etc/nginx/conf.d /opt/nginx/
docker cp tmp-nginx:/usr/share/nginx/html/index.html /var/www/html/
docker rm -f tmp-nginx
#運(yùn)行
docker run \
--name nginx \
--net host \
-v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/conf.d:/etc/nginx/conf.d \
-v /opt/nginx/logs:/var/log/nginx \
-v /var/www/html:/usr/share/nginx/html \
-d nginx
四经磅、Trojan
運(yùn)行Trojan
docker run \
--name trojan \
--net host \
-v /etc/trojan:/etc/trojan \
-v /etc/trojan/fullchain.crt:/etc/trojan/fullchain.crt \
-v /etc/trojan/private.key:/etc/trojan/private.key \
-d teddysun/trojan
五、Nginx配置
開啟全站https钮追,在conf.d/default.conf配置修改至如下:
#本地網(wǎng)站目錄预厌,供Trojan訪問
server {
listen 127.0.0.1:80 default_server;
server_name <domain>;
location / {
root /var/www/html;
index index.html index.htm;
}
}
# server_name替換為服務(wù)器ip,目的是元媚,當(dāng)有人直接訪問服務(wù)器ip時(shí)轧叽,跳轉(zhuǎn)至https網(wǎng)站
server {
listen 127.0.0.1:80;
server_name <10.10.10.10>;
return 301 https://$host$request_uri;
}
# 將所有80端口的流量重定向至443
server {
listen 0.0.0.0:80;
listen [::]:80;
server_name _;
location / {
return 301 https://$host$request_uri;
}
}