【问题标题】:NGINX rewrite IP to Domain NameNGINX 将 IP 重写为域名
【发布时间】:2016-09-14 11:47:16
【问题描述】:

服务器设置:

  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 流量定向到域名?到目前为止,我已经尝试将 sn-p 放在各种 NGINX 配置文件中,但均未成功。

谢谢。

【问题讨论】:

    标签: apache nginx vps centos7 plesk


    【解决方案1】:

    您可以尝试将其添加到 Nginx 的 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;
    }
    

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

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

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

    【讨论】:

    • 感谢您的帮助。
    【解决方案2】:

    最简单的方法是在Tools & Settings > IP addreses > xx.xx.xx.xx 中将IP xx.xx.xx.xxdefault domain 设置为domain.com

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

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

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

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

    【讨论】:

    • 谢谢。我很想使用 .htaccess,但是当使用 NGINX 作为 Apache 的反向代理时,这些规则似乎并不适用。以下是关于我当前设置的一些附加信息:PHP 处理程序当前由 NGINX 设置为 PHP-FPM 应用程序。我们还尝试了 Apache 的 PHP-FPM 应用程序,但根本没有看到行为变化。您是说通过如上所述设置 IP 的默认域将允许我们使用 .htaccess 代替 NGINX 指令?
    • 当您为 IP 设置“默认域”时,您不需要任何额外的规则/操作,因为 Plesk 将此 IP 作为“listen xx.xx.xx.xx”添加到您的域配置中。因此,您的域也可以直接通过 IP 访问。
    • 谢谢。有关我们需要做的其他示例,请参见上文。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2017-11-13
    • 2011-01-30
    相关资源
    最近更新 更多