【问题标题】:NGINX Multiple Site Config One IPNGINX 多站点配置一个 IP
【发布时间】:2016-04-22 06:33:22
【问题描述】:

我有一台虚拟机,我的公司将其用作 Web 服务器。我们有一个内部 PHP 应用程序,现在需要向该服务器添加一个站点。问题是虚拟机只有一个 IP。我将 Windows DNS 管理器设置为指向此 IP,但它只会拉起主站点。有没有办法将 nginx 配置到它将选择要拉起的站点的位置?

这是我的第一个站点配置:

 server {

  server_name develop.aspirion.com;


  root /var/www/vhosts/develop.aspirion.com;
  index index.html;

  location ~ \.php$ {

    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
 }
}

这是我的第二个站点配置:

server {

server_name aspirion.com;

access_log  /var/log/nginx/aspirion.com-access.log;
error_log   /var/log/nginx/aspirion.com-error.log;

root /var/virtual/aspirion.com/master/current/webroot;
index index.php;

location / {
  # error_page 404 /index.php;

  location = / {
    error_page 404 =200 /index.html;
  }

  location ~* ^.+\.(?:css|js|jpe?g|gif|htc|ico|png|html|xml)$ {
    access_log off;
    expires 30d;
    tcp_nodelay off;
    open_file_cache max=3000 inactive=120s;
    open_file_cache_valid 45s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
  }
  location ~* ^.+\.(?:pdf|pptx?)$ {
    expires 30d;
    tcp_nodelay off;
  }

  # TODO: never allow private files inside the web root?
  location ^~ /sites/default/files/private/ {
    internal;
  }

  location ~* ^(?:.+\.(?:htaccess|make|txt|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(?:\.php)?|xtmpl)|code-style\.pl|/Entries.*|/Repository|/Root|/Tag|/Template)$ {
    return 404;
  }

  try_files $uri $uri/ /index.php?$uri&$args;
}

location = /index.php {
  fastcgi_pass phpcgi;
}

location = /.git {
  return 404;
}

location = /patches {
  return 404;
}

location = /backup {
  return 404;
}

location = /robots.txt {
  access_log off;
}

location = /humans.txt {
  access_log off;
  try_files $uri $uri/ /index.php?$uri&$args;
}

location = /rss.xml {
  try_files $uri $uri/ /index.php?$uri&$args;
}

location = /sitemap.xml {
  try_files $uri $uri/ /index.php?$uri&$args;
}

location = /favicon.ico {
  try_files /favicon.ico =204;
}

location ~* ^.+\.php$ {
  return 404;
}
}

server {
server_name munin.aspirion.com;

include sites-available/admin.conf;
 }

【问题讨论】:

    标签: php nginx dns server webserver


    【解决方案1】:

    是的,Nginx 从一个 IP 地址服务多个域是正常的。

    浏览器将两个域的 DNS 解析为同一个 IP,然后将流量转发到该 IP 地址上的端口 80。 Nginx 在那里侦听并检查传入的 Host: 标头并将其与配置中的 server_name 值匹配。您的问题与在一个 IP 地址上拥有两个域无关。试试这个:

    1. 更改 Nginx 配置文件后,请确保重新加载 Nginx。
    2. 确保所有域都已实际启用。 (即:如果您将文件放在sites-available 中,请确保有指向sites-enabled 的符号链接。
    3. 尝试更简单的配置。尝试仅使用一个域的配置,然后尝试仅使用另一个域的配置。尝试只提供简单静态文件的配置。
    4. 检查您的 DNS。您希望涉及的所有域最终都应解析为 VM 的 IP。检查每一个:dig +short example.com
    5. 尝试更改配置在 Nginx 配置文件中出现的顺序。这不应该适用,因为我没有看到通配符匹配,但如果没有其他工作...

    您的 Nginx 配置显示您有单独的 server 块和唯一的 server_name 值,因此通常您的配置看起来适合在单个 IP 地址上托管多个域。

    (如果您碰巧去掉了 SSL 配置,则可能存在相关问题,但我假设不是基于您的配置)。

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 1970-01-01
      • 2013-08-29
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多