nginx 虚拟主机问题

nginx 虚拟主机问题

sites-available我在 centos 上运行 nginx,并使用在符号链接到的目录中为每个域设置单独的文件的方法sites-enabled

两个配置文件几乎相同。

我的第一个虚拟主机工作正常,但现在我添加了第二个虚拟主机,非wwwurl 重定向到example.com,并且wwwurl 不可用。

IE

example2.com -> example.com

www.example2.com -> not available

示例.com

# www to non-www redirect -- duplicate content is BAD:
# https://github.com/h5bp/html5-boilerplate/blob/5370479476dceae7cc3ea105946536d$
# Choose between www and non-www, listen on the *wrong* one and redirect to
# the right one -- http://wiki.nginx.org/Pitfalls#Server_Name
server {
  # don't forget to tell on which port this server listens

  # commented out these two lines incase they were too general and resulting in this server being the first match for example2
  # listen [::]:80;
  # listen 80;

  # listen on the www host
  server_name www.example.com;

  # and redirect to the non-www host (declared below)
  return 301 $scheme://example.com$request_uri;
}

server {
  # listen 80 deferred; # for Linux
  # listen 80 accept_filter=httpready; # for FreeBSD

  # commented out these two lines incase they were too general and resulting in this server being the first match for example2
  # listen [::]:80;
  # listen 80;

  # The host name to respond to
  server_name www.example.com;

  # Path for static files
  root /var/www/example.com/public_html/;
  index index.html index.php

  # Specify a charset
  charset utf-8;

  # Custom 404 page
  error_page 404 /404.html;


  location ~ \.php$ {
      try_files $uri =404;
      # fastcgi_pass   127.0.0.1:9000;
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
      include        fastcgi_params;
  }

}

example2.com

# www to non-www redirect -- duplicate content is BAD:
# https://github.com/h5bp/html5-boilerplate/blob/5370479476dceae7cc3ea105946536d$
# Choose between www and non-www, listen on the *wrong* one and redirect to
# the right one -- http://wiki.nginx.org/Pitfalls#Server_Name
server {
  # don't forget to tell on which port this server listens
  listen [::]:80;
  listen 80;

  # listen on the www host
  server_name example2.com;

  # and redirect to the non-www host (declared below)
  return 301 $scheme://example2.com$request_uri;
} 

server {
  # listen 80 deferred; # for Linux
  # listen 80 accept_filter=httpready; # for FreeBSD
  listen [::]:80;
  listen 80;

  # The host name to respond to
  server_name example2.com;

  # Path for static files
  root /var/www/example2.com/public_html/;
   index index.html index.php

  # Specify a charset
  charset utf-8;

  # Custom 404 page
  error_page 404 /404.html;


  location ~ \.php$ {
      try_files $uri =404;
      # fastcgi_pass   127.0.0.1:9000;
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
      include        fastcgi_params;
  }

}

浏览器消息www.example2.com

The server at www.fionacreative.co.uk can't be found, because the DNS lookup failed. DNS is the network service that translates a website's name to its Internet address. This error is most often caused by having no connection to the Internet or a misconfigured network. It can also be caused by an unresponsive DNS server or a firewall preventing Google Chrome from accessing the network.

我今天早上确实更改了名称服务器,但它们似乎已通过域名注册商进行了更新。

我不认为这解释了http://example2.com解决之example.com道,两者应该是相同的。

答案1

编辑:您的问题是您的 DNS 配置,nginx没有涉及。

没有 的主机定义www.fionacreative.co.uk,但是 有一个fionacreative.co.uk


原文基于格式错误的问题

您已经example2.com定义了两次,您需要添加www其中一个服务器名称才能工作,可能是第一次定义。

server {
  # don't forget to tell on which port this server listens
  listen [::]:80;
  listen 80;

  # listen on the www host
  #############################
  server_name www.example2.com;         ##### this line needs a www ######
  #############################

  # and redirect to the non-www host (declared below)
  return 301 $scheme://example2.com$request_uri;
} 

server {
  # listen 80 deferred; # for Linux
  # listen 80 accept_filter=httpready; # for FreeBSD
  listen [::]:80;
  listen 80;

  # The host name to respond to
  server_name example2.com;

答案2

我被彻底否定了,但我设法解决了我的问题。

其他人都是对的,这不是与 nginx 有关的错误,这是一个 DNS 问题,但我不习惯 nginx,这次尝试使用它而不是 apache,这并没有帮助。

不管怎样,事实证明虽然我有一个 A Name 记录,但example2.com我需要另一个www.example2.com,然后给它几分钟来传播,清除缓存,它应该可以工作。

相关内容