前几天遇到一个比较难搞的CDN,想了半天还是做了反代。
1、 首先安装Nginx(随便怎么装都行)
sudo apt-get install nginx
2、 在/etc/nginx/nginx.conf中的http{}部分include一个vhost的conf,例如
http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; include /etc/nginx/yoursite.conf; #在最后添加一行 }
3、 vhosts.conf的内容可以这样写
## Basic reverse proxy server ## ## backend for yoursite ## upstream apachephp { server 1.2.3.4; #源站IP } ## Start yoursite ## server { listen 80; #监听端口 server_name yoursite.com; #绑定域名 root html; index index.php; ## send request back to source ## location / { proxy_pass http://yoursite.com; #回源域名 稍后会讲一个小技巧 #Proxy Settings 按照实际需求修改以下参数 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } ## End yoursite ##
4、 重载Nginx
nginx -s reload