Apache 反向代理与修改网页内容

apahce与nginx经常被用于作为Web服务器,其实apache与nginx有非常多的拓展功能,可以通过访问官方网站了解相关内容。之前使用过apache实现反向代理的功能,于是把它记录下来,方便以后做其他的配置。反向代理的时候,也使用了两种方式实现了修改网页内容的功能,也碰到了一些问题,折腾了不少时间。


1、系统环境

   ubuntu+apache
   ubuntu使用15.10版本,apache使用源码安装或者apt-get命令安装。部署好ubuntu系统后,即开始apache的安装配置,支持反向代理以及修改网页内容的功能。
   

2、安装apache

   2.1命令方式安装

      sudo apt-get install apache2
  测试apache安装是否成功,命令行输入如下:
     liang@ubuntu:~$ ls /var/www/html

打印输出,产生了一个默认页面:

      index.html
命令行输入如下,查看apache2的安装目录:

      liang@ubuntu:~$ ls /etc/apache2
        打印如下内容:
      apache2.conf    conf-enabled  magic           mods-enabled  sites-available
      conf-available  envvars       mods-available  ports.conf    sites-enabled
        查看apache的网页配置文件:
      liang@ubuntu:~$ ls /etc/apache2/sites-available
        打印如下内容:
      000-default.conf  default-ssl.conf

        重新启动apache服务:     

      liang@ubuntu:~$ sudo /etc/init.d/apache2 restart

        启动成功:

      [ ok ] Restarting apache2 (via systemctl): apache2.service.
       浏览器访问http://localhost/,出现  Apache2 Ubuntu Default Page 默认页面即apache安装到此结束。
 

   2.2源码方式安装

      去官方网站下载源代码,停止命令行安装的apache2
     liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 stop
      安装依赖软件
     sudo apt-get install libtool autoconf build-essential
       源代码安装apr,进入apr源代码目录,运行 ./configure  报告出错:
      rm: cannot remove 'libtoolT': No such file or directory
      config.status: executing default commands
 
       尝试安装 autoconf automake libtool 工具:
      liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ sudo apt-get install autoconf automake libtool
      打印如下,系统已经安装如上工具:
      Reading package lists... Done
      Building dependency tree       
      Reading state information... Done
      autoconf is already the newest version.
      automake is already the newest version.
      automake set to manually installed.
      libtool is already the newest version.
      0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

       在执行./configure 之前,先执行:

      $autoreconf --force --install     
      $libtoolize --automake --force
      $automake --force --add-missing

        打印如下内容:
      liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ autoreconf --force --install
      aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
      build/apr_network.m4:24: warning: underquoted definition of APR_TYPE_IN_ADDR
      build/apr_network.m4:24:   run info Automake 'Extending aclocal'
      build/apr_network.m4:24:   or see http://www.gnu.org/software/automake/manual/automake.html#Extending-aclocal
      libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build'.
      libtoolize: copying file `build/ltmain.sh'
      libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `build'.
      libtoolize: copying file `build/libtool.m4'
      libtoolize: copying file `build/ltoptions.m4'
      libtoolize: copying file `build/ltsugar.m4'
      libtoolize: copying file `build/ltversion.m4'
      libtoolize: copying file `build/lt~obsolete.m4'
      libtoolize: Consider adding `-I build' to ACLOCAL_AMFLAGS in Makefile.am.
      libtoolize: `AC_PROG_RANLIB' is rendered obsolete by `LT_INIT'
      aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
      build/apr_network.m4:24: warning: underquoted definition of APR_TYPE_IN_ADDR
      build/apr_network.m4:24:   run info Automake 'Extending aclocal'
      build/apr_network.m4:24:   or see http://www.gnu.org/software/automake/manual/automake.html#Extending-aclocal

      liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ libtoolize --automake --force

      liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ automake --force --add-missing
      automake: warning: autoconf input should be named 'configure.ac', not 'configure.in'
      configure.in: error: no proper invocation of AM_INIT_AUTOMAKE was found.
      configure.in: You should verify that configure.in invokes AM_INIT_AUTOMAKE,
      configure.in: that aclocal.m4 is present in the top-level directory,
      configure.in: and that aclocal.m4 was recently regenerated (using aclocal)
      configure.in:207: installing 'build/install-sh'
      BUG.am: error: 'install.sh' is an anachronism; use 'install-sh' instead
      automake: error: no 'Makefile.am' found for any configure output

      重新输入命令完成apr安装即可: 
      ./configure && sudo make && sudo make install
      开始安装apr-util,进入apr-util源码目录:
      liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ ./configure --with-apr=/usr/local/apr
      liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make
      liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make install
      开始安装apache,进入apache源码目录:
      liang@ubuntu:~/Desktop/httpd-2.4.16$ ./configure 
      liang@ubuntu:~/Desktop/httpd-2.4.16$ sudo make
      liang@ubuntu:~/Desktop/httpd-2.4.16$ sudo make install
      上面执行正确后,可以启动apache测试安装是否成功,进入安装目录:
      liang@ubuntu:~/Desktop/httpd-2.4.16$ cd /usr/local/apache2/
      进入命令行目录:
      liang@ubuntu:/usr/local/apache2$ cd bin/
       启动apache服务:
      liang@ubuntu:/usr/local/apache2/bin$ sudo ./httpd 
       提示出错:
      AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
       去修改apache配置文件,
      liang@ubuntu:/usr/local/apache2/bin$ sudo vi /usr/local/apache2/conf/httpd.conf

      去掉下面的注释,重启apache2即可使用,浏览器访问服务器地址,出现 It works! 字样,即安装成功。

     #ServerName www.example.com:80

3、配置apache反向代理

       这里的配置使用的是命令行安装的apache,源代码安装的配置原理一样,但是配置过程不一样,大概是操作http.conf文件。

        查看apache安装目录:

      liang@ubuntu:~$ whereis apache2

        打印如下内容,切换至apache的安装目录:

      apache2: /usr/sbin/apache2 /usr/lib/apache2 /etc/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz

        查看proxy模块,并且启用proxy代理模块:
      liang@ubuntu:~$ cd /etc/apache2/mods-available
        启用proxy代理模块:
sudo ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load
sudo ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load
sudo ln -s /etc/apache2/mods-available/proxy_balancer.load /etc/apache2/mods-enabled/proxy_banancer.load

 
  
 
 
        添加代理模块的配置,这里以sina与163为例进行配置:
vi /etc/apache2/sites_available/proxy
        写入下面的内容:
ProxyRequests Off
ProxyVia On
UseCanonicalName Off
ProxyMaxForwards 10000


<VirtualHost *:80>
ServerAlias www.sina.com.cn
<Proxy>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass / http://www.sina.com.cn/
ProxyPassReverse / http://www.sina.com.cn/
</VirtualHost>


<VirtualHost *:80>
ServerAlias www.163.com
<Proxy>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass / http://www.163.com/
ProxyPassReverse / http://www.163.com/
</VirtualHost>


 
 
        启用代理配置:
sudo ln -s /etc/apache2/sites_available/proxy /etc/apache2/sites_enabled/proxy
sudo /etc/init.d/apache2 restart
 
  
 
 
         到此配置反向代理已经成功,具体测试过程请参照第五步。

4、配置apache修改网页

         查看apxs工具是否安装:
liang@ubuntu:/usr/share/apache2/build$ apxs
         打印如下即没有安装:
The program 'apxs' is currently not installed. You can install it by typing:
         使用命令行安装:
sudo apt-get install apache2-dev
         再次查看apxs工具是否安装:
l iang@ubuntu:/usr/share/apache2/build$ apxs
         打印如下即安装成功:
Usage: apxs -g [-S <var>=<val>] -n <modname>
       apxs -q [-v] [-S <var>=<val>] [<query> ...]
       apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]]
               [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>]
               [-Wl,<flags>] [-p] <files> ...
       apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
       apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...

 
  
 
  
 
 
          部署修改网页的功能,去下载 mod_line_edit.c 模块源代码,mod_line_edit 可以实现网页内容修改功能,详情说明内容到mod_line_edit网站查看。        添加mod_line_edit模块:
liang@ubuntu:~/workspace/mod_line_edit$ apxs -c mod_line_edit.c 
        打印如下内容,提示缺少pcre支持:
/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security  -D_FORTIFY_SOURCE=2   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -pthread  -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include  -c -o mod_line_edit.lo mod_line_edit.c && touch mod_line_edit.slo
mod_line_edit.c:26:18: fatal error: pcre.h: No such file or directory
compilation terminated.
apxs:Error: Command failed with rc=65536
 
 
         更新系统的软件源,并且安装pcre库支持:
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
 
  
 
  
 
 
          再次添加mod_line_edit模块:
liang@ubuntu:~/workspace/mod_line_edit$ sudo apxs -i -a -c mod_line_edit.c 
         打印如下内容,即可进行下一步操作:
/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security  -D_FORTIFY_SOURCE=2   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -pthread  -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include  -c -o mod_line_edit.lo mod_line_edit.c && touch mod_line_edit.slo
/usr/share/apr-1.0/build/libtool --silent --mode=link --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -Wl,--as-needed -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now    -o mod_line_edit.la  -rpath /usr/lib/apache2/modules -module -avoid-version    mod_line_edit.lo
/usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' mod_line_edit.la /usr/lib/apache2/modules
/usr/share/apr-1.0/build/libtool --mode=install install mod_line_edit.la /usr/lib/apache2/modules/
libtool: install: install .libs/mod_line_edit.so /usr/lib/apache2/modules/mod_line_edit.so
libtool: install: install .libs/mod_line_edit.lai /usr/lib/apache2/modules/mod_line_edit.la
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/lib/apache2/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/lib/apache2/modules


If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'


See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 644 /usr/lib/apache2/modules/mod_line_edit.so
[preparing module `line_edit' in /etc/apache2/mods-available/line_edit.load]
Enabling module line_edit.
To activate the new configuration, you need to run:
  service apache2 restart

 
  
 
 
         配置修改网页功能,执行命令vi /etc/apache2/sites_available/proxy,编辑proxy文件,覆盖下面内容到文件中:
ProxyRequests Off
ProxyVia On
UseCanonicalName Off
ProxyMaxForwards 10000
#RequestHeader unset Accept-Encoding


<VirtualHost *:80>
ServerAlias www.sina.com.cn
<Proxy>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass / http://www.sina.com.cn/
ProxyPassReverse / http://www.sina.com.cn/


<IfModule line_edit_module>
RequestHeader unset Accept-Encoding
SetOutputFilter line-editor
SetEnv LineEdit 'text/plain;text/css;text/html;test/javascript'
LERewriteRule photo.sina.com.cn  hello.photo.sina.com.cn
</IfModule>


</VirtualHost>


<VirtualHost *:80>
ServerAlias www.163.com
<Proxy>
Order Deny,Allow
Allow from all
</Proxy>
ProxyPass / http://www.163.com/
ProxyPassReverse / http://www.163.com/
</VirtualHost>
 
  
 
 
         查看是否已经启用mod_line_edit模块:
liang@ubuntu:~/workspace/mod_line_edit$ ll /etc/apache2/mods-enabled/line_edit.load 

         打印如下内容,即已经启用:

lrwxrwxrwx 1 root root 32 Feb 25 13:35 /etc/apache2/mods-enabled/line_edit.load -> ../mods-available/line_edit.load

         此时重启apache服务:

liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 restart

         如果提示出错:

[....] Restarting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
 failed!
 
  
 
 
         配置了headers解析模块的功能,而没有启用该模块:
liang@ubuntu:/etc/apache2/mods-enabled$ sudo ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
liang@ubuntu:/etc/apache2/mods-enabled$ ll /etc/apache2/mods-enabled/headers.load 
 
 
        打印如下内容:
lrwxrwxrwx 1 root root 40 Feb 25 13:59 /etc/apache2/mods-enabled/headers.load -> /etc/apache2/mods-available/headers.load
        再次重启apache:
liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 restart
       启动成功:
[ ok ] Restarting apache2 (via systemctl): apache2.service.
        到此,修改网页内容的功能配置已经完成,根据第五步进行测试。其实,还有一种修改网页内容的方法,但是是针对URL的,使用模块mod_proxy_html。

5、测试过程

        测试环境:虚拟机ubuntu15.10 代理服务器、虚拟机windows7 客户机。两个虚拟机都使用NAT的网络连接方式,修改Windows7的hosts文件,末尾添加如下内容:
192.168.213.132    www.sina.com.cn
192.168.213.132    www.163.com
 
 
        但是之前配置的hosts文件是如下格式的:
192.168.213.132    sina.com.cn
192.168.213.132    163.com
        不知道为何现在要配置成上面的格式才可以正常运行,留下了一个问题需要去思考。
        浏览器访问sina主页,发现http://photo.sina.com.cn/的url修改成了http://hello.photo.sina.com.cn/,证明了上面的反向代理以及修改网页内用两个模块功能配置成功。这时候,去查看apache2日志即可看到代理了163与sina网站。

你可能感兴趣的:(apache,proxy,ubuntu,反向代理,edit_line)