1. 程式人生 > >Apache 反向代理與修改網頁內容

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安裝是否成功,命令列輸入如下:
     [email protected]:~$ ls /var/www/html

列印輸出,產生了一個預設頁面:

      index.html
命令列輸入如下,檢視apache2的安裝目錄:
      
[email protected]
:~$ ls /etc/apache2
        列印如下內容:
      apache2.conf    conf-enabled  magic           mods-enabled  sites-available
      conf-available  envvars       mods-available  ports.conf    sites-enabled
        檢視apache的網頁配置檔案:
      [email protected]:~$ ls /etc/apache2/sites-available
        列印如下內容:
      000-default.conf  default-ssl.conf

        重新啟動apache服務:     

      [email protected]:~$ sudo /etc/init.d/apache2 restart

        啟動成功:

      [ ok ] Restarting apache2 (via systemctl): apache2.service.
       瀏覽器訪問http://localhost/,出現  Apache2 Ubuntu Default Page 預設頁面即apache安裝到此結束。
 

   2.2原始碼方式安裝

      去官方網站下載原始碼,停止命令列安裝的apache2
     [email protected]
:~/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 工具:
      [email protected]:~/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
        列印如下內容:
      [email protected]:~/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

      [email protected]:~/Desktop/depenforapche/apr-1.5.2$ libtoolize --automake --force

      [email protected]:~/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原始碼目錄:
      [email protected]:~/Desktop/depenforapche/apr-util-1.5.4$ ./configure --with-apr=/usr/local/apr
      [email protected]:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make
      [email protected]:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make install
      開始安裝apache,進入apache原始碼目錄:
      [email protected]:~/Desktop/httpd-2.4.16$ ./configure 
      [email protected]:~/Desktop/httpd-2.4.16$ sudo make
      [email protected]:~/Desktop/httpd-2.4.16$ sudo make install
      上面執行正確後,可以啟動apache測試安裝是否成功,進入安裝目錄:
      [email protected]:~/Desktop/httpd-2.4.16$ cd /usr/local/apache2/
      進入命令列目錄:
      [email protected]:/usr/local/apache2$ cd bin/
       啟動apache服務:
      [email protected]:/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配置檔案,
      [email protected]:/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安裝目錄:

      [email protected]:~$ whereis apache2

        列印如下內容,切換至apache的安裝目錄:

      apache2: /usr/sbin/apache2 /usr/lib/apache2 /etc/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz
        檢視proxy模組,並且啟用proxy代理模組:
      [email protected]:~$ 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工具是否安裝:
[email protected]:/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[email protected]:/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模組:
[email protected]:~/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模組:
[email protected]:~/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模組:
[email protected]:~/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服務:

[email protected]:~/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解析模組的功能,而沒有啟用該模組:
[email protected]:/etc/apache2/mods-enabled$ sudo ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
[email protected]:/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:
[email protected]:~/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 反向代理修改網頁內容

apahce與nginx經常被用於作為Web伺服器,其實apache與nginx有非常多的拓展功能,可以通過訪問官方網站了解相關內容。之前使用過apache實現反向代理的功能,於是把它記錄下來,方便以後做其他的配置。反向代理的時候,也使用了兩種方式實現了修改網頁內容的功能,

(Nginx+Apache)實現反向代理負載均衡

1、概念實現這個反向代理能力並不能算是一個特別複雜的任務,但是在負載均衡中要求特別高的效率,這樣實現起來就不是十分簡單的了。每針對一次代理,代理伺服器就 必須開啟兩個連線,一個為對外的連線,一個為對內的連線,因此對於連線請求數量非常大的時候,代理伺服器的負載也就非常之大了,在最後反向代理伺服器會成 為服務的瓶

Apache 正向代理反向代理配置

Apache提供了 mod_proxy 模組用於提供代理服務,能夠支援的包括正向代理、反向代理、透明代理、快取、負載均衡,HTTP代理、FTP代理、SSL代理等若干強大的功能。配置代理方法很簡單那,首先在 Apache 上啟用 mod_proxy 模組,需要注意的是,如果在

apache反向代理服務器

系統 tar 訪問 ssr 反向 linux squid 沒有 service 1 正向代理: 客戶端無法直接訪問外部的web,需要在客戶端所在的網絡內架設一臺代理服務器,客戶端通過代理服務器訪問外部的web(需要在客戶端的瀏覽器中設置代理服務器) 適用於: ①局域

Chrome瀏覽器任意修改網頁內容

tar 瀏覽器 doc log abs 技術分享 單擊 first top 在Chrome瀏覽器按F12,打開開發者工具,切換到console選項卡: 在下面的輸入行輸入下面的命令回車:document.body.contentEditable="true" 再

【Web】Nginx 反向代理負載均衡

連接 代理服務器 body 後端服務 style 執行 class redirect 配置文件 反向代理   反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給in

Nginx/Apache反向代理

gin oca lin 應用程序 分享 ogr con caption rri 初衷:一臺服務器上只有一個80端口,如果要部署多套網站,難免會有www.veblen.com:80808080或者www.veblen.com/test/child/pro.html這樣的尷

使用apache反向代理tomacat

客戶端瀏覽器 node type 客戶 易用 ble error_log 用戶 -1 起源 在大部分的生產環境中,基本上使用的都是java程序,從而促進了各種應用程序中間件的產生,在這裏大概有幾種,tomcat作為最著名的開源servlet容器,jboss也

Ngnix反向代理負載均衡[轉]

web 客戶端連接 虛擬域名 安裝 服務器性能 正則匹配 服務端 file add nginx啟動和關閉(centos平臺) /usr/local/nginx/sbin/nginx #啟動 /usr/local/nginx/sbin/nginx -s re

Apache反向代理Tomcat(mod_proxy方式)

eve mpm conf host 報文 支持 反向代理 jdk1 catalina Apache反向代理Tomcat(mod_proxy方式)環境使用兩臺服務器,apache服務器IP為192.168.8.18,tomcat服務器地址為192.168.8.16Tomcat

實踐NGINX的反向代理負載均衡

nginx源碼安裝實踐NGINX的反向代理與負載均衡 安裝nginx過程 [root@lb01 opt]# yum install pcre-devel openssl-devel -y [root@lb01 opt]# wget -q http://nginx.org/download/nginx-1.1

Nginx反向代理負載均衡

宋體 static initial connect make man perf 為什麽 腳本 第1章 Nginx反向代理與負載均衡1.1 集群簡介若要用一句話描述集群,即一堆服務器合作做同一件事為什麽要使用集群1)高性能2)價格有效性3)可伸縮性(Scalability)

隨意修改網頁內容的代碼 xg

document sign javascrip scrip tab ble con false lse 先把這段代碼輸入到地址欄: javascript:document.body.contentEditable=&#39;true&#39;; docume

Nginx反向代理後端服務采用連接池參數分析,長連接減少TIME_WAIT

數據 模型 業務需求 技術 nginx服務器 程序 創建 反向代理 還需 前面已經講過,在使用locust直連後端服務器時,可以通過設置HTTP頭部為keep-alive,並在客戶端斷開連接,減少服務器的連接壓力。因為由客戶端斷開連接,客戶端的連接會變為TIME_WAIT狀

Nginx快速入門(二)反向代理負載均衡

接下來我們將介紹Ngix 反向代理 、負載均衡 、HTTP伺服器(包含動靜分離) 、正向代理 等幾個方面,由於本人接觸使用也沒有多久,所以還會有很多不全以及錯誤的地方,還望大家指正 共同進步。 1.反向代理 反向代理(Reverse Proxy)方式是指以代理伺服器來接受intern

Linux環境下Apache反向代理金蝶中介軟體Apusic叢集

作業系統:RedHat Enterprise Linux 5.6 文件參考:《金蝶Apusic應用伺服器 幫助手冊| IX. Apusic Http Server使用指南》   一、金蝶中介軟體配置 金蝶中介軟體需要修改的是叢集環境中所有節點域中的apusic.conf和vm.options

Nginx反向代理負載均衡。

Nginx反向代理與負載均衡   叢集簡介 簡單地說,叢集就是指一組(若干個)相互獨立的計算機,利用高速通訊網路組成的一個較大的計算機服務系統,每個叢集節點(即叢集中的每臺計算機)都是執行各自服務的獨立伺服器。這些伺服器之間可以彼此通訊,協同向用戶提供應用程式,系統資源和資料,並以

Nginx反向代理負載均衡應用實踐

Nginx反向代理與負載均衡應用實踐 課堂筆記   一、原理   1.1 為什麼要使用叢集 (1)高效能 一些國家重要的計算密集型應用(如天氣預報,核試驗模擬等),需要計算機有很強的運算處理能力。以全世界現有的技術,即使是大型機,其計算能力也是有限的,很難單獨

Linux 下 nginx反向代理負載均衡

前面幾篇記錄下nginx的基本運功,代理伺服器的訪問,這裡來試驗下nginx的反向代理。 反向代理(Reverse Proxy)方式是指以 代理伺服器來接受internet上的連線請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連線的客戶端,此時

nginx的反向代理負載均衡

1、概念:          正向代理:客戶端明確被訪問伺服器 反向代理:客戶端不清楚被訪問伺服器,只清楚代理伺服器   負載均衡   2、nginx負載均衡的實現