·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设问答 >> Nginx反向绑定域名方法和详细操作应用实例:Google和Gravatar

Nginx反向绑定域名方法和详细操作应用实例:Google和Gravatar

作者:佚名      网站建设问答编辑:admin      更新时间:2022-07-23

反向绑定域名,即将域名B绑定到域名A上,用户只要访问B就等同于进入A,内容都是由A提供,它有点像建立了一个A的镜像。什么时候要用到反向绑定域名?服务器集群和网站负载均衡时,把用户访问请求发送不同的服务器上。

关于反向绑定域名的方法部落之前也分享过好几次,本篇文章就来详细介绍一下Nginx反向绑定域名方法。之所以要用Nginx,主要在于Nginx在反向绑定域名有着天然的优势,并且功能强大,可以满足我们更多更高的应用需求。

平常我们都是用Nginx反向绑定域名来搞定无法绑定域名的空间,这次来分享一下搞定Google和Gravatar两个网站访问的问题,更多的有关于反向绑定域名的方法还有:

  1. 多种应用:反向绑定域名的方法-强制绑定域名,实现负载均衡,域名内网转发;
  2. 简单操作:反向绑定域名方法-Nginx反向配置和kangle服务器反向设置;
  3. 虚拟主机:7ghost基于PHP的网站反向绑定域名程序无需.htaccess的URL重写。

注意:上面提到的Nginx反向绑定域名都需要用到VPS主机,如果你只有虚拟主机,则可以试试7ghost。

Nginx反向绑定域名方法和详细操作应用实例:Google和Gravatar

一、Nginx安装和基本操作命令

1、Nginx可以直接使用LNMP这样的一键安装包,例如:LNMP新版VPS主机控制面板安装。

2、如果你是用一个专门的服务器来作反向绑定域名用,则只需要安装一个Nginx即可,为VPS主要省点资源。命令:

  • wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz
  • tar zxvf nginx-0.7.64.tar.gz
  • cd nginx-0.7.64
  • ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-ipv6
  • make && make install

3、如果在执行以上命令遇到./configure: error: the HTTP rewrite module requires the PCRE library.错误提示,运行:yum -y install pcre-devel openssl openssl-devel

4、执行以下命令,把ngx_http_substitutions_filter_module模块编译进去,主要为了反向绑定域名过滤到页面的URL地址。

  • git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
  • ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/root/nginx-0.7.64/ngx_http_substitutions_filter_module
  • make && make install

5、最后添加www用户,启动Nginx服务。

  • /usr/sbin/groupadd -f www
  • /usr/sbin/useradd -g www www
  • /usr/local/nginx/sbin/nginx

6、或者,你也可以直接使用以下命令:

  • cd /tmp
  • git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git # nginx 的过滤器模块(比http_sub_module更加灵活)
  • wget http://nginx.org/download/nginx-1.7.7.tar.gz
  • tar -xzvf nginx-1.7.7.tar.gz
  • cd /tmp/nginx-1.7.7
  • ./configure \
  • --prefix=/www/wdlinux/nginx \ # 安装位置
  • --with-http_ssl_module \
  • --with-http_sub_module \
  • --with-http_gzip_static_module \
  • --with-http_stub_status_module \
  • --add-module=/tmp/ngx_http_substitutions_filter_module # 添加nginx过滤器模块
  • make & make install

7、Nginx的配置文件一般是在:/usr/local/nginx/conf 这个目录下nginx.conf。

8、修改了nginx.conf文件后,记得先检测一下语法是否正常:/usr/local/nginx/sbin/nginx -t,防止重启Nginx后服务器不正常。

Nginx检查语法是否正确

9、Nginx重启命令:/usr/local/nginx/sbin/nginx -s reload 或者 kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 或者 service nginx resatrt

10、上面介绍的两种安装方法适合有一定VPS经验的朋友,这里还有一个适合新手朋友的安装命令:

  • yum -y install gcc automake autoconf libtool make
  • yum install gcc gcc-c++
  • cd /usr/local/src
  • wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
  • tar -zxvf pcre-8.34.tar.gz
  • cd pcre-8.34
  • ./configure
  • make
  • make install
  • cd /usr/local/src
  • wget http://zlib.net/zlib-1.2.8.tar.gz
  • tar -zxvf zlib-1.2.8.tar.gz
  • cd zlib-1.2.8
  • ./configure
  • make
  • make install
  • cd /usr/local/src
  • wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
  • tar -zxvf openssl-1.0.1c.tar.gz
  • cd /usr/local/src
  • wget http://nginx.org/download/nginx-1.4.2.tar.gz
  • tar -zxvf nginx-1.4.2.tar.gz
  • cd nginx-1.4.2
  • git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
  • ./configure --sbin-path=/usr/local/nginx/nginx \
  • --conf-path=/usr/local/nginx/nginx.conf \
  • --pid-path=/usr/local/nginx/nginx.pid \
  • --with-http_ssl_module \
  • --with-http_sub_module \
  • --with-http_gzip_static_module \
  • --with-http_stub_status_module \
  • --with-pcre=/usr/local/src/pcre-8.34 \
  • --with-zlib=/usr/local/src/zlib-1.2.8 \
  • --add-module=/usr/local/src/nginx-1.4.2/ngx_http_substitutions_filter_module \
  • --with-openssl=/usr/local/src/openssl-1.0.1c
  • make
  • make install
  • /usr/local/nginx/nginx