Emacs Lisp: avoiding repetition in writing Nginx web server configurations

Emacs Lisp: avoiding repetition in writing Nginx web server configurations

Imagine you are handling hundreds of domains, and each time you need to write pretty similar web server configuration for each new domain. GNU Emacs is extensible text editor and operating environment that helps you to speed up your editing. Nginx is a web server with pretty complex configuration syntax. Yet directives for each doman may be similar to each other. By adapting these functions to your own needs you may get some speed in preparation of Nginx configuration.

The result of the below functions would be something like this:

  1. First you do: M-x nginx-insert-full-redirect

  2. You will be asked for the domain name

  3. If you wish to use the prefix such as www then enter such, otherwise it would be prepared without it.

  4. There is one or more glitches in these functions, that is why you will need to know what you are doing as you don’t want to mess with your web server configuration.

The result would be something as following:

server {
    listen 80;
    server_name example.com;
    access_log /var/log/nginx/www.example.com;
    error_log /var/log/nginx/www.example.com;
    error_page 404 /error.html;
    rewrite ^ https://$server_name$request_uri? permanent;
    }

server {
    listen 80;
    server_name www.example.com;
    access_log /var/log/nginx/www.www.example.com;
    error_log /var/log/nginx/www.www.example.com;
    error_page 404 /error.html;
    rewrite ^ https://$server_name$request_uri? permanent;
    }

server {
    listen 443 ssl;
    server_name www.example.com;
    access_log /var/log/nginx/www.example.com;
    error_log /var/log/nginx/www.example.com;
    error_page 404 /error.html;
    root /var/www/websites/www.example.com;
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }

}

And here are those few functions:

(defun nginx-ssl-redirect (domain &optional www)
  "Redirect www.example.com from port 80 to SSL port 443"
  (let ((host (if www (concat "www." domain) domain)))
    (format "
server {
        listen 80;
        server_name %s;
        access_log /var/log/nginx/www.%s;
        error_log /var/log/nginx/www.%s;
        error_page 404 /error.html;
        rewrite ^ https://$server_name$request_uri? permanent;
    }
" host host host)))

(defun nginx-ssl (domain &optional www)
  "SSL settings for nginx for website. Optional WWW argument, if true, is added to the domain as prefix"
  (let ((host (if www (concat "www." domain) domain)))
    (format "
server {
    listen 443 ssl;
    server_name %s;
    access_log /var/log/nginx/%s;
    error_log /var/log/nginx/%s;
    error_page 404 /error.html;
    root /var/www/websites/%s;
        ssl_certificate /etc/letsencrypt/live/%s/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/%s/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }

}
" host host host host host host)))

(defun nginx-insert-full-redirect (domain &optional www)
  "Inserts nginx SSL redirect settings from plain domain to https://www.example.com domain"
  (interactive "sDomain: \nsPrefix like \"www\": ")
  (let ((www (if (> (length www) 0) www nil))
    (nginx-1 (nginx-ssl-redirect domain))
    (nginx-2 (if www (nginx-ssl-redirect domain t) ""))
    (nginx-3 (nginx-ssl domain www)))
    (insert nginx-1)
    (insert nginx-2)
    (insert nginx-3)))

The idea is to use Emacs Lisp functions to speed up the editing of Nginx web server configuration snippets. There are many ways to do it. This is one example for your own adaptations.

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: