使用 nginx 做加速

使用 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_namessl_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值