Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx 如何将IP上的请求重定向到域_Nginx - Fatal编程技术网

Nginx 如何将IP上的请求重定向到域

Nginx 如何将IP上的请求重定向到域,nginx,Nginx,偶尔有人试图通过公共ip而不是我们的一个域访问我们的网站(是的,我们有多个国家/地区特定的域.dk、.it、.es等,但我们也有.com作为“通用”) 现在,我想将IP地址上的请求重定向到我们的www.domain.com域。在nginx中,除了直接发送到IP的请求外,我如何在不涉及任何内容的情况下做到这一点 这是我的nginx.conf upstream unicorn { server unix:/tmp/unicorn.mysite.sock fail_timeout=0; } se

偶尔有人试图通过公共ip而不是我们的一个域访问我们的网站(是的,我们有多个国家/地区特定的域.dk、.it、.es等,但我们也有.com作为“通用”)

现在,我想将IP地址上的请求重定向到我们的www.domain.com域。在nginx中,除了直接发送到IP的请求外,我如何在不涉及任何内容的情况下做到这一点

这是我的nginx.conf

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

server {
  listen 80 default deferred;

  root /home/deployer/apps/mysite/current/public;
  proxy_set_header X-Request-Start "t=${msec}";
  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

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

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    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;

}

所以问题是,我如何修改它以重定向,例如
http://123.123.123.123/some_page
http://www.mysite.com/some_page
但保留所有其他类似的
http://www.mysite.dk/some_page
未更改?

您可以使用以下指令设置catchall服务器:

listen 80 default_server;
server_name _;
如果有多个服务器节,可以使用
default\u server
指定在主机名与任何其他
server\u name
项不匹配时使用的配置。使用ip号码适合这种情况。
server\u name行充当空的
服务器名称
条目


如果你的一台服务器有多个域名,这也很有用。

不明白,你能说得更清楚一点吗?@Niels,我在回答中添加了一个解释。这有帮助吗?嗯,我想我需要阅读一下nginx的配置,以了解它是如何工作的。我已经添加了我的nginx配置,因此您可以查看它-也许还有帮助内联。请尝试在
listen
指令中将
default
更改为
default\u server
。然后添加
server\u name在它下面。