Serving localhost web with no permissions

I'm on a bad connection: half of the time it's sluggishly slow, the other half is throwing errors or meddling with certificates. A lot of pages are blacklisted, and you have to fill a form to request a ban lift. It sucks. I had to download some manuals to have them available offline, and this is how I serve them.

The manuals

I needed the following manuals, all of them as static Html pages:

The deploy

I put the files under ~/Docs/Manuals or something like that, and then sync the folder with Syncing , MEGA or whatever you like. You can access those files directly from the browser, but some functionality can be limited. E.g. the Nim search doesn't work because Cors:

CORS error

I remember dealing with this issue before, fixing it somehow in the Firefox settings and then being reverted after an update. Well, if Firefox don't like pages served in that way, lets feed them through a web server.

Serving static pages

You can launch a web server from an unprivileged user as long as you don't use ports below 1024 (like the 80). The simplest command line to fire up nginx is:

$ nginx -c /home/xbello/nginx.conf -e /home/xbello/logs_error.log

Nginx needs the absolute path to the config file and a place to store logs (you don't have access to write logs in the default location). The minimal config file I could get to work is this:

pid /home/xbello/run/nginx.pid;
daemon off;

events {
 worker_connections 16;
}

http {
 client_body_temp_path /home/xbello/tmp/;
 proxy_temp_path /home/xbello/tmp/;
 fastcgi_temp_path /home/xbello/tmp;
 uwsgi_temp_path /home/xbello/tmp;
 scgi_temp_path /home/xbello/tmp;
 access_log off;

 sendfile on;
 include /etc/nginx/mime.types;
 default_type application/octet-stream;
 types_hash_max_size 2048;
 types_hash_bucket_size 128;

 server {
  listen 8088;
  server_name _;

  root /home/xbello/MEGA/Docs/;
 }
}

You'll need to create some paths to store the nginx.pid and all the temp files. Launching nginx with that file should serve them interactively

A daemon to launch it

I'm going to use systemd. It has its haters, feel free to use whatever you want. With systemd you'll need to create a launching scripts at ~/.local/share/systemd/user/, call it manuals.service for example. A minimal example could be:

[Unit]
Description=Show Manuals from Local Dir
After=network.target
After=sockets.target

[Service]
WorkingDirectory=/home/xbello/
ExecStart=nginx -c /home/xbello/etc/nginx.conf -e /home/xbello/logs/error.log
Restart=always
RestartSec=3

[Install]
WantedBy=default.target

Notice that "ExecStart" is the same command line we saw above. It's important to have "daemon=off" in the nginx config file if you plan to use it this way, as systemd doesn't work with daemonized processes.

Now we instruct systemd to reload the config, start it, and it all works fine, enable it to auto-launch at every login:

$ systemctl --user daemon-reload
$ systemctl --user start manuals.service
$ systemctl --user enable manuals.service

If everything went smoothly, you should have your manuals available at http://localhost:8088. Time to bookmark them and forget about it.