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.