How to proxy a web site by apache2 in Ubuntu

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

Install apache2

To execute the install command in terminal:

sudo apt-get install apache2

Then, we can find that the apache2 has been installed in "/etc/" directory.

[email protected]:cd /etc/apache2[email protected]:/etc/apache2$ apache2 -versionServer version: Apache/2.4.7 (Ubuntu)Server built:   Apr  3 2014 12:20:28[email protected]:/etc/apache2# ls -ltotal 80-rw-r--r-- 1 root root  7115 Jan  7 21:23 apache2.confdrwxr-xr-x 2 root root  4096 Jun 17 15:09 conf-availabledrwxr-xr-x 2 root root  4096 Jun 17 15:09 conf-enabled-rw-r--r-- 1 root root  1782 Jan  3 22:48 envvars-rw-r--r-- 1 root root 31063 Jan  3 22:48 magicdrwxr-xr-x 2 root root 12288 Jun 17 15:09 mods-availabledrwxr-xr-x 2 root root  4096 Jun 17 15:09 mods-enabled-rw-r--r-- 1 root root   320 Jan  7 21:23 ports.confdrwxr-xr-x 2 root root  4096 Jun 17 15:08 sites-availabledrwxr-xr-x 2 root root  4096 Jun 17 15:09 sites-enabled
Attention:

After executing the install command, some echo exception messages may shown like that.

AH00558: apache2: 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(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80no listening sockets available, shutting down

If so, we need to:

1) Config the "ServerName" in apache2.conf.

[email protected]:cd /etc/apache2[email protected]:cd vi apache2.conf...ServerName localhost...

2) End the existed process which is using the 80 socket.

netstat -ap | grep 80lsof -i:80kill {PID}

Or modify the listen socket. (See Config listening ports)

Then, we can restart apache2.

[email protected]:sudo /etc/init.d/apache2 restart
Config listening ports

We can change and add the listening ports by modifying port.conf file in "/etc/apache2/".

[email protected]:sudo vi /etc/apache2/ports.conf

For example, we change the default port from 80 to 81 to avoid the in used portd.

Listen 81<IfModule ssl_module>    Listen 443</IfModule><IfModule mod_gnutls.c>    Listen 443</IfModule>

After changing the default port, the default site configuration (/etc/apache2/sites-enabled/000-default.conf) also need be updated.

[email protected]:sudo vi /etc/apache2/sites-enabled/000-default.conf

Modify

<VirtualHost *:80>

as

<VirtualHost *:81>
Config proxy or reverse proxy

Here, there is a Tomcat worked in 8080 port as our J2EE server and an application named "jreport" running in it. We will config the apache to proxy it.

1. Activate proxy module

There are "mods-available" and "mods-enabled" two directories in apache. The "mods-available" directory includes all available module configuration files. If we want to make them take effect, they must be copied or linked into the "mods-enabled" directory.

For activating the proxy module, we create some soft link for "proxy.load", "proxy_http.load" and "proxy.conf".

[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.load[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy_http.load[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.conf

Then, execute the a2enmod command.

[email protected]:/etc/apache2$ a2enmod proxy
2. Config proxy

After activating the proxy module, we can config the "Forward Proxy" or "Reverse Proxy" for the "jreport" application in Tomcat.

  • Reverse Proxy

Reverse proxy is the most used way.

ProxyRequests OffProxyPass /jreport ${JREPORT_SERVER}/jreportProxyPassReverse /jreport ${JREPORT_SERVER}/jreport

or

ProxyRequests OffTimeout 36000ProxyTimeout 36000<Location /jreport/>    ProxyPass ${JREPORT_SERVER}/jreport    ProxyPassReverse ${JREPORT_SERVER}/jreport    ProxyPassReverseCookiePath /jreport /</Location>

For easy to config, we define a variable named "JREPORT_SERVER" in "/etc/apache2/envvars".

export JREPORT_SERVER=http://192.168.0.88:8080

After restarting the apache with the latest configuration, we can access the "jreport" application with:

http://localhost:81/jreport
  • Forward Proxy

For example, to control who can access your proxy:

ProxyRequests OnProxyVia On<Proxy *>  Require ip 192.168.0</Proxy>

For more details, please see the official doc about mod_proxy.

Add SSL Support1. Install openssl and ssl_cert
[email protected]: sudo apt-get install openssl ssl_cert
2. Generate private key and certification
[email protected]: sudo mkdir /etc/apache2/ssl[email protected]: cd /etc/apache2/ssl[email protected]:/etc/apache2/ssl$ sudo openssl genrsa -des3 -out my-server.key 1024[email protected]:/etc/apache2/ssl$ sudo openssl req -key my-server.key -x509 -out my-server.crt -config /etc/ssl/openssl.cnf -days 3650
3. Activate SSL module
[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.load[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.conf[email protected]:/etc/apache2/mods-enabled$ sudo a2enmod ssl
4. Add SSL support for site

Now, we modify the default site configuration (/etc/apache2/sites-enabled/000-default.conf) to add SSL support and make non-https access use the https automatically.

Usually, we config the 443 port for SSL support.

<VirtualHost *:81>    ...    RewriteEngine on    RewriteCond %{HTTPS} !=on    RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]</VirtualHost><VirtualHost *:443>    SSLEngine on    SSLCertificateFile /etc/apache2/ssl/my-server.crt    SSLCertificateKeyFiel /etc/apache2/ssl/my-server.key    ...</VirtualHost>
Postscript

I have just recorded my first attempt to proxy a web site by apache for memo. There are some other useful and complex modules in apache, such as rewrite, load balance and so on.

Reference
  1. Apache official doc: http://httpd.apache.org/docs/2.4/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.