FastHttpd - Alternate nginx

mojatter

mojatter

Posted on August 23, 2022

FastHttpd - Alternate nginx

Introduction

I was looking for a reverse proxy to replace nginx and found valyala/fasthttp.

I had just developed jarxorg/tree, so I started developing fasthttpd/fasthttpd, thinking that if I combined the two, I could create a product that would meet my needs.

Features

  • Serve static files
  • Simple routing
  • Access logging
  • Reverse proxy
  • Customize headers
  • Support TLS
  • Virtual hosts
  • YAML configuration

Getting started

Specify the configuration file.

fasthttpd -f path/to/config.yaml
Enter fullscreen mode Exit fullscreen mode

Use build-in configuration and some customization.

fasthttpd -e root=./public -e listen=:8080
Enter fullscreen mode Exit fullscreen mode

Installation

Using homebrew.

brew tap fasthttpd/fasthttpd
brew install fasthttpd
Enter fullscreen mode Exit fullscreen mode

In addition, binaries, deb, rpm and docker image are available.

Refer to https://github.com/fasthttpd/fasthttpd#installation

Alternate nginx example

The following is nginx.conf.

upstream backend {
    server 127.0.0.1:8000;
}

server {
    listen 80 default_server;
    return 302 https://$host$request_uri;
}

server {
    listen 443 default_server;
    ssl on;
    ssl_protocols      TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate  /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location ~ ^/(404|50.).html {
        root /usr/share/nginx/html;
        internal;
    }
    location ~ ^/(.*)\.(ico|css|git|jpg|jpeg|png|js)$ {
        alias /app/public/$1.$2;
    }
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_intercept_errors on;
        proxy_pass http://backend;
    }
}
Enter fullscreen mode Exit fullscreen mode

The following is a configuration of fasthttpd.

host: example.com
listen: ':80'

routes:
  - path: ^/(.*)$
    match: regexp
    rewrite: https://example.com/$1
    status: 302
---
host: example.com
listen: ':443'
root: /usr/share/fasthttpd/html
ssl:
  autoCert: true

errorPages:
  '404': /err/404.html
  '5xx': /err/5xx.html

handlers:
  'backend':
    type: proxy
    url: http://localhost:8080/
  'public':
    type: fs
    root: /app/public
    indexNames: [index.html]

routes:
  - path: ^/(.*)\.(ico|css|git|jpg|jpeg|png|js)$
    match: regexp
    handler: public
  - path: /
    handler: backend
Enter fullscreen mode Exit fullscreen mode

I would be happy to get your feedback, Thank you!

πŸ’– πŸ’ͺ πŸ™… 🚩
mojatter
mojatter

Posted on August 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related