Showing posts with label Cheat sheet. Show all posts
Showing posts with label Cheat sheet. Show all posts

2016-04-17

Redirect all your subdomains to a single https domain with nginx

On my playground server, I run a bunch of services in docker containers.
Most of them uses HTTP but don't natively support HTTPS so I'm using nginx as a front web server / reverse proxy to secure the connection.

I like to access my services using the following notation:

  • service1.example.com
  • service2.example.com

In fact, they all point to the same host and I'm using the DNS A records @ and *

The thing is, if I want to enable HTTPS for all these subdomains, I need as many legit SSL certificates. Another option is to buy a wildcard certificate but it's rather expensive ... for a playground server.

So I wrote this handy nginx config which permanently redirect HTTP requests to one unique HTTPS host:

  • http://service1.example.com => https://example.com/service1/
  • http://service2.example.com/path => https://example.com/service2/path

It also supports

  • http://example.com => https://example.com//


Ideally I'd get rid of the double trailing slash, but it'll work for now. Let me know if you have suggestions, this could certainly be enhanced


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # Match domain.tld or prefix.domain.tld
    server_name ~^(?<domain>[a-z0-9]*\.[a-z0-9]*)$ ~^(?<prefix>[a-z0-9]*)\.(?<domain>[a-z0-9]*\..*)$;

    error_log stderr;

    location / {
        rewrite ^ https://$domain/$prefix$uri permanent;
    }
}

2015-02-23

Realtime bandwidth motinoring in one line

To get traffic RX/TX in Mb/s on Linux

IF=eth0 ;echo "RX\tTX" ; while true ; do A=$(awk "/$IF:/ {print \$2,\$10}" /proc/net/dev) && sleep 8 && B=$(awk "/$IF:/ {print \$2,\$10}" /proc/net/dev) && echo -n "$A $B" |awk '{print ($3 - $1)/2^20,($4 - $2)/2^20}';done

2015-02-21

How to clone machine with failing drive to another machine

A server's main disk is about to fail, you see a lot os I/O error in the /var/log/messages, it's not good.

Let's clone that server before it dies. I've choosen to clone it into a VM but the steps are exactly the same with another physical machine/disk

Create or use any existing linux machine, I'm using a toolbox VM running Ubuntu on a Xen hypervisor. Create a disk about the same size as the failing one and attach it to your toolbox machine.

If the old disk is OK, copy the whole disk using dd over the network (netcat or ssh), there is a dedicated tutorial for that here : http://matfrapp.blogspot.com/2015/01/how-to-clone-disk-using-dd-over-network.html

If the old disk is not good looking and you cannot read everything but the machine still works, here is the procedure :

- Backup the MBR ```dd if=/dev/sda of=/root/mbr bs=514 count=1```
Save the UUID of the partition on a notepad, you will need it:
ls -l /dev/disk/by-uuid

-Copy it on the new host using scp for example and restore it on the new drive
```scp failingmachine:/root/mbr /tmp/oldmbr
dd if=/tmp/oldmbr of=/dev/<freshlyattacheddrive> bs=514 count=1```

Reboot the toolbox machine so the new MBR can be read. Using fdik -l, format the partitions with the same filesystem than before.
```
mkfs.ext3 /dev/xvb2
mkfs.ext4 /dev/xvb3
mkswap /dev/xvdb4
```

Change the UUID of the freshly formated partitions to match the previous UUID
EG.  tune2fs /dev/xvdb3 -U 20e9a9d5-809c-4773-8d33-a0434c712345

Mount the partitions and Launch the copy
```
mount /dev/xvdb3 /mnt/newmachine
rsync -avP --numeric-ids --exclude='/dev' --exclude='/proc' --exclude='/sys' root@failingmachine:/ /mnt/newmachine
```
Remember to manually  create a /dev, /proc, /sys paritition otherwise the system won't boot.
```
mkdir /mnt/newmachine/proc
mkdir /mnt/newmachine/sys
mkdir /mnt/newmachine/dev
```

Delete the /mnt/newmachine/etc/udev/rules.d/70-persistent-net.rules file
edit the /mnt/newmachine/etc/network/interfaces file to match the new NIC names if the configuration if different.

That's it

2015-01-22

How to clone a disk using dd over network

!Warning: This method is hardcore and may not be used on production machines! There is a cleaner procedure at the bottom.

Required tools : dd, nc (or ssh is the link is not trusted) and one of these following tools:

  • If you have a lot of bandwith, I recommand the use of lz4
  • If you have a lot of CPU and less bandwith, go with gzip
  • If you have a extremely tiny bandwith and huge amount of CPU, use lzma but I don't recommand it


First, login on the machine you want to clone.

dd bs=16M if=/dev/vda |lz4c -c |nc 172.30.12.6 12345


Create a machine in your new environment.
Create a disk about the same size of you source disk
Attach it to a third party machine in your new environement.
root@tranfert:~# nc -l 13245|lz4 -d|dd bs=16M of=/dev/vdc


Clean Procedure:
Take a snapshot of a volume, mount this snapshot to a linux machine




Popular posts