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

标签 nginx

每隔一段时间就会有人试图通过公共(public) 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_pagehttp://www.mysite.com/some_page 但保留所有其他如 http://www.mysite .dk/some_page不变?

最佳答案

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

listen 80 default_server;
server_name _;

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

如果您的其中一台服务器有多个域名,它也很有用。

关于nginx - 如何将 IP 上的请求重定向到域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703753/

相关文章:

nginx - 使用 Istio 在两个 pod 之间进行简单的 http 请求

php - Nginx 无法与 uwsgi 对话(连接过早关闭)

php - 带有 InstagramLive-PHP 的 Nginx RTMP 无法正常工作

nginx - 将 Nginx 服务器配置为 python flask 应用程序

python - 如何理解gunicorn中的workers是如何被消耗的

linux - 全新安装后gitlab无法运行

java - 可以用java编写nginx模块吗?

django - gunicorn:错误(无此类文件)nginx + gunicorn +主管

php - Drupal 8 + Nginx 反向代理作为子目录

css - 可以吗? wordpress + nginx+ https 一页