Nginx反向代理反向域名绑定绕过国内域名白名单封锁

国内的备案很纠结,提交资料往往要半个月左右才会审批下来,而且提交的资料稍有不对就会打回来让你重新更正,好吧,再等半个月吧。。。可说是相当的抓狂加蛋疼!如果不备案的话就算域名绑定好了也直接被白名单给拦截了。很烦,想要不备案就绑定域名,有方法没?当然是有,那就要用到我们强大的武器nginx和反向代理绑定域名了。

Nginx(“engine x”)是俄罗斯人Igor Sysoev编写的一款高性能HTTP 和反向代理服务器。Nginx选择了epoll和kqueue作为网络I/O模型,在高连接并发的情况下,Nginx是Apache服务器不错的替代品,它能够支持高达50000个并发连接数的响应,运行稳定,且内存、CPU等系统资源消耗非常低。国内使用Nginx的公司有:淘宝、新浪、搜狐、网易、腾讯、金山、TOM、中华网、赛尔网络、上海九城等。。。

反向绑定域名也叫做反向D理,是指用D理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时D理服务器对外就表现为一个服务器。

什么时候要用到反向绑定域名呢?反向绑定可以分流请求和负载均衡,因此它广泛用于一些大流量的网站和数据集群,降低系统负载。对于一些不能直接绑定域名的主机空间,我们可以用反向绑定域名的方法来强制绑定自己的域名。

安装Nginx

1、安装Nginx所需的pcre库:

tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

2、安装Nginx

tar zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54/
./configure –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
cd ../

3、创建Nginx日志目录

mkdir -p /data1/logs
chmod w /data1/logs
chown -R www:www /home/wwwlogs

安装完成,然后编辑Nginx的配置文件进行配置。

vi /usr/local/nginx/conf/nginx.conf

一个Nginx的配置实例:

user  www www;

worker_processes 1;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /tmp/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{
include       mime.types;
default_type  application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone  crawler  $binary_remote_addr  10m;
server {
listen 80;
server_name old.wangxun.me;
location / {
proxy_pass http://new.wangxun.me/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
保存后检查下配置文件是否有错误:
/usr/local/nginx/sbin/nginx -t
如果结果是:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
那就说明配置文件没有错误。否则就说明配置文件出错,配置正确后运行以下内容使配置生效:
/usr/local/nginx/sbin/nginx -s reload
到此已经成功。另外注意正确的域名解析。
例如上面我想在VPS上建一个old.wangxun.me的域名用来反向代理访问new.wangxun.me,需要先在域名管理上为域名old.wangxun.me添加A记录到VPS的IP上。

上面的Nginx配置文件只是示例,实际应用中Nginx并不是只能用做反向代理服务器。还可以用其作为WEB服务器,而且性能不输Apache哦!返回正题:我们其实只需要新建配置文件。内容如下:
server
{
listen          80;
server_name     old.wangxun.me;

location / {
proxy_pass              http://new.wangxun.me/;
proxy_redirect          off;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然后在主配置文件(nginx.conf)中加载就可以实现反向代理了。


另外,介绍一下LNMP一键环境安装包的反向代理绑定域名的方法:

例如购买了阿里云服务器,没有备案的域名就不能绑定,这时我们用反向绑定域名方法就可以搞定了,其它的国内空间要绑定域名也是一样。本篇文章就来分享一下使用VPS主机的Nginx反向代理配置来搭建反向D理服务器。

1、要使用Nginx反向配置,你需要一个已经安装了Nginx的VPS主机,部落用的是LNMP,创建一个虚拟机,绑定域名:alili.freehao123.info.

2、执行:cd /usr/local/nginx/conf/vhost,找到alili.freehao123.info.conf这个文件,删除其中的内容,将以下代码替换保存,上传,覆盖。

server {
listen  80;
server_name  alili.freehao123.info;

location / {
proxy_pass http://115.28.7.69: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 115.28.7.69;
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;
}

}
3、其中proxy_pass和proxy_set_header Host是阿里云服务器的IP地址。有关阿里云VPS评测参考:阿里云服务器ECS性能与速度简单测试。

4、阿里云服务器也是安装了LNMP,同样也是创建一个虚拟机,绑定的域名也是alili.freehao123.info,找到该域名conf这个文件,是改动的就是listen ,其它的都是一样的。

5、这是我的阿里云服务器的Nginx反向配置,大家可以参考一下。

log_format  alili.freehao123.info  '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
listen       8080;
server_name alili.freehao123.info localhost;
index index.html index.htm index.php default.html default.htm default.php;
root  /home/wwwroot/alili.freehao123.info;

include wordpress.conf;
location ~ .*.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}

location ~ .*.(js|css)?$
{
expires      12h;
}

access_log  /home/wwwlogs/alili.freehao123.info.log  alili.freehao123.info;
}
6、改完配置后,执行:/usr/local/nginx/sbin/nginx -t,如果出现:nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok和nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful表示修改代码正确。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注