参考https://linux.do/t/topic/491533 的反代配置
上面帖子提供了宝塔反代的教程
下面分享下docker反代的教程
准备工作
vps安装docker
vps安装Nginx proxy manage
将域名解析到服务器
正式开始
新建反代目录,创建如下几个文件
docker-compose文件
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx_reverse_proxy
ports:
- "9923:9923"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log/nginx
restart: always
nginx配置
配置中这句改自己的域名
proxy_redirect https://z-library.sk/ https://你的域名/;
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 9923;
listen [::]:9923;
resolver 8.8.8.8 ipv6=off; # 关闭 IPv6 DNS 解析,防止网络不可达错误
location / {
resolver 8.8.8.8; # 使用公共 DNS
proxy_pass https://z-library.sk;
proxy_ssl_server_name on;
proxy_set_header Host z-library.sk;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect https://z-library.sk/ https://你的域名/;
# 允许 Cookie 透传
proxy_cookie_domain z-library.sk $host;
proxy_cookie_path / /;
# 允许 WebSocket 连接
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
# 代理 WebSockets
proxy_buffering off;
# 解决 CORS 跨域问题
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'Content-Type, Authorization, Cookie';
add_header Access-Control-Allow-Credentials true;
# 取消安全限制,防止某些页面无法加载
proxy_hide_header X-Frame-Options;
proxy_hide_header X-Content-Type-Options;
proxy_hide_header X-XSS-Protection;
# 允许 Set-Cookie 头透传
proxy_pass_header Set-Cookie;
}
}
}
运行
docker-compose up -d
进入Nginx proxy manage添加代理,大致流程如下(域名,端口,ip注意替换)