Search code examples
apachenginxvpscentos7plesk

NGINX rewrite IP to Domain Name


Server Setup:

  1. VPS
  2. Plesk 12.5
  3. Centos 7
  4. NGINX as reverse proxy to Apache 2.4
  5. Path to NGINX config: /etc/nginx/nginx.conf

Plesk provides a GUI Apache & nginx Settings dialog box, but is unable to accept server{} blocks from there.

I've tried the following, and several variations thereof, without success:

server {
  server_name xx.xx.xx.xx;
  return 301 https://domain.com$request_uri 
}

Here's another example of what we are trying to do and need to know where to place the code so NGINX reads and honors the instruction for execution.

server {
server_name newdomain.com www.newdomain.com;

# ngx_pagespeed & ngx_pagespeed handler
#include /usr/local/nginx/conf/pagespeed.conf;
#include /usr/local/nginx/conf/pagespeedhandler.conf;
#include /usr/local/nginx/conf/pagespeedstatslog.conf;

# limit_conn limit_per_ip 16;
# ssi  on;

access_log /home/nginx/domains/newdomain.com/log/access.log combined    buffer=32k;
error_log /home/nginx/domains/newdomain.com/log/error.log;

root /home/nginx/domains/newdomain.com/public;

location / {

# block common exploits, sql injections etc
#include /usr/local/nginx/conf/block.conf;

# Enables directory listings when index file not found
#autoindex  on;

 }

 location /forums {
 try_files  $uri $uri/ /index.php;
}

location ~^(/forums/page/).*(\.php)$ {
    try_files  $uri $uri/ /index.php;
}

# Mask fake admin directory
location ~^/forums/admin/(.*)$ {
    deny     all;
}

# Secure real admin directory
location ~^(/forums/mynewadmin/).*(\.php) {
    #allow         127.0.0.1;
    #deny          all;
    #auth_basic    "Restricted Area";
    #auth_basic_user_file $document_root/forums/mynewadmin/.htpasswd;
 include /usr/local/nginx/conf/php.conf;
}

# IP.Board PHP/CGI Protection
location ~^(/forums/uploads/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/hooks/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/cache/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/screenshots/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/downloads/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/blog/).*(\.php)$ {
    deny     all;
}
location ~^(/forums/public/style_).*(\.php)$ {
    deny     all;
}

include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;
}

Where do I need to place this or similar directing in this scenario to direct all direct IP traffic to the domain name? I've tried placing the snippet in various NGINX config files so far without success.

Thanks.


Solution

  • You can try to add this into Nginx's "additional directives" in UI:

    location /forums {
     try_files  $uri $uri/ /index.php;
    }
    
    location ~^(/forums/page/).*(\.php)$ {
        try_files  $uri $uri/ /index.php;
    }
    
    # Mask fake admin directory
    location ~^/forums/admin/(.*)$ {
        deny     all;
    }
    
    # IP.Board PHP/CGI Protection
    location ~^(/forums/uploads/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/hooks/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/cache/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/screenshots/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/downloads/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/blog/).*(\.php)$ {
        deny     all;
    }
    location ~^(/forums/public/style_).*(\.php)$ {
        deny     all;
    }
    

    I've ignore all system-wide and commented settings. Also you can try to add content from

    include /usr/local/nginx/conf/staticfiles.conf;
    include /usr/local/nginx/conf/php.conf;
    include /usr/local/nginx/conf/drop.conf;
    

    Pay attention that your web site root is placed into /httpdocs folder, according to this config I've see that you web root was in public directory.