2

例如,在我的域下example.com,我想要一个登录页面(点击 时将显示example.com)、一个 WordPress 博客(将位于blog.example.com)和我的 HTML 简历(将位于resume.example.com)。使用 nginx,我想拥有三个单独的 .conf 文件,以便我可以将它们彼此分开。DNS 设置为“resume”和“blog”是example.com.

我在 Linode 上运行它,所以我可以完全控制 DNS 和 Web 服务器。但是,当我尝试将 nginx .conf 条目拆分为一个单独的文件时,当我在 Web 浏览器中点击它时blog.example.com,我会得到一个重定向。blog.example.comwww.example.com

我知道 WordPress 的安装不是问题,因为当所有三个服务器条目都在一个 nginx 文件中时,一切正常。我还尝试为博客(blogs-availableblogs-enabled)创建一个单独的文件夹结构,然后将这些文件夹包含在包含nginx.conf 之前sites-enabled,但这也不起作用。这给我留下了两个问题:

  1. 有没有人对如何让它工作有任何建议?也许我的 DNS 设置不正确。

  2. nginx 关心加载顺序还是模棱两可?如果是后者,那么我会将所有内容保存在一个服务器块中,要么全部启动,要么全部关闭。

更新:

nginx.conf 用于example.com

# Blog (WordPress site - blog.example.com)
server {
  listen <ip-address>;
  server_name blog.example.com;
  root /www/blog.example.com/public_html/current;

  access_log /www/blog.example.com/logs/access.log;
  error_log /www/blog.example.com/logs/error.log;

  location / {
    index index.html index.htm index.php;
    try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  /www/blog.example.com/public_html/current/$fastcgi_script_name;

    if ($uri !~ "^/images/") {
      fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
    }
    fastcgi_index index.php;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
  }
}

# redirect example.com to www.example.com
server {
  listen <ip-address>;
  server_name example.com;
  rewrite ^(.*)$ $scheme://www.example.com$request_uri? permanent;
  break;
}

# static site - www.example.com
server {
  listen <ip-address>;
  server_name www.example.com;
  # static HTML / soon-to-be Sinatra site
  root /www/example.com/public_html/current/public;

  access_log /www/example.com/logs/access.log;
  error_log /www/example.com/logs/error.log;
}

注意:我没有resume.example.com上面的站点,但这应该是微不足道的。此外,上述工作顺序不限,所以我不认为这是重定向问题。

**第二次更新**

example.com凭直觉,我在我的conf 文件中注释掉了以下内容:

server {
    listen <ip-address>:80;
        server_name example.com;
        rewrite ^(.*)$ $scheme://www.example.com$request_uri? permanent;
        break;
    }

它按我的预期工作。我上面的非 www -> www 重定向规则有问题吗?

4

1 回答 1

0

For 2nd question, nginx config is declarative. The server block order doesn't matter. So it's easy to manage each server block in its own .conf file.

UPDATE After seeing the nginx config file

can't find anything wrong with the nginx conf file. Tried it in my devbox and it worked fine. So it could be from dns setup. One possibility is that you setup "blog" as a CNAME record of "www" (a cname record is an alias). e.g.,

blog   IN   CNAME   www

If that's the case, a web browser will send http://blog.example.com to http://www.example.com.

Also note that dns record could be cached inside your browser, or in your isp dns server. Even if you fix it for a while, the wrong record may still be cached. Try

dig blog.example.com

to obtain some information of the dns setup.

于 2013-04-01T22:14:13.550 回答