(blog-of ‘Alex’)

A personal blog about software development and other things. All opinions expressed here are my own, unless explicitly stated.

Quick Http Server

For many years I used to use python -mSimpleHTTPServer whenever I wanted to start an HTTP server to surface folder as a set of static files. It is however no longer available in python 3 which I think says a lot about maintainers of python as a language - retaining backwards compatibility never seemed to be a priority.

On python 3 you can use python3 -m http.server.

If you’re familiar with nginx and/or don’t want to continue using python for that simple purpose you can also use nginx, provided that you have it in one form or another - e.g. as a docker image on your local system.

Here are a few examples how to use it:

  1. Surface local folder, you must have a local html file in it, e.g. via echo '<h1>Hello</h1>' > index.html:
docker run --rm -it --network host -v $(pwd):/usr/share/nginx/html:ro --name local-nginx nginx

Above command uses host network, which may or may not be what you want. It is simple enough, albeit not secure if you expose port 80 to the outside world. Simply remove that if you’re OK with default docker network configuration or use port mapping. For added security, you can replace --network host with -p which will bind port 80 that nginx will listen to to the port 8080 on your local network interface, i.e. in the latter case it will not be accessible outside your local host.

  1. A more complicated example would be using your own nginx.conf, e.g.:
docker run --rm -it -p -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx

Key parts in the above example:

  • -p maps port 80 that nginx listens to to the localhost port 8080.
  • $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro assumes that you have nginx.conf in the current folder and maps it in read only mode to /etc/nginx/nginx.conf file in the docker container with nginx which is configured by default to pick up its configuration from that location.

For starter, here is the minimalistic nginx.conf example:

user                  nginx;
worker_processes      1;

error_log             /var/log/nginx/error.log warn;
pid                   /var/run/nginx.pid;

events {
  worker_connections  1024;

http {
  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log        /var/log/nginx/access.log  main;

  sendfile          on;

  keepalive_timeout 65;
  gzip              on;

  server {
    listen          80;
    server_name     localhost;
    location / {
      add_header Content-Type text/html;
      return 200 '<html><body>Hello World</body></html>';