本人做了一个网站 tou.mobi 属于一个小工具类的网站。就是让朋友在我这个网站上从excel复制汽车配件的号码,就可以轻松生成报价单,求购单的小工具。
刚开始是放在美国的VPS,页面打开速度还不错,但是就是在生成报价单,及求购单的速度一直没办法提高,这也难怪,因为我的小工具的汽配数据源在国内。
后来下了点钱,购买了个阿里云的服务器,就是最经济的那种。850元一年的配置。
买回来后,却发现如果不备案,就不能通过域名打开,于是我去申请备案,但是备案却被打回,原因是因为个人只能备案博客类的域名。
后来我一直想,怎么办呢,怎么利用上这个阿里云。钱不能白花呀。。
后面终于让我想到了一个办法,那就是通过反向代理的方式来实现了。思路是这样的:
域名解析到美国vps, vps 装的是nginx 反向代理到 阿里云服务器。
方法是行得通,唯一担心的是速度,因为通过了代理,速度肯定没有直接访问美国服务器快,而且美国服务器访问阿里云也肯定会有一定的延迟。如是先做个测试吧。
先用test.tou.mobi域名做测试 先把域名解析到美国服务器
配置了一下代码:
美国服务器:nginx conf文件如下:
server { listen 80; server_name test.tou.mobi; error_log /home/wwwlogs/nginx_error.log crit; location / { proxy_pass http://xxx.xxx.xxx.xxx:8080; proxy_redirect off; #媒体过期时间设长一些 if ($request_filename ~* .*\.(gif|jpg|jpeg|png|bmp|swf)$) { expires 30d; } #js和css过期时间设置为12小时; if ($request_filename ~ .*\.(js|css)?$) { expires 12h; } 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_set_header Content-Type $content_type; #proxy_hide_header Content-Type; } }
阿里云服务器的nginx.conf文件如下:
两边都设置好了,重启服务器server {
listen 8080;
server_name test.tou.mobi localhost;
error_log /home/wwwlogs/nginx_error.log crit;
location / {
root /home/wwwroot/toyou;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/wwwroot/toyou/$fastcgi_script_name;
include fastcgi_params;
}
}
访问test.tou.mobi 但是却无法打开。。出现了个502错误,好吧,我尝试用ip加端口的方式检查一下阿里云服务器,http://xxx.xxx.xxx.xx:8080 可以打开的呀。。
后面突然想起来了,阿里云域名不备案不是不能访问吗?用域名http://test.tou.mobi:8080 访问,果然,是无法打开,看来阿里云只对域名做限制呀。对ip不做限制
于是修改了一下美国服务器vps这边的nginx conf配置,其实只改了一个地方
再重启一下 nginx服务器,server {
listen 80;
server_name test.tou.mobi;
error_log /home/wwwlogs/nginx_error.log crit;
location / {
proxy_pass http://xxx.xxx.xxx.xxx:8080;
proxy_redirect off;
#媒体过期时间设长一些
if ($request_filename ~* .*\.(gif|jpg|jpeg|png|bmp|swf)$)
{
expires 30d;
}
#js和css过期时间设置为12小时;
if ($request_filename ~ .*\.(js|css)?$)
{
expires 12h;
}
proxy_set_header Host xxx.xxx.xxx.xxx; #就是这里,把host改掉,直接换成阿里云的ip
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Content-Type $content_type;
#proxy_hide_header Content-Type;
}
}
打开浏览器,ok,一切正常, 用站长工具:http://whichloadsfaster.com/ 比较一下 http://test.tou.mobi 和 http://tou.mobi的打开速度。 发现相差不是很多,大概差一秒吧。在可以接受的范围之内,但是在生成求购单和报价单的速度 有了大辐度的提高。
于狠下心了,将tou.mobi主域名 按test 的部署,切换成功~~~
呵呵,现在终于可以用上阿里云了。 以后开发更新文件,只需要在阿里云上更新就可以了,国内服务器更新文件也快了些。。
总结一下:
1、用了测试域名来做一些尝试,不影响主域名的正常使用。
2、利用了nginx 的反向代理功能,还隐藏了正式服务器的地址
3、阿里云服务器可以通过不同端口的配置,实现挂多个网站的目的。
4、遇到问题时,多多思考,服务器配置,也需要想一些办法进行调试,比如用 rewrite 来测试变量值。
5、对服务器配置更加熟悉了。