最近,公司搬迁,专线更随着更换了。没有想到,新申请的专线是要备案的,不然专线IP对外的80和443是被封掉的。但备案基本上是要一周时间,这样,公司内部的exchange和OA服务器因为没有80和443端口而无法在公司外使用。解决方案是使用nginx或HAproxy反向代理。
The solution
- 在Firewall上设置端口映射,把专线对外IP的TCP 2443 map到 443,TCP 1080 map 到 80.
- 在Ucloud云主机上安装nginx ,reverse proxy 到公司专线的TCP 2443和 1080
- 在DNS上更改相应的A记录,指向新的nginx reverse proxy完成配置
Nginx reverse proxy configration
server {
listen 80;
server_name mail.corp.example.com autodiscover.corp.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
client_max_body_size 0;
ssl_certificate /etc/nginx/conf.d/ssl/mail.corp.example.com.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/mail.corp.example.com.key;
server_name mail.corp.example.com cas.corp.example.com autodiscover.corp.example.com;
ssl_session_timeout 10m;
access_log /opt/log/nginx/mail.corp.example.com.log main;
error_log /opt/log/nginx/mail.corp.example.com.error.log;
open_log_file_cache max=100;
location / {
proxy_ssl_server_name on;
proxy_ssl_session_reuse off;
proxy_ssl_verify off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header cookie $http_cookie;
proxy_set_header Proxy-Connection "";
proxy_http_version 1.1;
proxy_pass https://xxx.xxx.xxx.xxx:2443;
}
}
HAproxy reverse proxy configration
上面的配置有一个问题,用手机访问没有问题,但如果客户端是outlook2013以前的版本,那是无法认证通过的。因为 nginx 免费版本不支持 RPC over http.但是haproxy支持 RPC over http, 下面是用haproxy来实现的haproxy.conf配置文件:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
# option httplog
option dontlognull
option http-server-close
# option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend ft_http
bind *:80
mode http
log global
redirect scheme https if !{ ssl_fc }
frontend ft_https
bind *:443
mode tcp
log global
use_backend example_com
backend example_com
mode tcp
server ip_xxx.xxx.xxx.xxx_2443 xxx.xxx.xxx.xxx:2443 weight 1 maxconn 3000 check
在上面的配置文件中,我把所有http 转向了https,并用mode tcp 模式,在4层上,把所有tcp 443端口的数据转发到公司外网IP xxx.xxx.xxx.xxx:2443端口上。
Relevant matters needing attentions
- proxy_ssl_server_name on; 要记得加上,不然主机头会丢失,如果后端IIS有多个站点的话,会出404.
- windows 上的exchange 的证书要转换成nginx 可以识别的证书,放到nginx reserve proxy上。用haproxy的tcp mode方式没有这个问题,可以不用做证书转换。
- 上面的nginx配置,用手机访问没有问题,但如果客户端是outlook2013或以前的版本,那是无法认证通过的。因为 nginx 不收费版本不支持 RPC over http.但是haproxy支持 RPC over http, 大家可以用haproxy来实现。
相关链接。