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

4

7

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?

Tree answered 20/8, 2015 at 10:49 Comment(1)
ping! time for the bounty awards!Candelabrum
L
1

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
Liebknecht answered 29/8, 2015 at 6:19 Comment(0)
S
1

If you google "dyndns" you will find multiple providers that give you a "dynamic domain name" for free. You should sign up for one of those.

There are lots of clients that will keep the dynamic domain name in sync with your dynamic IP-address. The dyndns provider you choose will probably have all information you need about clients suitable for their service. Just run the client on your home computer, and viola, the dynamic doman name will always point to your dynamic IP! Even some home routers can update your dynamic domain name for you, and then you dont even have to run the client.

Then change your nginx configuration to point to your dynamic domain name (not your IP).

Spinneret answered 22/8, 2015 at 22:48 Comment(0)
E
1

I'm using ddclient which on Debian based distros should be already available

apt-get install ddclient

Shouldn't be difficult to configure /etc/ddclient.conf:

protocol=dyndns2
syslog=yes
ssl=yes
use=web, web=checkip.dynu.com/, web-skip='IP Address'
server=api.dynu.com
login=<your email>
password='<your password>'
<your subdomain>.dynu.com

It should support different protocols and dyndns providers, I personally used dynu.com for years, should be free (I'm not sure about new users).

Emalee answered 27/8, 2015 at 13:23 Comment(0)
C
1
  1. I would suggest you use proxy_pass and an upstream, instead of going with rewrite. This will make sure that your website is always available through your VPS provider, without revealing your dynamic IPv4 address, which may change from time to time (thus invalidating the bookmarks your users may make were you to use the redirects through the rewrite directive, as well as invalidating the search index generated by Google/Yandex and such).

    1. As others suggested, you could employ one of those Dynamic DNS providers to make sure to always have a domain name that resolves to your IPv4 address, and then use such domain name from within the nginx configuration. Just some most popular options:

      Note, however, that nginx will only attempt to resolve the domain name once upon startup, so, you'll have to keep restarting it in order to pick up any possible changes.

      Additionally, if your IPv4 address really does change every 30 minutes, not just once every couple of years or so, then however you do this updating (whether or not through Dynamic DNS), this approach will likely result not only in a likely downtime (which you could combat with proxy_cache in nginx), but would also allow your fellow subscribers at your home ISP to impersonate your web-site between the time your IPv4 address has changed, and the time the update has been applied on the VPS, or, also, in case your line or power goes down.

      As such, I would not recommend going with the Dynamic DNS route.

    2. Instead of trying to track your Dynamic IPv4 address, you might instead have some success in obtaining a free IPv6 tunnel and a static IPv6 allocation. In which case, you could instead decide to publish your IPv6 address directly to the world, and only use the VPS to proxy IPv4 clients.

      http://SixXS.net/

    3. Instead of having your VPS connect to your home machine, it would be a much better, reliable and robust approach to instead have your home machine phone back to the VPS instead. You could accomplish such phoning back with either a VPN or an IPsec solution, or even a plain old OpenSSH.

      In this case, you'd simply have, (0), the address of the VPS hardcoded on your home box, and, (1), the private IP address and port number of your home machine hardcoded within nginx on your VPS. This will ensure that not only can noone steal your connection (as it'll be fully encrypted and authenticated), but also that nowhere in any configuration files is your Dynamic IPv4 address will be hardcoded.

      If using ssh, you could either go with the tunnel interface in the newer version, or the plain old port forwarding.


On the VPS:

server {
  listen *:80;
  listen [::]:80;
  server_name vps;
  location / {
      proxy_pass http://127.0.0.1:2280;
  }
}

On the home machine, provided you're running your webapp on port 8080:

sh -xc 'while (true); do ssh -v -N -C -R127.0.0.1:2280:127.0.0.1:8080 -oServerAliveInterval=20 -oServerAliveCountMax=1 user@vps; date; sleep 1; done'

Candelabrum answered 28/8, 2015 at 7:25 Comment(0)
L
1

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
Liebknecht answered 29/8, 2015 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.