Ubuntu: Simulating a Web Server using Netcat

ubuntuWhen tasked with deploying a web application and it is not responsive to your browser requests, sometimes you need to take a step back from the complexity of your full stack and run a quick sanity check.

You can use netcat as a simple web server to prove to yourself that the network infrastructure is allowing the traffic, the guest OS is not blocking the port with its own firewall, and the browser can receive the HTTP response.

Start the netcat HTTP Server

If you want to refer back to my post on the minimal TCP server using netcat, read here.  Extending that concept, here is the bash command to echo out a basic set of HTTP headers and body on port 8080:

echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n<h1>hello world from $(hostname) on $(date)</h1>" |  nc -vl 8080

You can now use either a web browser from the desktop or a console client like curl to make an HTTP request and the output should look like below:

# curl http://myserver:8080
<h1>hello world from myserver on Mon Sep 26 13:10:22 UTC 2016</h1>

If you cannot get a response, there is either an issue at the network infrastructure level or the firewall on the guest OS.  You can either disable the firewall completely as a test or selectively enable the server port:

# ufw allow 8080/tcp

also note that unless you are root, you may not be able to bind to ports less than 1024.

HTTPS

Using this concept to create a secure TLS connection (HTTPS), is possible, but starts getting complex.

What you are usually doing with this concept is making sure your ports are not being blocked at the network infrastructure level or guest OS firewalls.  If that is the case, simply bind the HTTP server above to port 443 and use HTTP over the traditional SSL port

curl http://myserver:443

 

REFERENCES

http://stackoverflow.com/questions/16640054/minimal-web-server-using-netcat

http://serverfault.com/questions/102032/connecting-to-https-with-netcat-nc

http://www.commandlinefu.com/commands/view/9164/one-command-line-web-server-on-port-80-using-nc-netcat