重写-将服务器IP转发到域url nginx

重写-将服务器IP转发到域url nginx,nginx,ip,forwarding,unicorn,Nginx,Ip,Forwarding,Unicorn,我想有我的服务器的IP被重写为域名url,但我有最困难的时间弄清楚如何做到这一点 例如,当我在浏览器中输入213.34.54.xxx时,我希望它被重写为mydomain.com,而不显示IP地址 我目前的配置如下: /etc/nginx/nginx.conf user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_conne

我想有我的服务器的IP被重写为域名url,但我有最困难的时间弄清楚如何做到这一点

例如,当我在浏览器中输入213.34.54.xxx时,我希望它被重写为mydomain.com,而不显示IP地址

我目前的配置如下:

/etc/nginx/nginx.conf

user www-data;
worker_processes 2;
error_log  /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
  worker_connections 1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  access_log /var/log/nginx/access.log;
  server_names_hash_bucket_size 64;
  sendfile        on;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/mydomain;
}
/etc/nginx/sites enabled/mydomain

upstream unicorn {
    server unix:/tmp/unicorn.mydomain.sock fail_timeout=0;
   }

    server {
      listen 80 default;
      server_name localhost;
      root /home/deployer/apps/mydomain/current/public;

      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }

      try_files $uri/index.html $uri @unicorn;
      location  / {
        proxy_set_header Host $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unicorn;
      } 

      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
     }
我已尝试将其添加到mydomain配置文件中的server指令:

rewrite ^ $scheme://mydomain.com$request_uri redirect;
但是我的浏览器中出现了太多重定向错误

至少,我可以通过在服务器指令中使用以下命令来防止显示IP地址:

if ($host !~* ^www\.) {
    rewrite ^(.*)$ http://www.$host$1 permanent;
   }
然而,这被列为nginx站点的陷阱之一:(

任何关于我可能做错什么的见解都将不胜感激


谢谢!

在Nginx配置文件中,添加以下服务器块

server {
listen 80 default;
rewrite ^ http://your_domain_name.com$request_uri permanent;
}

事实上,上面的答案不起作用(至少对我来说)。我想既然提出了这个问题,提问者已经找到了他的解决方案,但是因为我在谷歌上搜索了很多链接,可能这个解决方案会帮助其他人,只需将此添加到您的conf文件中:

server {
        server_name 162.13.171.178;
        # you can add other server_name if you need other IPs
        # or domain names such as without the www

        add_header X-Frame-Options "SAMEORIGIN";

        return 301 $scheme://www.example.com$request_uri;
}

将新服务器实例添加到我的域vhost文件的底部,如下所示:

server {
listen xxx.xxx.xxx.xxx:80;
server_name xxx.xxx.xxx.xxx;
rewrite ^ http://example.com$request_uri? permanent;
}
/etc/init.d/nginx重新加载

注意:我添加此重写的唯一原因是,我的一台服务器的IP地址以某种方式出现在Google搜索结果中,这可能会导致重复内容的问题。如果没有发生这种情况,我就不会费心添加重写。您可以通过运行y搜索来检查您的服务器IP是否在搜索结果中我们的IP地址:


站点:xxx.xxx.xxx.xxx

您需要为重写规则编写一个服务器块。一个块可以容纳多个
服务器名称
元素


也可以使用一个服务器块从多个IP重定向

server {
  listen 80;
  listen 443;
  server_name 172.20.0.2 172.20.0.3 172.20.0.4;

  return 301 $scheme://www.yourdomain.com$request_uri;
}

碰巧还有一个常规的PCI合规性查找(泄漏的私有IP),可以使用此策略解决。

可以在一个服务器块中列出多个
server\u name
条目。您的最后两个块可以压缩为一个。有谁能帮我按照crftr的建议编辑我的解决方案吗?=)
server {
  listen 80;
  listen 443;
  server_name 172.20.0.2 172.20.0.3 172.20.0.4;

  return 301 $scheme://www.yourdomain.com$request_uri;
}