【问题标题】:How to redirect www.example.com to example.com in NginX?如何在 NginX 中将 www.example.com 重定向到 example.com?
【发布时间】:2017-09-24 18:17:01
【问题描述】:

当我们打开example.com(不带HTTPS)时,网站加载得很好,但是当我们打开www.example.com(不带HTTPS)时,它会打开“欢迎使用nginX”,但我不需要这个,我们想打开@987654323 @即使网址是www.example.com

注意:我们的网站有“HTTPS”,当我们打开example.com时,它会被重定向到https://example.com

注意:当我们打开 https://www.example.com 时,网站加载良好,但当 URL 为 www.example.comhttps://www.example.com 时,它没有被重定向。

我们在 AWS 使用 Ubuntu。

【问题讨论】:

    标签: redirect nginx


    【解决方案1】:

    您是否为每个选项定义了单独的服务器? 请参阅下面的示例 sn-p。 您的问题看起来与this questionthis question 非常相似,它们都有更多信息,我不会在这里复制粘贴。

    此链接也可能有帮助 creating-nginx-rewrite-rules

    server {
        listen 80;
        listen 443 ssl;
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
    
    server {
        listen 80;
        server_name example.com;
        # here goes the rest of your config file
        # example 
        location / {
    
            rewrite ^/cp/login?$ /cp/login.php last;
            # etc etc...
    
        }
    

    }

    $scheme 是协议(HTTP 或 HTTPS),$request_uri 是包含参数的完整 URI。

    【讨论】:

      【解决方案2】:

      为此,我建议编辑您的 nginx 配置文件(通常在 /etc/nginx/sites-enabled/you-example-config 中),添加返回 301,如 here 所示,类似于:

      server {
          listen 80;
          listen 443;
          server_name www.example.com example.com;
          return 301 https://example.com$request_uri;
          #[...ssl settings go here, certs etc....]
      }
      server {
          listen 443;
          server_name example.com;
          # [...ssl and main settings go here...]
      }
      

      这将导致所有请求返回https://example.com

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-11
        • 2011-07-06
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多