shellnginxnatadsl

How to map my private ip which change dynamically onto my vps_ip?


I have create a droplet in digitalocean,there is a vps_ip i can use.
In my home the way connected to the internet is: route+modem+adsl.
I built a wordpress on the local pc on my home.
The net status is as below when to connect to the web.

WAN:
MAC:ommitted for privacy
IP :public_ip PPPoE
subnet mask:255.255.255.255
gateway:153.0.68.1
DNS:114.114.114.114 223.5.5.5

LAN
MAC:ommitted for privacy
IP :192.168.1.1
subnet mask:255.255.255.0
DHCP:active

ifconfig
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0                                                                    

My goal: let the public access my wordpress site on the home pc with vps_ip digitalocean gave me.

Thank to CrypticDesigns .
https://www.digitalocean.com/community/questions/how-to-map-my-local-ip-192-168-1-100-with-my-vps_ip?
I have solved the problem with the help of CrypticDesigns.

In my local network:
On my router portforward port 80 and private ip 192.168.1.100 to the outside of your network.

In public droplet system:

sudo apt-get install nginx
sudo nano /etc/nginx/sites-available/default
server {
    listen *:80;
    server_name vps_ip;
    rewrite .* http://publlic_ip$request_uri permanent; 
}
sudo service nginx restart

Anyone who go to the vpsip can browse my wordpress now.
It is important that my ip address on the wan changes about every 30 minutes.How about 30 minutes later?
The publicip will change,the configurration file /etc/nginx/sites-available/default can't work .
I want to make improvements on the problem.
It is my opinion to make the task done that :
1.in my home pc
The command curl ipinfo.io/ip can get my public ip.
Write it into crontab for every 30 minutes.
2.send the vpsip and change the value of publicip in /etc/nginx/sites-available/default
,and restart nginx.

How to express the two steps with shell command to make the process automatic?


Solution

  • There are a lot of ways to face this. For me, this is the simplest one without having to install extra software or subscribing to dynamic dns sites.

    I don't know if it's a temporal problem but ipinfo.io didn't work for me, so I use another service in the solution. Change it if desired.

    First, in your local PC, write the IP you have in the remote /etc/nginx/sites-available/default (the one you called publlic_ip) to /tmp/oldIP. Just the IP, like:

    20.20.20.20
    

    This is needed to be done just once. Then, save the following script wherever you want, provide it execution permissions and add it to cron:

    #!/bin/bash
    
    VPS_IP= #fill this 
    VPS_USER= #fill this
    MyOldIP=$(cat /tmp/oldIP)
    MyIP=$(curl http://bot.whatismyipaddress.com)
    
    if [ $MyOldIP != $MyIP ] ; then
      ssh $VPS_USER@$VPS_IP "sudo sed -i 's/$MyOldIP/$MyIP/' /etc/nginx/sites-available/default" \
      && ssh $VPS_USER@$VPS_IP sudo service nginx restart
    fi