同一IP上的Nginx不同域 [英] Nginx Different Domains on Same IP
问题描述
我想使用 nginx 在同一台服务器上托管 2 个不同的域.我通过@ 属性将两个域重定向到该主机.虽然我配置了 2 个不同的服务器块,但每当我尝试访问第二个域时,它都会重定向到第一个.
I would like to host 2 different domains in the same server using nginx. I redirected both domains to this host via @ property. Although I configure 2 different server blocks, whenever I try to access second domain, it redirects to first one.
这是我的配置.
server {
listen www.domain1.com:80;
access_log /var/log/nginx/host.domain1.access.log main;
root /var/www/domain1;
server_name www.domain1.com;
location ~ .php$ {
# Security: must set cgi.fixpathinfo to 0 in php.ini!
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include /etc/nginx/fastcgi_params;
}
}
server {
listen www.domain2.com:80;
access_log /var/log/nginx/host.domain2.access.log main;
root /var/www/domain2;
server_name www.domain2.com;
location ~ .php$ {
# Security: must set cgi.fixpathinfo to 0 in php.ini!
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include /etc/nginx/fastcgi_params;
}
}
我该如何解决这个问题?谢谢.
How can I fix this? Thanks.
推荐答案
您的听"指令是错误的.请参阅此页面:http://nginx.org/en/docs/http/server_names.html.
Your "listen" directives are wrong. See this page: http://nginx.org/en/docs/http/server_names.html.
他们应该是
server {
listen 80;
server_name www.domain1.com;
root /var/www/domain1;
}
server {
listen 80;
server_name www.domain2.com;
root /var/www/domain2;
}
注意,我只包含了相关的行.其他一切看起来都不错,但为了清楚起见,我只是删除了它.为了测试它,您可能想在实际提供 php 之前先尝试从每个服务器提供一个文本文件.这就是为什么我在那里留下了root"指令.
Note, I have only included the relevant lines. Everything else looked okay but I just deleted it for clarity. To test it you might want to try serving a text file from each server first before actually serving php. That's why I left the 'root' directive in there.
这篇关于同一IP上的Nginx不同域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!