My first website is still online

I started programming with Pascal in 8th grade. Pascal was a very good starting point back then. I quickly learned the basics of programming and created my first game. It was a snake game running in console like we had in Nokia phones. Unfortunately I could not find the source code or any screenshot of my game 🙁

Snake game in Nokia 331

My game development adventure ended when I met with web development in 1999. As I remember those were the days that Perl was dominating the web application domain. PHP wasn’t running on Zend engine and Javascript was just 4 years old.

Internet access was very limited in Turkey and it was almost impossible to find Turkish resources about programming. That’s why I wanted to share my little knowledge about Perl with my Turkish fellows. But there was a problem. I didn’t have a credit card to buy a domain name or pay for the hosting. I found a free hosting service that gave a small disk space and created my first website (erdemkose.azit.com) titled “CGI with Perl” in 1999. It is still online thanks to Internet Archive. I would like to thank to them for providing this excellent service.

My first website

I remember checking my visitor counter at the bottom of the page every day. That was the only free way to measure visitor engagement. It was 6 years before Google launched Analytics.

Cloudflare Registrar

Do you remember the times that Cloudflare was just an easy to set-up CDN? With the launch of Cloudflare Registrar, only missing feature in Cloudflare is hosting.

With the new service, you can register the domain name you already added to Cloudflare. Cloudflare promises that they will only charge for the registry cost, nothing else. This means a domain name could cost only a couple of dollars.

You need to login to your Cloudflare account and add as many domains as you can. After that, you must claim your place in line.

Cloudflare Registrar
I am in Wave 3 and waiting for my line 🙂

Nginx can help you resize your images

Nginx is getting more and more market share every day. However, nginx is more than just a web server. Here you can find how to use it as an image server.

How often do you need to generate different sizes of images on your site or application? I need it almost every time. Unfortunately image processing in PHP is a time and memory consuming operation. So what are our options?

  • Develop custom image resizer in PHP
  • Integrate 3rd party PHP library (e.g: Intervention Image)
  • Deploy image manipulation app (e.g: Glide)
  • Use an external service (e.g: Cloudinary)
  • Use nginx on your own server

Configure nginx as an image server

Nginx has the ability to manipulate images with the ngx_http_image_filter_module. If you do not have this module enabled on your host, you should build from the source with the --with-http_image_filter_module configuration parameter.

Example configuration

server {
    server_name localhost;
    listen 80;

    location ~ "^/image-resizer/(?<width>\d+)/(?<path>.+)$" {
        alias /var/www/images/$path;

        #resize image without changing the ratio
        image_filter resize $width -;

        access_log off;
        expires 30d;
        add_header Cache-Control "public";
    }
}

Caching generated images

If you use the above configuration, your images will be cached on the client side but the requested image will be regenerated for every request. This is not something you would want to use your precious CPU time. To avoid regeneration, you can make your server to cache the results.

Sequence Diagram of Nginx as an image server (with cache)
Sequence Diagram of Nginx as an image server (with cache)

Nginx has built-in caching mechanism but to use this cache, we need to seperate web and image server. Web server redirects the request to local image processing server and caches the result. 

Example configuration for web and image server (with cache)

In this scenario, you must create two server instances. First instance will serve the usual web content.

# Web server
server {
  listen 80;
  server_name localhost;

  root /var/www;

  location ~ "^/image-server/(100|250|500|1000)/(.+)\.(jpg|png)$" {
    # Pass the request to the local image processing server
    proxy_pass http://localhost:8888;

    # Cache the results
    proxy_cache processed-images;

    # Keep file in cache for 30 days
    proxy_cache_valid 200 720h;

    # Add cache status header (HIT, MISS, BYPASS, etc.)
    add_header X-Image-Cache $upstream_cache_status;

    # Let the client to cache the processed image
    expires 30d;
    add_header Cache-Control "public";
  }
}

Second instance will only serve the processed images. This instance will handle only local requests so there is no need to open a new port.

# Local image processing server.
server {
  # listen on a different port
  listen 8888;
  server_name localhost;

  # This server is not public, so block other requests
  allow 127.0.0.1;
  deny all;

  # Get the parameters from the request
  # $width: Resize image to this width
  # $path: Original image file path
  location ~ "^/image-server/(?<width>\d+)/(?<path>.+)$" {
    # Use the original image
    alias /var/www/$path;

    # Resize image (ratio is preserved)
    image_filter resize $width -;
  }
}

Finally, to cache the results, we should define a new proxy cache in http block. You can use the example configuration below.

proxy_cache_path /tmp/processed-images/ levels=1:2 keys_zone=processed-images:10m inactive=240h max_size=1024m;

Test the new configuration

To test the new configuration make the following request.

GET /image-server/1000/assets/images/image-0.jpg

You should get an image scaled to 1000px.

Response from Nginx
Response from server

Moving to Berlin and finding a new job

I’m moving to Berlin this month and I see this as a good opportunity to make new career choices. Berlin is a great city for a software developer. There are many international companies and lots of start-ups. If you need to find a job in Berlin, LinkedIn and Indeed are great sites to look for job listings. Also you can use Glassdoor to learn more about the companies and salaries.

Istanbul to Berlin
Istanbul to Berlin