blog

RSS
[TAGGED: nginx]
  1. How to host a Gemini capsule with Node and Nginx

    Project Gemini is a text-based web protocol, like a mash-up of TLS + Gopher. It's hyped as a "Small Internet" with outer space imagery, where instead of web sites, we have Gemini capsules. Some people love it, some people hate it, but it's there and I think it's kind of cool. Anyway, recently I was playing with it and I realized there aren't good docs on how to get it running with nginx. So here's a quick howto:

    1. Enable the nginx stream module

    Depending on your environment, you may need to install the nginx stream module (eg. sudo apt install libnginx-mod-stream), or it might just need to be enabled. Assuming it's installed, simply add this to the very top of your nginx.conf to enable it (the path may be different in your environment): load_module /usr/lib/nginx/modules/ngx_stream_module.so;

    2. Set up a stream directive in nginx.conf

    This should be in your nginx.conf as a sibling to the http directive (i.e. not within the http directive or sites_available). Basically in your actual nginx.conf, put it underneath the http directive, like this:

    http {
    
        ##
        # Basic Settings
        ##
    
        # ...
        # ... skipping ahead ...
        # ...
    
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
    
    stream {
    
        ##
        # Configure ngx_stream_module for Gemini
        ##
    
        limit_conn_zone               $binary_remote_addr zone=addr:10m;
        limit_conn_log_level          warn;
        limit_conn                    addr 1;
    
        log_format                    basic '$remote_addr $upstream_addr [$time_local] '
                                      '$protocol $status $bytes_sent $bytes_received '
                                      '$session_time';
        access_log /var/log/nginx/gemini-access.log;
        error_log  /var/log/nginx/gemini-error.log;
    
        server {
            listen                    1965;
            proxy_buffer_size         16k;
            proxy_pass                'localhost:9003';  # set your actual port here
        }
    }

    3. Have a Gemini server listening on the local port specified in nginx.conf

    This is really easy to setup using the gemini-server npm package, which is modeled after Express (but really you can use any Gemini server). Here's a simple Node.js server written in TypeScript:

    import { readFileSync } from 'fs';
    import gemini, { Request, Response, status } from 'gemini-server';
    
    const PORT = 9003;
    
    const app = gemini({
      cert: readFileSync('./cert.pem'),
      key: readFileSync('./privkey.pem'),
      titanEnabled: false
    });
    
    app.on('/', (_req: Request, res: Response) => {
      res.file('pages/index.gmi');
    });
    
    // Get the facts.
    app.on('/facts/:file', (_req: Request, res: Response) => {
      try {
        res.file('pages/facts/' + _req.params.file);
      } catch(error) {
        res.error(40 as status, 'File not found.')
      }
    })
    
    app.listen(PORT, () => console.log('Gemini listening on ' + PORT + '...'));

    Note that Gemini requires TLS, so you'll have to use a real cert.pem and privkey.pem, but if you already have these for your HTTPS domain you can reuse them. Otherwise check out this wiki to set up a cert.

    That's it. Have fun!

    Posted 2023-08-12 17:59:59 CST by henriquez. 3 comments
  2. How to Self-Host your own Node.js Website

    Amazon, Microsoft and Google control too much of the Internet. If you're an employed web engineer chances are almost 100% that you're renting web hosting services from one or more of these companies at work. You will own nothing and be happy (at work)! But at home you have choice over where you host your web presence. If you have personal sites or side projects, you may want to consider self-hosting these on your home computer. It's easy, free, and fun. You will learn a lot by being your own sysadmin, and I'll show you how.

    In this guide I'll explain how to deploy your Node.js web app to a web server virtual machine (VM) running on a computer that you control. Our methodology will be configuring the Server VM to a "blank slate" status with network access, and then using Ansible over ssh to completely automate the process of installing and configuring your web project. For this to work you just need the following:

    • An Internet Service Provider that can assign you a Static IP address
    • A router that can do port forwarding
    • A computer that stays awake and connected to the Internet

    If this sounds intriguing, read on and I'll show you how deep the rabbit-hole goes.

    Read More

    Posted 2023-06-07 13:45:44 CST by henriquez. Comments