r0g
By:
r0g

Moved DynDNS Service from Apache to nginx

July 12, 2017 418 views
Nginx Apache DNS Debian

Hi,

i just moved my dyndns service from my apache2 server to my new nginx machine.
While the service was working good on apache it didn´t on nginx.

The Fritzbox connects to my dyndns service via this link

http://dyn.myserver.co/myscript.php?pass=mypassword&meineip=<ipaddr>

Thats the script (myscript.php)

$pwort = 'mypassword';
$port = ':80';

$dyntxt = "my_IP.txt";
$pworttest = $_GET["pass"];
$IP = $_GET["meineip"];

if (file_exists($dyntxt)){if($pworttest==$pwort) { $a = fopen("$dyntxt", "w");
$dynamicip = $_SERVER["REMOTE_ADDR"];
fwrite($a, $IP);
fclose($a); } 
else { $a = fopen("$dyntxt", "r+"); 
$dynamicip = fread($a,filesize($dyntxt));
fclose($a);

$url="http://".$dynamicip."".$port; 
header("Location: $url");} }
?>

And here is my config for dyn.myserver.co on nginx:

server {
    listen 80;
    listen [::]:80;

    root /var/www/dyn;
    index index.php index.html index.htm;

    server_name dyn.myserver.co;

    location / {
        try_files $uri/ /index.php?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/dyn;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

The IP which is written in myIP.txt is copied to the DNS Server by cronjob. That is working.
For example: On my apache server i called the dyndns URL (http://dyn.myserver.co/myscript.php) and was redirected to the IP which was filled in my
IP.txt. On nginx it just shows me the content of myscript.php.

I think it´s a problem with the nginx config. Can anybody help me?

5 Answers

Hi @r0g

Are you using DigitalOcean droplets? Or are you running Nginx on your home server?

Change this line in the Nginx config and restart Nginx service nginx restart:

try_files $uri/ /index.php?$args;

It should be change to this (I'm guessing you're running WordPress too?):

try_files $uri $uri/ /index.php?$args;

If you don't run WordPress or something like that, then change it to this:

try_files $uri $uri/ =404;

Hi @hansen

no i´m running nginx on a dedicated root server. Thank you i´ll try that.

  • Both of them are not working

    try_files $uri $uri/ /index.php?$args;
    
    try_files $uri $uri/ =404;
    

    still the same issue... Any further ideas?

    • @r0g
      Did you remember to restart Nginx? Do you have PHP 5.6 installed and working?
      Make sure you edit the files located here /etc/nginx/sites-enabled/, since they are the active configuration of Nginx. If your configuration is only located in /etc/nginx/sites-available/, then it's not activated and will not be used.

      • @hansen
        yep i firstly checked the config via nginx -t and restarted nginx by service nginx restart

        I created a symlink via ln -s for the configs so i think it´s okay to edit the config at sites-available, isn´t it?

        And yes i´m running wordpress, too.

@r0g
Please run the following to list all site configurations:

ls -ls /etc/nginx/sites-available/
ls -ls /etc/nginx/sites-enabled/

If you're using WordPress too on the same site, then the try_files should be this:

try_files $uri $uri/ /index.php?$args;

@hansen Thats the output:

root@host01:/etc/nginx/sites-available# ls -ls /etc/nginx/sites-available/
total 24
4 -rw-r--r-- 1 root root  650 Jul 13 18:11 dyn
4 -rw-r--r-- 1 root root  700 Jul  8 19:04 site1
4 -rw-r--r-- 1 root root  689 Jul  9 23:38 pattymitzaufen
4 -rw-r--r-- 1 root root 1055 Jul 10 13:39 smarthome
4 -rw-r--r-- 1 root root  653 Jul 10 10:57 ts3wi
4 -rw-r--r-- 1 root root  681 Jul  8 19:04 site2
root@host01:/etc/nginx/sites-available# ls -ls /etc/nginx/sites-enabled/
total 0
0 lrwxrwxrwx 1 root root 30 Jul 10 15:22 dyn -> /etc/nginx/sites-available/dyn
0 lrwxrwxrwx 1 root root 35 Jul  6 15:08 site1 -> /etc/nginx/sites-available/site1
0 lrwxrwxrwx 1 root root 41 Jul  9 23:38 pattymitzaufen -> /etc/nginx/sites-available/pattymitzaufen
0 lrwxrwxrwx 1 root root 36 Jul 10 12:21 smarthome -> /etc/nginx/sites-available/smarthome
0 lrwxrwxrwx 1 root root 32 Jul 10 10:57 ts3wi -> /etc/nginx/sites-available/ts3wi
0 lrwxrwxrwx 1 root root 33 Jul  8 19:04 site2 -> /etc/nginx/sites-available/site2
root@host01:/etc/nginx/sites-available#

Ah ok no wordpress is running on another site not on dyn.

  • @r0g

    Okay, then it makes no sense if all the other sites have the same location ~ \.php$ section, but the dyn configuration doesn't compile the PHP script.
    The dyn config should use try_files $uri $uri/ =404; if it's not a WordPress site.

I already tried try_files $uri $uri/ =404; and its not working. The skript is shown as plain text while calling the URL...

I´m wondering because any of the wordpress sites are working good.

Have another answer? Share your knowledge.