域名访问
反代可以用Nginx
、Apache
、Caddy
,这里只说宝塔和Caddy
。如果你网站有宝塔,就可以使用宝塔进行反代,如果没有,建议使用第2
种的Caddy
反代,配置很快。
1、宝塔反代
先进入宝塔面板,然后点击左侧网站,添加站点,然后再点击添加好了的域名名称,这时候就进入了站点配置,点击反向代理,目标URL
填入http://IP:端口
,再启用反向代理即可。至于启用SSL
就不说了,直接在站点配置就可以启用。
2、Caddy反代
安装Caddy
:
wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubiBackup/doubi/master/caddy_install.sh && chmod +x caddy_install.sh && bash caddy_install.sh install http.filemanager
配置Caddy:
#以下全部内容是一个整体,请修改域名后一起复制到SSH运行!
#http访问,该配置不会自动签发SSL
echo "xx.com {
gzip
proxy / IP:端口 {
header_upstream Host {host}
header_upstream X-Real-IP {remote}
header_upstream X-Forwarded-For {remote}
header_upstream X-Forwarded-Proto {scheme}
}
}" > /usr/local/caddy/Caddyfile
#https访问,该配置会自动签发SSL,请提前解析域名到VPS服务器
echo "xx.com {
gzip
tls [email protected]
proxy / IP:端口 {
header_upstream Host {host}
header_upstream X-Real-IP {remote}
header_upstream X-Forwarded-For {remote}
header_upstream X-Forwarded-Proto {scheme}
}
}" > /usr/local/caddy/Caddyfile
tls
参数会自动帮你签发ssl
证书,如果你要使用自己的ssl
,改为tls /root/xx.crt /root/xx.key
即可。后面为ssl
证书路径。
启动Caddy
:
/etc/init.d/caddy start
就可以打开域名进行访问了。
3、Nginx配置
如果你使用其它的,这里就大概发个Nginx
反代配置,直接添加到配置文件即可。
#在配置文件里添加
location / {
proxy_pass http://IP:端口;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}