如何使两个域访问服务器,并使用两个不同的主页?

我有一个DigitalOcean服务器与Ubuntu的Linux,Nginx的1.4.6(运行在端口80),清漆3.0.5(运行在端口8080,在一起)我有两个名,说siteA.com和siteB.com。 在nginx的default.confconfiguration中,前门(80)以root身份使用siteA文件夹,代码是:

server { listen *:8080 default_server; root /home/sitea; index index.html index.htm index.php; server_name IP_domain_siteA; location / { autoindex on; autoindex_exact_size off; # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location ~ \.php$ { try_files $uri =404; expires off; fastcgi_read_timeout 900s; fastcgi_index index.php; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

但我希望SiteB使用相同的端口访问使用2个域到同一台服务器。 所以当我从访问:

 siteA.com => carry my server folder: /home/siteA/index.php siteB.com => carry the same server folder (same ip as well): /home/siteB/index.html 

我怎么做? 我已经尝试了一切,甚至在default.VCL(varnishconfiguration)中包括这些后端行。

 backend siteA{ .host = "sitea.com"; .port = "8080"; } backend siteB{ .host = "siteb.com"; .port = "8080"; } sub vcl_recv { if (req.http.host == "sitea.com") { #You will need the following line only if your backend has multiple virtual host names set req.http.host = "sitea.com"; set req.backend = siteA; return (lookup); } if (req.http.host == "siteb.com") { #You will need the following line only if your backend has multiple virtual host names set req.http.host = "siteb.com"; set req.backend = siteB; return (lookup); } } 

它没有解决,它返回我的错误:

BACKEND HOST“siteB.com”:parsing为多个IPv4地址。 只允许一个地址。

我已经使用虚拟主机 ,为其他文件夹与Nginx,但它只是可能的更改端口,行server_name定向到域A或域B,不工作….因为是相同的IP

我能做什么有什么build议? 谢谢

编辑1:

这两个网站的nginxconfiguration在这里(siteA):

 server { listen *:8080 default_server; root /var/www/public/sitea; try_files $uri $uri/ @handler; index index.php index.html index.htm; # Make site accessible from http://localhost/ ##domain address 1 of server... server_name www.sitea.com.br sitea.com.br; #location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. #try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules #} ## These locations would be hidden by .htaccess normally location ^~ /app/ { deny all; } location ^~ /includes/ { deny all; } location ^~ /lib/ { deny all; } location ^~ /media/downloadable/ { deny all; } location ^~ /pkginfo/ { deny all; } location ^~ /report/config.xml { deny all; } location ^~ /var/ { deny all; } location /var/export/ { ## Allow admins only to view export folder auth_basic "Restricted"; ## Message shown in login window auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword autoindex on; proxy_read_timeout 150; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location @handler { ## Magento uses a common front handler rewrite / /index.php; } location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler rewrite ^(.*.php)/ $1 last; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_read_timeout 120; include fastcgi_params; } } 

另一个网站(siteB):

 server { listen 8090; client_max_body_size 20M; root /var/www/public/siteb; index index.html index.htm index.php; ##domain address 2 of server... server_name www.siteb.com.br siteb.com.br; location / { autoindex on; try_files $uri $uri/ /index.php?q=$request_uri; autoindex_exact_size off; proxy_pass http://localhost:8080; } location ~ \.php$ { #try_files $uri =404; expires off; fastcgi_read_timeout 900s; fastcgi_index index.php; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

如果您正在使用Varnish,则可以configuration清除到其他网站,例如default.vcl:

 acl purge { "localhost"; "127.0.0.1"; "siteb.com"; } 

和nginxconfiguration:

 server { listen 8080 default_server; server_name sitea.com; ... } server { listen 8080; server_name siteb.com; ... } 

如果你caching两个网站的清漆Nginx无法正确区分。 也许有更好的办法,但这也有效。

您收到的错误表明siteB.com有多个IPv4地址。 而不是使用DNS名称指定后端主机,只需使用localhost或127.0.0.1即可。 这可能安抚清漆,它消除了可能导致延迟或(显然在这种情况下)出错的DNS查找。 我对自己的清漆不太熟悉,但是我还会想,你只需要指定一个单一的后端(因为它实际上是同一个盒子),只要确保正确的主机头文件通过nginx 。 说起来…

你不需要再设置代理。 Nginx将乐意让你指定两个监听同一端口的虚拟主机,并使用server_name指令来匹配传入请求的主机头。 任何与已知server_name不匹配的请求都将被默认服务器块捕获,通常是首先指定的服务器块,除非您使用listen指令的default_server选项覆盖该服务器块。 看下面的例子:

 server { listen 8080; server_name siteA.com; ... } server { listen 8080 default_server; server_name siteB.com; ... } 

在这里,两个服务器块都监听端口8080,nginx通过匹配主机头来知道哪个请求是哪个站点的。 如果获取的请求没有主机匹配,则通常会将其作为第一个定义的块传递给siteA.com的块,但是我们通过声明siteB.com的块为默认值来专门覆盖该行为服务器在该端口上的请求。

由于你的清漆configuration确保你在将请求传递给后端之前设置适当的主机头,所以你应该做的就是从它服务多个站点。 在这种情况下,我不应该认为你甚至需要手动清漆,因为你不会覆盖标题; 大多数似乎是正常化(即收集www.site.com和site.com,所以你不要caching两个相同的东西副本)。 我的阅读表明,你不应该指定多个后端,因为它是同一端口上的同一台服务器。 清漆应该足够聪明,根据不同的主机分开caching。