http-apache

Introduction

modules

Apache provides lots of to user, these modules provide lots of directives you can use in conf file through these, user can modify the behavior of apache like modify request, response header etc.

check enabled modules

1
2
3
$ ls /etc/apache2/mods-enabled/
access_compat.load authn_core.load authz_user.load deflate.load filter.load mpm_prefork.load php5.load ssl.conf
...

check available modules

1
2
3
$ ls /etc/apache2/mods-available/
access_compat.load authz_core.load charset_lite.load file_cache.load macro.load proxy_ajp.load remoteip.load speling.load
...

enable a mod

1
2
# just add a symbol link in /etc/apache2/mods-enabled/ to  /etc/apache2/mods-available/
$ ln -s /etc/apache2/mods-available/macro.load /etc/apache2/mods-enabled/

create web server by python

In this case, you can add any header and any body to client, fully controlled by your self.

define your own simple server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /usr/bin/env python3.5
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# only add status line, server and date headers
self.send_response(200)

# add response header by yourself
#self.send_header("Content-Length", "0")
# flush the header buffer
self.end_headers()
self.wfile.write(b'Hello, world!\n')
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()

use built-in simple server

1
2
3
4
5
6
7
8
9
10
11
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer

class GetHandler(SimpleHTTPRequestHandler):
def do_GET(self):
# static web server, rewrite like above for dynamic
SimpleHTTPRequestHandler.do_GET(self)

httpd = TCPServer(("", 8000), GetHandler)

httpd.serve_forever()

FAQ

enable keep-alive

edit /etc/apache2/apache2.conf with below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On

# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100

#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection. when timeout, apache will close the connection
#
KeepAliveTimeout 500

return application/json for xx.json file

1
AddType 'application/json; charset=UTF-8' .json

modify response header

modify header feature is provided by mod_headers.so, first you should enable such module by ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/

1
2
# apache.conf
Header set Host "www.test.com";

Remember to restart service service apache2 restart

more details about