Discover Centmin Mod today
Register Now

Replacing IP Not Working

Discussion in 'System Administration' started by Jon Snow, May 1, 2021.

  1. Jon Snow

    Jon Snow Active Member

    616
    112
    43
    Jun 30, 2017
    Ratings:
    +171
    Local Time:
    1:55 PM
    Nginx 1.13.9
    MariaDB 10.1.31
    I have a Linode VPS with 2 IPs for 2 websites.


    I'm gotten a 3rd IP to replace one of the current IPs because it's been leaked and I use Cloudflare to mask it. Trying to replace the IP isn't working to my expectations.

    I replaced the IP with the new one in custom_config.inc
    Code (Text):
    SECOND_IP=xx.xx.xx.xxx

    And I replaced it for the vhost conf for both ports 80 and 443.

    Code (Text):
    server {
      listen   xx.xx.xx.xxx:80;


    Code (Text):
    server {
      listen xx.xx.xx.xxx:443 ssl http2;

    I've also restarted NGINX.

    Changing the DNS entry in Cloudflare to the IP was also something I did.

    I can see the new IP under Networking in the Linode control panel but when I load the IP in my browser, it doesn't resolve to the default centminmod page. My website also doesn't load.

    Did I do everything properly or is there something I'm missing?
     
  2. eva2000

    eva2000 Administrator Staff Member

    46,612
    10,583
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +16,423
    Local Time:
    2:55 AM
    Nginx 1.19.x
    MariaDB 5.5/10.x
    Adding additional IPs to a server involves 2 main tasks - looks like you're missing the 1st task. But be sure to practice this task on a test VPS first as you can screw up your network config if you don't. Or just ask Linode for help as they have paid professional services too Professional Services - Hire a Linode Consultant. The CentOS 6 method seems to worker better on CentOS 7 for some systems so just FYI.
    1. Adding and registering additional IPs to system network configuration
    2. Configuring Nginx to use those additional IPs

    Additional IPs System Configuration



    For CentOS to add additional IP address, you need to first be assigned additional IP addresses by your web host or order the additional IPs. For instance, OVH calls additional IPs failover IPs.

    OVH has a guide for adding and registering the additional IPs for CentOS and other Linux distributions to the system outlined at Network IP Aliasing | OVH Docs.

    There's a previous discussion for this with tips at Sysadmin - How to add a IPv4 Failover Range to CentOS 7? including scripting the format for each
    /etc/sysconfig/network-scripts/ifcfg-eth0:X file you need to create.

    Example for CentOS 6 & 7



    Example IP range 192.168.2.2 to 192.168.2.10 in SSH or shell script define IPLIST variable listing all additional IP addresses in single space separated list and define i variable with starting value of 0. Then a for ip in $IPLIST loop incrementing i variable by 1 for each IP listed in IPLIST
    Code (Text):
    IPLIST="192.168.2.2 192.168.2.3 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7 192.168.2.8 192.168.2.9 192.168.2.10"
    i=0
    for ip in $IPLIST; do
      i=$(($i+1))
    echo -e "\n/etc/sysconfig/network-scripts/ifcfg-eth0:${i}"; echo "
    DEVICE="eth0:$i"
    BOOTPROTO=static
    IPADDR="$ip"
    NETMASK="255.255.255.255"
    BROADCAST="xxx.xxx.xxx.xxx"
    ONBOOT=yes";
    echo -e "\nifup eth0:${i}";
    done
    

    Gives following display only output
    Code (Text):
    /etc/sysconfig/network-scripts/ifcfg-eth0:2
    
    DEVICE=eth0:2
    BOOTPROTO=static
    IPADDR=192.168.2.2
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:2
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:3
    
    DEVICE=eth0:3
    BOOTPROTO=static
    IPADDR=192.168.2.3
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:3
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:4
    
    DEVICE=eth0:4
    BOOTPROTO=static
    IPADDR=192.168.2.4
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:4
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:5
    
    DEVICE=eth0:5
    BOOTPROTO=static
    IPADDR=192.168.2.5
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:5
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:6
    
    DEVICE=eth0:6
    BOOTPROTO=static
    IPADDR=192.168.2.6
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:6
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:7
    
    DEVICE=eth0:7
    BOOTPROTO=static
    IPADDR=192.168.2.7
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:7
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:8
    
    DEVICE=eth0:8
    BOOTPROTO=static
    IPADDR=192.168.2.8
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:8
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:9
    
    DEVICE=eth0:9
    BOOTPROTO=static
    IPADDR=192.168.2.9
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:9
    
    /etc/sysconfig/network-scripts/ifcfg-eth0:10
    
    DEVICE=eth0:10
    BOOTPROTO=static
    IPADDR=192.168.2.10
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    
    ifup eth0:10

    Where the first entry /etc/sysconfig/network-scripts/ifcfg-eth0:2 would contain
    Code (Text):
    DEVICE=eth0:2
    BOOTPROTO=static
    IPADDR=192.168.2.2
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    and command to start up the interface for initial first time for eth0:2 would be
    Code (Text):
    ifup eth0:2
    


    Example for CentOS 7 Only



    For CentOS 7 there's a 2nd method for adding additional IPs to your system Linux Basics: Assign Multiple IP Addresses To Single Network Interface Card On CentOS 7 | Unixmen. Instead of separate /etc/sysconfig/network-scripts/ifcfg-eth0:X files where X increments, you add additional IPs within the same /etc/sysconfig/network-scripts/ifcfg-eth0 existing file defined by variable IPADDRx where x increments from 1 to number of IP addresses you want to add. Your file might not be named eth0 as it depends on your CentOS 7 network device naming scheme which you can find out via command below to check which network device belongs to your existing public IP address for your server:
    Code (Text):
    ifconfig
    

    So if you existing /etc/sysconfig/network-scripts/ifcfg-eth0 file contain
    Code (Text):
    DEVICE=eth0
    BOOTPROTO=static
    IPADDR=192.168.2.1
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    adding additional example IPs above would add these additional entries
    Code (Text):
    IPADDR1="192.168.2.2"
    IPADDR2="192.168.2.3"
    IPADDR3="192.168.2.4"
    IPADDR4="192.168.2.5"
    IPADDR5="192.168.2.6"
    IPADDR6="192.168.2.7"
    IPADDR7="192.168.2.8"
    IPADDR8="192.168.2.9"
    IPADDR9="192.168.2.10"
    

    so /etc/sysconfig/network-scripts/ifcfg-eth0 file with additional IPs would contain
    Code (Text):
    DEVICE=eth0
    BOOTPROTO=static
    IPADDR0=192.168.2.1
    IPADDR1="192.168.2.2"
    IPADDR2="192.168.2.3"
    IPADDR3="192.168.2.4"
    IPADDR4="192.168.2.5"
    IPADDR5="192.168.2.6"
    IPADDR6="192.168.2.7"
    IPADDR7="192.168.2.8"
    IPADDR8="192.168.2.9"
    IPADDR9="192.168.2.10"
    NETMASK=255.255.255.255
    BROADCAST=xxx.xxx.xxx.xxx
    ONBOOT=yes
    

    again you can script for this IPLIST variable defining space separated list of additional IP addresses in a for ip in $IPLIST loop with starting i=0 variable that increments by a value of 1 for each IPLIST entry
    Code (Text):
    IPLIST="192.168.2.2 192.168.2.3 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7 192.168.2.8 192.168.2.9 192.168.2.10"
    i=0
    for ip in $IPLIST; do
      i=$(($i+1))
      echo "IPADDR${i}=\"$ip\""
    done
    

    changing existing IPADDR line from
    Code (Text):
    IPADDR=192.168.2.1
    

    to
    Code (Text):
    IPADDR0=192.168.2.1
    

    Then restart your networking service
    Code (Text):
    service network restart
    

    check the additional IPs registered on your system via command
    Code (Text):
    ifconfig
    


    Configuring Nginx Additional IP Usage



    To configure Nginx additional IPs, you need to first do the above to add the additional IPs to the system's networking configuration and for the IPs to show up in ifconfig output and be pingable. Nginx can setup Vhosts to use either name based hosting (default) sharing the main IP address or IP based hosting where a Vhost uses a dedicated or different IP address for main IP address. This is controlled by Nginx listen directive How nginx processes a request

    Centmin Mod 123.09beta01 and higher have native support to configure Nginx vhost creation routines to use a separate second IP from the main server IP address via persistent config file /etc/centminmod/custom_config.inc set variable called SECOND_IP discussed and added at Beta Branch - update nginx vhost generator routines support second IP

    Support secondary dedicated IP configuration for centmin mod nginx vhost generator, so out of the box, new nginx vhosts generated will use the defined SECOND_IP=111.222.333.444 where the IP is a secondary IP addressed added to the server.

    You define SECOND_IP variable is centmin mod persistent config file outlined at Upgrade Centmin Mod - CentminMod.com LEMP Nginx web stack for CentOS you manually create the file at /etc/centminmod/custom_config.inc and add SECOND_IP=yoursecondary_IPaddress variable to it which will be registered with nginx vhost generator routine so that any new nginx vhosts created via centmin.sh menu option 2 or /usr/bin/nv or centmin.sh menu option 22, will have pre-defined SECOND_IP ip address set in the nginx vhost's listen directive.

    So in persistent config file you create or already created at /etc/centminmod/custom_config.inc set in it
    Code (Text):
    SECOND_IP=111.222.333.444
    


    So at centmin.sh menu option 2, 22 or nv command runs to create a fresh new Nginx vhost instead of server{} context's listen directive being listed as
    Code (Text):
    listen 80;
    

    with SECOND_IP set it will become
    Code (Text):
    listen 111.222.333.444:80;
    

    for HTTPS on port 443

    instead of
    Code (Text):
    listen 443;
    

    with SECOND_IP set it will become
    Code (Text):
    listen 111.222.333.444:443;
    

    where you defined SECOND_IP=111.222.333.444

    Thus allowing you to use a different server IP for new nginx vhosts generated automatically.

    Now if you want to use a different IP address from main server IP or the one defined in SECOND_IP variable, then right now you would need to manually edit your Nginx vhost config file's listen directive yourself defining the additional IP you added earlier and restart Nginx server afterwards
    Code (Text):
    service nginx restart
    

    or centmin mod command shortcut
    Code (Text):
    ngxrestart
    
     
  3. Jon Snow

    Jon Snow Active Member

    616
    112
    43
    Jun 30, 2017
    Ratings:
    +171
    Local Time:
    1:55 PM
    Nginx 1.13.9
    MariaDB 10.1.31
    This was it. I wasn't aware about that part. Thanks so much.