mongodb
pymongo使用代理 或 ssh隧道访问 mongodb
## 使用ssh隧道 , ssh隧道库: pip install sshtunnel
server = sshtunnel.open_tunnel((HOST, xxxxxx),
ssh_username="xxx",
ssh_password='xxxx',
remote_bind_address=(MONGODB_HOST, MONGODB_PORT))
server.start() # server最好设定为全局变量
atexit.register(lambda : server.stop())
# 启动ssh隧道链接后,再链接mongodb
client = pymongo.MongoClient('xxx.xxx.xxx.xxx', server.local_bind_port, directConnection=True, username=MONGODB_USER, password=MONGODB_PASSWORD, authSource='admin' ) #同样对存在副本的情况也必须使用 directConnection=True
#使用代理 及分片,MONGODB_DATABASE_URI为 使用代理 host,port拼接的url
client = pymongo.MongoClient(MONGODB_DATABASE_URI, directConnection=True) #对于存在副本的情况必须添加directionConnnection参数
nginx
关于nginx 代理中的重定向处理
location /xxx_proxy/ {
proxy_pass http://host_a/;
proxy_intercept_errors on; # 目标服务器返回的300以上的错误都会被捕获。
error_page 301 302 307 = @handle_redirects;
}
location @handle_redirects {
set $saved_redirect_location 'http://host_a$upstream_http_location';
proxy_pass $saved_redirect_location;
}
// 其中 $upstream_http_location 代表了 重定向的路径( 如果是绝对路径, 可以不加http://host_a, 如果是相对路径则需要加上host