I wanted a little mock server that just embeds the HTTP request header in the response and returns it when checking the operation of proxy_pass of nginx.
Speaking of simple HTTP servers, python's one-liner is enough for just static file distribution,
$ sudo python -m SimpleHTTPServer 80
This time I wanted to embed the HTTP request header in the body of the response and return it, so I wrote a simple python script. By the way, python is implemented in python because it is included in many Linux distributions by default and can be done with only the standard library and no additional dependent modules. By the way, python is 2.7 series.
$ python --version
Python 2.7.12
Create a script like this with the name mock_server.py
.
mock_server.py
#!/usr/bin/env python
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write('%s %s %s\n' % (self.command, self.path, self.request_version))
self.wfile.write(self.headers)
self.finish()
self.connection.close()
port = 80
server = HTTPServer(('', port), RequestHandler)
server.serve_forever()
Execute via sudo for the convenience of listening on port 80.
$ sudo python mock_server.py
Try curl.
$ curl -H "Host: hoge.example.com" -H "X-MOCK-PROXY-PASS: hoge.example.com" http://127.0.0.1/hoge
GET /hoge HTTP/1.1
Host: hoge.example.com
User-Agent: curl/7.47.1
Accept: */*
X-MOCK-PROXY-PASS: hoge.example.com
If you put nginx you want to test in between and configure it like curl => nginx => mock server, you can see if the expected HTTP header is passed, and if you fill in the appropriate header for debugging, which location I feel like I can easily test if proxy_pass is done with.