【问题标题】:Rewrite - forward server IP to domain url nginx重写 - 将服务器 IP 转发到域 url nginx
【发布时间】:2012-04-29 03:57:11
【问题描述】:

我想将我的服务器的 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 配置文件中的服务器指令:

rewrite ^ $scheme://mydomain.com$request_uri redirect;

但我的浏览器中出现太多重定向错误。

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

if ($host !~* ^www\.) {
    rewrite ^(.*)$ http://www.$host$1 permanent;
   }

然而,这被列为 nginx 网站上的陷阱之一:(

非常感谢任何关于我可能做错的见解!

谢谢!

【问题讨论】:

    标签: nginx ip forwarding unicorn


    【解决方案1】:

    实际上上面的答案不起作用(至少对我来说)。我想自从提出问题以来,提问者已经找到了他的解决方案,但是由于我在谷歌上搜索并且这里有很多链接,也许这个解决方案会帮助其他人,只需将其添加到您的 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;
    }
    

    【讨论】:

      【解决方案2】:

      也可以使用一个服务器块从多个 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),可以使用此策略解决。

      【讨论】:

        【解决方案3】:

        将一个新的服务器实例添加到我的域 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 搜索结果中,这可能会导致重复内容出现问题。如果这没有发生,我就不会费心添加重写。您可以通过搜索您的 IP 地址来检查您的服务器 IP 是否在搜索结果中:

        网站:xxx.xxx.xxx.xxx

        【讨论】:

          【解决方案4】:

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

          # Say this is your default BIG block
          
          server {
            listen 80 default_server;
            server_name example.com;
          
            passenger_enabled on;
            rails_env production;
            root /var/www/your_app/current/public/;
          }
          
          # Requests made to WWW and directly to the IP address should be
          # forwarded to the BIG block
          
          server {
            listen 80;
            server_name www.example.com 55.55.55.55;
          
            return 301 http://example.com$request_uri;
          }
          

          【讨论】:

          • 可以在一个服务器块中列出多个server_name 条目。您的最后两个块可以压缩为一个。
          • 谁能帮我按照 crftr 的建议编辑我的解决方案? =)
          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2012-05-27
            • 2013-03-12
            • 1970-01-01
            • 1970-01-01
            • 2021-06-29
            • 2021-07-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多