Welcome to Centmin Mod Community
Become a Member

Nginx How to configure IP to display my website just like domain does?

Discussion in 'Install & Upgrades or Pre-Install Questions' started by iamhappy, Jul 5, 2018.

  1. iamhappy

    iamhappy New Member

    3
    1
    3
    Jul 5, 2018
    Ratings:
    +1
    Local Time:
    4:36 AM
    • CentOS Version: CentOS 7 64bit
    • Centmin Mod Version Installed: 123.09beta01
    • Nginx Version Installed: 1.15.1
    • PHP Version Installed: 5.6.36
    • MariaDB MySQL Version Installed: 10.1.34
    hello, i just bought a $5 droplet VPS in digitalocean. and installed centminmod and everthing on default setup. please help, when i access my website using ip address, it displays like this : [​IMG]


    and when i access using domain name, it displays normal with wordpress installed.

    [​IMG]

    and my question is, how to make the ip address show my website but not redirect?
    because I will only install 1 website on the server. and i will never create sub domains or add on domains. and I've seen someone success to do it with centminmod installed. thanks you so much :)
     
  2. eva2000

    eva2000 Administrator Staff Member

    35,099
    7,748
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,948
    Local Time:
    6:36 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    You can't as Centmin Mod uses IP and main hostname nginx vhost which is what you see when you visit IP address for other important stuff/stats PHP Opcode and Memcached statistics pages

    Only way really is if you had 2 public IP addresses, one for main hostname nginx vhost which is your main server IP. And have 2nd public IP address assigned to your site's nginx vhost config file. You can setup SECOND_IP persistent variable mentioned further below so that by default any new centmin.sh menu option 2, 22 or nv command created nginx vhost site uses the defined SECOND_IP 2nd public IP address.

    Adding additional IPs to a server involves 2 main tasks
    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
    
     
..