【问题标题】:How to redirect non www to www in nginx in multi domain environment如何在多域环境中将非 www 重定向到 nginx 中的 www
【发布时间】:2017-04-19 14:20:23
【问题描述】:

我在服务提供商处注册了多个域,并使用 nginx 在 linode 上托管。

域名示例是www.example.com(不是example.com)

所有域都可以在 www 下正常工作,但如果没有它,页面将无法加载。

In curl -I http://example.com 

结果

curl: (6) Could not resolve host http://example.com

我分别有 3 条 A/AAAA 记录

blank    IP
mail     IP
www      IP

在我的/etc/nginx/sites-available

server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com; 
        access_log /var/log/nginx/example.com.log;
        error_log /var/log/nginx/example.com-error error;

        root /var/www/examplecom/public;
        index index.html index.php;

        location ~ \.php?$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php-main.socket;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            #Prevent version info leakage
            fastcgi_hide_header X-Powered-By;
        }
}

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

我也尝试过在顶部写第二个服务器块,但结果相同。

请立即提供指导/帮助。

【问题讨论】:

  • Could not resolve host — 表示您应该正确配置 DNS
  • DNS 可能需要一些时间来传播。假设您声明为“空白”的 A 记录实际上是空白的(根级别的“example.com”而不是“空白.example.com”),您的 DNS 配置看起来是正确的。

标签: nginx web-hosting linode


【解决方案1】:

谢谢大家的回复。

顺便说一句,当我将 linode 设置中的域名从 www.example.com 更改为 example.com 时,问题得到了解决

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多