Apache NGINX将IP重写为域名

Apache NGINX将IP重写为域名,apache,nginx,vps,centos7,plesk,Apache,Nginx,Vps,Centos7,Plesk,服务器设置: 副总裁 普莱斯克12.5 Centos 7 NGINX作为Apache 2.4的反向代理 NGINX config的路径:/etc/NGINX/NGINX.conf Plesk提供了一个GUI Apache和nginx设置对话框,但无法从中接受服务器{}块 我尝试了以下方法,以及其中的几种变体,但都没有成功: server { server_name xx.xx.xx.xx; return 301 https://domain.com$request_uri } 下面是

服务器设置:

  • 副总裁
  • 普莱斯克12.5
  • Centos 7
  • NGINX作为Apache 2.4的反向代理
  • NGINX config的路径:/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
    中将IP
    xx.xx.xx
    默认域设置为
    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域的服务器{}内包含自定义指令-因此服务器内的服务器不可能。这是设计的
    • 自定义指令includinö位于nginx域的服务器{}的末尾,因此,如果请求被某个上级规则或位置捕获,则此请求将忽略所有其他指令
    您可以尝试将其添加到用户界面中的Nginx“附加指令”中:

    我已忽略所有系统范围和注释设置。您还可以尝试从中添加内容

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

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

    谢谢。我很想使用.htaccess,但当使用NGINX作为Apache的反向代理时,这些规则似乎并不适用。以下是关于我当前设置的一些附加信息:NGINX当前将PHP hander设置为PHP-FPM应用程序。我们还尝试了Apache的PHP-FPM应用程序,但根本没有看到行为改变。您的意思是,通过如上所述为IP设置默认域,我们可以使用.htaccess来代替NGINX指令吗?当您为IP设置“默认域”时,您不需要任何其他规则/操作,因为Plesk将此IP作为“侦听xx.xx.xx.xx”添加到域的配置中。所以你的域名也可以通过IP直接访问。谢谢。关于我们需要做什么的其他示例,请参见上文。谢谢,谢谢你的帮助。
    include /usr/local/nginx/conf/staticfiles.conf;
    include /usr/local/nginx/conf/php.conf;
    include /usr/local/nginx/conf/drop.conf;