NGINX 将 IP 重写为域名

NGINX rewrite IP to Domain Name

服务器设置:

  1. VPS
  2. Plesk 12.5
  3. Centos 7
  4. NGINX 作为 Apache 2.4 的反向代理
  5. NGINX 配置路径:/etc/nginx/nginx.conf

Plesk 提供了一个 GUI Apache 和 nginx 设置对话框,但无法从那里接受服务器{}块。

我尝试了以下方法及其多种变体,但均未成功:

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

这是我们正在尝试做的事情的另一个例子,我们需要知道将代码放在哪里,以便 NGINX 读取并遵守执行指令。

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;
}

在这种情况下,我需要在哪里放置这个或类似的定向以将所有直接 IP 流量定向到域名?到目前为止,我已经尝试将代码片段放入各种 NGINX 配置文件中,但没有成功。

谢谢。

最简单的方法是在 Tools & Settings > IP addreses > xx.xx.xx.xx

中将 IP xx.xx.xx.xxdefault domain 设置为 domain.com

您还可以在 domain.com 的网站根目录中创建 .htaccess 文件,内容为:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^xx\.xx\.xx\.xx
RewriteRule (.*) http://domain.com/ [R=302,L]

为什么它不能通过附加指令工作?

  • plesk 在 nginx 域的服务器内包含自定义指令{} - 所以服务器内的服务器是不可能的。这是设计使然。
  • 自定义指令包括在 nginx 域服务器的末尾{}因此,如果请求被某些上层规则或位置捕获,则此请求将忽略所有其他规则。

您可以尝试将其添加到 UI 中 Nginx 的 "additional directives":

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;
}

我忽略了所有系统范围和注释设置。您也可以尝试从

添加内容
include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;

请注意,您的网站根目录位于 /httpdocs 文件夹中,根据此配置,我看到您的网站根目录位于 public 目录中。