AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 35955
Accepted
Jauder Ho
Jauder Ho
Asked: 2009-07-05 17:31:57 +0800 CST2009-07-05 17:31:57 +0800 CST 2009-07-05 17:31:57 +0800 CST

如何让 nginx 从 www 重定向到非 www 域?

  • 772

假设我想从 www.example.com 重定向到 example.com,并且我想使用 nginx 来做到这一点。我环顾四周,没有看到任何好的文档,所以我想我会问并回答我自己的问题。

website web-server web-hosting web nginx
  • 5 5 个回答
  • 11121 Views

5 个回答

  • Voted
  1. Daemon
    2012-02-11T08:37:52+08:002012-02-11T08:37:52+08:00

    我还在 Nginx wiki 和其他博客上查看了这一点,性能方面的最佳方法是执行以下操作:

    使用 nginx(撰写本文时版本 1.0.12)从 www.example.com 重定向到 example.com。

    server {
      server_name www.example.com;
      rewrite ^ $scheme://example.com$request_uri permanent; 
      # permanent sends a 301 redirect whereas redirect sends a 302 temporary redirect
      # $scheme uses http or https accordingly
    }
    
    server {
      server_name example.com;
      # the rest of your config goes here
    }
    

    当请求来到 example.com 时,不使用 if 语句来提高性能。它使用 $request_uri 而不是必须创建一个 $1 匹配来对重写征税(请参阅 Nginx Common Pitfalls 页面)。

    资料来源:

    • https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#server-name-if
    • http://wiki.nginx.org/NginxHttpCoreModule#.24scheme
    • 7
  2. Ali
    2013-08-06T02:32:40+08:002013-08-06T02:32:40+08:00

    请在 SO 中访问此问题:https ://stackoverflow.com/a/11733363/846634

    从更好的答案:

    其实你甚至不需要重写。

    server {
        #listen 80 is default
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
    
    server {
        #listen 80 is default
        server_name example.com;
        ## here goes the rest of your conf...
    }
    

    因为我的回答是得到越来越多的赞成票,但以上也是如此。你不应该rewrite在这种情况下使用 a 。为什么?因为 nginx 必须处理并开始搜索。如果您使用return(应该在任何 nginx 版本中都可用),它会直接停止执行。这在任何情况下都是首选。

    • 5
  3. Best Answer
    Jauder Ho
    2009-07-05T17:35:30+08:002009-07-05T17:35:30+08:00

    经过一番挖掘和一些失误,这里是解决方案。我遇到的问题是确保使用“ http://example.com $uri”。在 $uri 前面插入 / 会导致重定向到http://example.com//

      server {
        listen 80;
        server_name www.example.com;
        rewrite ^ http://example.com$uri permanent;
      }
    
      # the server directive is nginx's virtual host directive.
      server {
        # port to listen on. Can also be set to an IP:PORT
        listen 80;
    
        # Set the charset
        charset utf-8;
    
        # Set the max size for file uploads to 10Mb
        client_max_body_size 10M;
    
        # sets the domain[s] that this vhost server requests for
        server_name example.com;
    
        # doc root
        root /var/www/example.com;
    
        # vhost specific access log
        access_log  /var/log/nginx_access.log  main;
    
    
        # set vary to off to avoid duplicate headers
        gzip off;
        gzip_vary off;
    
    
        # Set image format types to expire in a very long time
        location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
            access_log off;
            expires max;
        }
    
        # Set css and js to expire in a very long time
        location ~* ^.+\.(css|js)$ {
            access_log off;
            expires max;
        }
    
        # Catchall for everything else
        location / {
          root /var/www/example.com;
          access_log off;
    
          index index.html;
          expires 1d;
    
          if (-f $request_filename) {
            break;
          }
        }
      }
    
    • 4
  4. the_guv
    2009-07-10T10:23:04+08:002009-07-10T10:23:04+08:00

    要重定向到非 www,请修改 vhost 文件:

    server {
      listen 80;
      server_name www.example.com;
      rewrite ^/(.*) http://example.com/$1 permanent;
    }
    

    “永久”将重定向转换为 301 重定向。在此代码块之后,您可以在没有 www 的情况下配置域。

    用于将非 www 重定向到 www:

    server {
      listen 80;
      server_name example.com;
      rewrite ^/(.*) http://www.example.com/$1 permanent;
    }
    

    萨西特。

    顺便说一句,要使用 Nginx 进行完整的 VPS 设置,请查看我的网站 guvnr.com 上的 VPS 圣经,我希望这很方便!

    • 1
  5. Neel
    2014-10-15T13:45:53+08:002014-10-15T13:45:53+08:00

    这就是我使用的:

    # ---------------------------------------
    # vHost www.example.com
    # ---------------------------------------
    
    server {
    
    ##
    # Redirect www.domain.tld
    ##
    
        server_name  www.example.com;
        rewrite ^(.*) http://example.com$1 permanent;
    
    }
    
    # ---------------------------------------
    # vHost example.com
    # ---------------------------------------
    
    server {
    
       # Something Something
    }
    
    • 0

相关问题

  • 虚拟主机建议 [关闭]

  • 谁能推荐一个网站监控服务?[关闭]

  • 阿帕奇的替代品

  • 上线的第一天:如何不杀死您的网站

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve