因为v2ray的加密措施,导致代理确实慢一些,才萌发了使用Trojan的想法,考虑到Trojan 独占443端口,无法和nginx 共用的问题,参考了下网上的做法,实现了nginx 多网站443 与Trojan 共存
1.安装 Trojan
bash -c "$(curl -fsSL https://raw.githubusercontent.com/trojan-gfw/trojan-quickstart/master/trojan-quickstart.sh)"
#默认配置文件在 /usr/local/etc/trojan/config.json,我不是很喜欢这个位置
mkdir /etc/trojan
cp /usr/local/etc/trojan/config.json /etc/trojan/
vim etc/systemd/system/trojan.service
#修改配置文件位置 ,具体如下
------
[Unit]
Description=trojan
Documentation=https://trojan-gfw.github.io/trojan/config https://trojan-gfw.github.io/trojan/
After=network.target network-online.target nss-lookup.target mysql.service mariadb.service mysqld.service
[Service]
Type=simple
StandardError=journal
ExecStart="/usr/local/bin/trojan" "/etc/trojan/config.json"
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=51200
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
------
你启动文件都改了,当然需要重载一下
systemctl daemon-reload
2.nginx 我是使用宝塔的,版本 1.18,其他版本的话,你检查 nginx -V,有
—with-stream_ssl_preread_module 模块即可,没有的话,你就需要手动编译安装了,网上教程很多,这里不再赘述
3.修改宝塔nginx 默认配置,在events 和 http中间新增
stream {
map $ssl_preread_server_name $name {
#有多少网站写多少网站
www.xxx.top blog;
nginx.xxx.top trojan;
}
#记住下面的端口,后面要用
upstream blog {
server 127.0.0.1:3000; #blog 博客
}
upstream trojan {
server 127.0.0.1:3001; #trojan端口
}
server {
listen 443 reuseport;
listen [::]:443 reuseport;
proxy_pass $name;
ssl_preread on; #开启 ssl_preread
}
}
4.修改宝塔网站配置,之前的端口现在要用了(网站的配置跟默认的宝塔一致,添加网站,填写域名 ,申请证书 ssl,强制 https之类的,我也不说了,说的话,要写好多,我默认你配好了),这是你自己想正常访问的站点,重启下nginx服务,检查下访问有没有什么问题
listen 80;
listen 443 ssl http2;
server_name www.xxx.top;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/xxx.top/public;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
5.Trojan站点配置
新增网站 配置ssl 修改配置文件 80端口是拿来续签用的,请不要动,不用和上面 stream里的冲突就好了,前往不要和Trojan的端口冲突,否则Trojan是启动不了的,这里其实也是想方便续签而已
listen 80;
listen 443 ssl http2;
server_name nginx.xxx.top;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/nginx.xxx.top;
6.trojan配置
{
"run_type": "server",
"local_addr": "127.0.0.1",
"local_port": 3001,//和stream里配置的Trojan 端口填写一致,上面的流量会代理到这个端口来
"remote_addr": "127.0.0.1",
"remote_port": 443,
"password": [
"XXXXXXX"//自己定义一个密码,复杂一点
],
"log_level": 1,
"ssl": {
//证书地址 之前宝塔不是创建了一个Trojan续签的网站吗,没错,把配置里的证书地址直接拿过来就好了
"cert": "/www/server/panel/vhost/cert/nginx.xxx.top/fullchain.pem",
"key": "/www/server/panel/vhost/cert/nginx.xxx.top/privkey.pem",
"key_password": "",
"cipher": "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384",
"cipher_tls13": "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384",
"prefer_server_cipher": true,
"alpn": [
"http/1.1"
],
"alpn_port_override": {
"h2": 81
},
"reuse_session": true,
"session_ticket": false,
"session_timeout": 600,
"plain_http_response": "",
"curves": "",
"dhparam": ""
},
"tcp": {
"prefer_ipv4": false,
"no_delay": true,
"keep_alive": true,
"reuse_port": false,
"fast_open": false,
"fast_open_qlen": 20
},
"mysql": {
"enabled": false,
"server_addr": "127.0.0.1",
"server_port": 3306,
"database": "trojan",
"username": "trojan",
"password": "",
"key": "",
"cert": "",
"ca": ""
}
}
7.接下来,重启下Trojan
systemctl restart trojan
没什么问题的话,你就可以用了