【问题标题】:Non-www to www domain using Nginx on Ubuntu 12.04 LTS on Ec2 instance在 Ec2 实例上的 Ubuntu 12.04 LTS 上使用 Nginx 的非 www 到 www 域
【发布时间】:2012-12-27 09:50:22
【问题描述】:

看到http://www.ewanleith.com/blog/900/10-million-hits-a-day-with-wordpress-using-a-15-server这个帖子后,我将服务器从 apache2 更改为 nginx。我不是计算机极客,精明。我按照步骤操作。在那之后,这个网站就完美了,除了一件事:非 www 到 www 的事情。我在网上搜索了如何做到这一点。我尝试了他们所说的 modrewrite 东西,但变得更糟了。目前,它被定向到 www,因为我使用 wordpress 并将其设置为常规设置http://www.pageantly.com。然而,我有静态目录,它是普通的非 www。请查看我在 /etc/nginx/conf.d/ 中的 default.conf 以及上面链接的教程:

server {
server_name pageantly.com www.pageantly.com;
root /var/www/;
listen 8080;
## This should be in your http block and if it is, it's not needed here.
index index.html index.htm index.php;

include conf.d/drop;

 location / {
            # This is cool because no php is touched for static content
try_files $uri $uri/ /index.php?q=$uri&$args;
    }

        location ~ \.php$ {
        fastcgi_buffers 8 256k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/dev/shm/php-fpm-www.sock;
    }

# BEGIN W3TC Page Cache cache
location ~ /wp-content/w3tc/pgcache.*html$ {
add_header Vary "Accept-Encoding, Cookie";
   } 
[...]
}
# END W3TC Page Cache core

}

【问题讨论】:

    标签: nginx amazon-ec2 ubuntu-12.04 no-www


    【解决方案1】:

    理想情况下,每个域(包括子域)都应该有一个单独的 server 块。这样一来,您的配置将如下所示:

    # Following block redirects all traffic coming to pageantly.com to www.pageantly.com
    server {
      server_name pageantly.com;
      listen 8080;
    
      # Send a 301 permanent redirect to any request which comes on this domain
      return 301 http://www.pageantly.com$request_uri;
    }
    
    # Following block handles requests for www.pageantly.com
    server {
      server_name www.pageantly.com;
      listen 8080;
      root /var/www;
      [...] # all your default configuration for the website
    }
    

    实现此目的的另一种不干净且低效的方法是引入if 语句,该语句读取域值并相应地流向重定向流量(在 pageantly.com 的情况下)或处理请求(在 www.com 的情况下)。 pageantly.com),但我建议您避免走那条路。

    希望这会有所帮助。

    【讨论】:

    • 我在其他指南上也看到了这一点。但是我仍然尝试使用它来调整我的 default.conf 但不幸的是它给了我错误 503 并且在命令行中重新启动 nginx 后:[emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] 仍然无法绑定()...而且我对 If 并不特别,仅在 PHP 中(微笑)。
    • ...0.0.0.0:80 failed (98: Address already in use) 表示您的主要 nginx 配置正在尝试将 nginx 与端口 80 绑定,该端口正在被其他进程使用。您可能希望使用sudo fuser -n tcp 80 检查使用端口 80 的进程 PID。此外,您可以使用ps aux | grep <obtained-pid> 检查进程。如果您不打算将 nginx 与端口 80 绑定,请查找其中包含 listen 80; 的服务器块并将该端口更改为其他端口。
    • 运行 fuser -n tcp 80 后它给了我“80/tcp: 14537”。这是什么意思?我在哪里可以编辑那个监听 80 的服务器块,因为我在 default.conf 文件中找不到它?
    • 您可以尝试执行ps aux | grep 14537 并将其输出粘贴到此处吗?
    • 没人 14537 0.0 0.6 269892 3788 ? Sl Dec26 1:03 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,64m 根 21793 0.0 0.1 8104 924 pts/0 S+ 13:59 0:00 grep --color=auto 14537
    【解决方案2】:

    如果您在 AWS 上使用 Route 53;那么你不必做任何这样的事情。在 Route53 本身上,我们可以创建一个别名并进行配置,以便将非 www 重定向到 www。

    【讨论】:

    • 我使用路由 53。*.pageantly.com 指向我的弹性 IP 以及非 www (pageantly.com)。那么您的意思是将“pageantly.com”重定向到别名“www.pageantly.com”而不是使用其弹性IP?
    • 我尝试使用别名将“pageantly.com”的 A 值从弹性 IP 更改为“www.pageantly.com”,但它显示“无法保存记录集,因为:-别名目标包含无效值。”所以无法添加记录。
    • 这是我使用的配置。 example.com [第一个文本框中的空白]。键入 A。别名为 Yes。别名目标为 www.example.com。路由策略很简单。
    • 是的,这就是我所做的。我又试了一次,还是不能添加。
    • 我真的很抱歉,伙计。不知道为什么这会给你带来问题!其他可能的解决方案是 .htaccess 文件;或在您的 web.xml 中创建一个过滤器以实现相同的重定向。
    猜你喜欢
    • 2015-02-13
    • 2011-12-18
    • 2018-03-12
    • 1970-01-01
    • 2020-12-31
    • 2014-05-12
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多