使用 nginx 做加速
nginx 一般做反向代理服务器。并且可以代理到任何可以访问的节点上。
例如我们发现某个 restful 的 api 网站,国内访问速度不佳,我们可以借助一台境外节点上的nginx来做加速。
先安装 nginx,然后修改 nginx.conf
如下:
user www;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
proxy_ssl_server_name on;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
include /etc/nginx/conf.d/*.conf;
}
其中 map $http_upgrade $connection_upgrade
是为了支持 websocket
。其中的 proxy_ssl_server_name
和 ssl_protocols
是为了更好的代理 https
站点,然后具体的加速代理长点在 /etc/nginx/conf.d/*.conf;
。
/etc/nginx/conf.d/
目录下写一个 PROXY.conf
server {
listen 80;
server_name api2-poloniex-com.example.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
location / {
proxy_pass https://api2.poloniex.com;
client_max_body_size 10m;
}
}
server {
listen 80;
server_name poloniex-com.example.com;
location / {
proxy_cookie_domain poloniex-com.example.com poloniex.com;
proxy_cookie_domain poloniex.com poloniex-com.example.com;
proxy_pass https://poloniex.com;
client_max_body_size 10m;
}
}
第一个 server
是代理 websocket,需要设置 Upgrade
,第二个是 restful 接口,可以把 cookie 设置上。
NGINX caching proxy fails with SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure