0

I have an EC2 instance running Nginx on Debian Buster. I added multiple private IPs to the instance (to allow nginx to host multiple websites) using the AWS web console. I then created the script /usr/local/bin/bind_secondary_ips:

#!/bin/bash

interface_mac=$(curl -s http://169.254.169.254/latest/meta-data/mac)
interface_ips=$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/${interface_mac}/local-ipv4s)
ipaddress_arr=($interface_ips)

for i in "${ipaddress_arr[@]:1}"
do             
    ip addr add dev ens5 $i/24
done

To run the script at boot, I created and enabled a systemd service unit named bind-secondary-ips.service as follows:

[Unit]
Description=Bind secondary IP address to network interface
After=default.target
Before=nginx.service

[Service]
ExecStart=/usr/local/bin/bind-secondary-ips
RemainAfterExit=true

[Install]
WantedBy=default.target

I modified the nginx service unit After directive as follows:

After=bind-secondary-ips.service network-online.target remote-fs.target nss-lookup.target

When I restart the instance, the secondary IPs correctly bind to the network interface. However, nginx remains in a dead state. Manually starting nginx after the instance boots works fine.

$ systemctl status nginx
  ● nginx.service - nginx - high performance web server
      Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
      Active: inactive (dead)
        Docs: http://nginx.org/en/docs/

I can't seem to figure out why nginx fails to start at boot time. The logs aren't showing anything useful either.

Jim Walker
  • 321
  • 1
  • 3
  • 10
  • Please check the output of `systemctl list-units --failed` – Michael Hampton Feb 14 '21 at 14:53
  • I did, it just shows that nginx failed. Also, `systemctl status nginx` shows `nginx: [emerg] bind() to :443 failed (99: Cannot assign requested address)`, which I assume means that the secondary ip was not yet bound to the interface when Nginx tried to start. – Jim Walker Feb 14 '21 at 15:44
  • That's odd, the output you show above shows no attempt to even start nginx, failed or otherwise. On second look, `After=default.target` in `bind-secondary-ips.service` is definitely wrong, so I would start by removing that. – Michael Hampton Feb 14 '21 at 15:49
  • My bad, the correct output to your command (`systemctl list-units --failed`): `0 loaded units listed`. The one I posted earlier was the output to something else I was trying. After I removed the `After=default.target`, I now see this output: `nginx: [emerg] bind() to :443 failed (99: Cannot assign requested address)` – Jim Walker Feb 14 '21 at 15:55

0 Answers0