Nginx在同一个IP上的不同域名 [英] Nginx Different Domains on Same IP

查看:160
本文介绍了Nginx在同一个IP上的不同域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用nginx在同一个服务器上托管2个不同的域。
我通过@属性将两个域重定向到此主机。虽然我配置了两个不同的服务器块,每当我尝试访问第二个域时,它将重定向到第一个。



这是我的配置。

  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;

位置〜\.php $ {
#安全性:必须在php.ini中将cgi.fixpathinfo设置为0!
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;
包含/ etc / nginx / fastcgi_params;
}
}

服务器{
收听www.domain2.com:80;
access_log /var/log/nginx/host.domain2.access.log main;
root / var / www / domain2;
server_name www.domain2.com;

位置〜\.php $ {
#安全性:必须在php.ini中将cgi.fixpathinfo设置为0!
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;
包含/ etc / nginx / fastcgi_params;
}
}

我该如何解决?
谢谢。

解决方案

你的听指令是错误的。请参阅此页面: http://nginx.org/en/docs/http/server_names.html



他们应该是

  server {
听80
server_name www.domain1.com;
root / var / www / domain1;
}

server {
listen 80;
server_name www.domain2.com;
root / var / www / domain2;
}

注意,我只包括相关行。一切都看起来不错,但是为了清楚起见我删除了它。要测试它,您可能希望在实际提供php之前首先尝试从每个服务器提供文本文件。这就是为什么我在那里离开了根指令。


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.

Here is my config.

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.

解决方案

Your "listen" directives are wrong. See this page: http://nginx.org/en/docs/http/server_names.html.

They should be

server {
    listen      80;
    server_name www.domain1.com;
    root /var/www/domain1;
}

server {
    listen       80;
    server_name www.domain2.com;
    root /var/www/domain2;
}

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.

这篇关于Nginx在同一个IP上的不同域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆