Linux

  • Nginx
    Linux,  Nginx,  OS Tools

    Nginx tips

    Table of contents


    Redirect rules

    Setting up maintenance page based on file presence

    First of all, determine which configuration file is being used by checking the main nginx configuration file.

    This can be retrieved by looking into nginx service startup arguments. Typically: /etc/nginx/nginx.conf

    Extract from nginx.conf:

    . . .
    include /etc/nginx/sites-enabled/*;
    . . .

    Based on the configuration statement above, we need to look into all files into folder /etc/nginx/sites-enabled

    Sample configuration file:

    ...
    server {
        listen   10.64.4.7:443 ssl;
        client_max_body_size 1024M;
        server_name test.demo test;
        root /opt/customer/test/apps/angular/angular;
    
        if (-f $document_root/themes/components/login/maintenance.html) {
            return 503;
        }
        error_page 503 @maintenance;
    
        expires 168h;
        add_header Content-Security-Policy "frame-ancestors 'self'";
        add_header Cache-Control "no-cache";
        add_header X-Frame-Options SAMEORIGIN;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header Referrer-Policy "same-origin";
        index index.html index.htm;
        ssl_certificate           /etc/nginx/cert/test.crt;
        ssl_certificate_key       /etc/nginx/cert/test.key;
        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols TLSv1.2;
        ssl_ciphers " ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256 ";
        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/ssl/certs/dhparam.pem;
        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log;
        rewrite   ^/$  /webconnect;
    
        location @maintenance {
            rewrite ^(.*)$ /themes/components/login/maintenance.html break;
        }
    ...

    Sections on lines 8 – 11 and 33 – 35 state what must be done whenever a given file on a path is available or not. If positive, nginx will redirect all requests to the @maintenance location (a maintenance page).


    Logging

    Log rotation settings

    Configuration file: /etc/logrotate.d/nginx

    Sample configuration (keeps last 30 days):

    /var/log/nginx/*log {
        daily        <--- rotates on a daily basis
        rotate 30    <--- last 30 logs
        missingok
        notifempty
        compress
        sharedscripts
        postrotate
            /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
        endscript
    }
  • Linux,  OS Tools

    find (Linux OS utility)

    Find, excluding a single directory:

    find . -path ./dir1 -prune -false -o -name "*" 

    Find, excluding a list of directories:

    find . \( -path ./dir1 -o -path ./dir2 \) -prune -false -o -name "*"

    Find files with creation date on a range:

    find -newermt "2017-11-06 17:30:00" ! -newermt "2017-11-06 22:00:00" -ls
  • GNU
    Linux,  OS Tools

    screen (Linux OS utility)

    Open a new (named) screen:

    screen -S session_name

    Lists all existing screen sessions:

    screen -ls

    Reconnects to an existing (detached) screen session:

    screen -r SCREEN_NAME_OR_NUMBER

    Force/reconnect to an Attached screen session:

    screen -rd SCREEN_NAME_OR_NUMBER

    Most useful keyboard shortcuts to manage an open session:

        Ctrl+a d Detach from current screen (without destroying it)
        Ctrl+a c Create a new window (with shell)
        Ctrl+a " List all window
        Ctrl+a 0 Switch to window 0 (by number )
        Ctrl+a A Rename the current window
        Ctrl+a S Split current region horizontally into two regions
        Ctrl+a | Split current region vertically into two regions
        Ctrl+a tab Switch the input focus to the next region
        Ctrl+a Ctrl+a Toggle between the current and previous region
        Ctrl+a Q Close all regions but the current one
        Ctrl+a X Close the current region
        Ctrl+a ESC Enters in copy mode (you can scroll the buffer with up/down pageup/pagedown keys), press ESC to return to the shell
        Ctrl+a [ Enters in copy mode (you can scroll the buffer with up/down pageup/pagedown keys), press ESC to return to the shell
            Once into copy mode:
              Move cursor to the text you want to copy
              Press SPACE to start highlighting
              Move cursor to end of text you want to copy
              Press SPACE to copy to the clipboard and exit from copy mode
              Press Ctrl+a ] to paste the text

    Resizing a screen Tab:

    type Ctrl-a :resize +10 to increase size

    How to unfreeze from accidental pressing of Ctrl-S:

    type Ctrl+q

    Sample ~/.screenrc

    # Turn off the welcome message
    startup_message off
    
    # Disable visual bell
    vbell off
    
    # Set scrollback buffer to 10000
    defscrollback 10000
    
    # Customize the status line
    hardstatus alwayslastline
    hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'