为什么我不能与nginx一起拥有多个域名?

为什么我不能与nginx一起拥有多个域名?,nginx,Nginx,我必须要重定向到一个nginx服务器的子域:first.domainOne.com和second.domainTwo.net 我的nginx站点可用目录中有两个文件(每个文件都有一个符号链接,在站点启用中指向它): first.server文件内容: server { root /usr/share/nginx/www; index index.html index.htm; server_name first.domainOne.com;

我必须要重定向到一个nginx服务器的子域:first.domainOne.com和second.domainTwo.net

我的nginx站点可用目录中有两个文件(每个文件都有一个符号链接,在站点启用中指向它): first.server文件内容:

server {
        root /usr/share/nginx/www;
        index index.html index.htm;

        server_name first.domainOne.com;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }
server{ 
        server_name second.domainTwo.net;

        root /usr/share/nginx/test;
        index index.html index.htm;

        location / {                                                                                                                    
            try_files $uri $uri/ /index.html;
        }                                                                                                                                      
    }
second.server文件内容:

server {
        root /usr/share/nginx/www;
        index index.html index.htm;

        server_name first.domainOne.com;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }
server{ 
        server_name second.domainTwo.net;

        root /usr/share/nginx/test;
        index index.html index.htm;

        location / {                                                                                                                    
            try_files $uri $uri/ /index.html;
        }                                                                                                                                      
    }
当我启用这两个文件时,我只能访问/usr/share/nginx/www的内容(如果我访问first.domainOne.com或second.domainTwo.net)

我必须能够显示第二台服务器的内容(/usr/share/nginx/test)的唯一方法是禁用(删除)sites enable中的第一个.server文件

这是我的nginx.conf:

    user www-data;
    worker_processes 4;
    pid /var/run/nginx.pid;

    events {
        worker_connections 768;
    }

    http {

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
    }
编辑:两个重定向都直接指向服务器名称(ie),但调试日志显示它检测到使用的两个不同URL):

server.name是承载我的nginx实例的服务器


我错过了什么?我的配置文件是否不正确,或者是否需要激活nginx.conf中的选项?

nginx支持多个服务器块,并且您的语法看起来合理。我怀疑问题可能出在上面没有看到的东西上。试着缩小搜索服务器的规模,看看你是否能找到问题所在。您还可以尝试从/etc/nginx/conf.d/*.conf中删除其他文件,以继续简化问题

下面的内容也可以解决您的问题

您也可以尝试从开始,然后构建它们以满足您的需求


最后,您的文章提到涉及重定向,但我在粘贴的代码中没有看到重定向。您发布的内容中是否缺少部分配置?问题可能就在那里

您是否在本地计算机上进行测试?否,在远程服务器上尝试显式地包括启用的
站点
文件,即
包括/etc/nginx/sites enabled/first.server
包括/etc/nginx/sites enabled/second.server
并查看订单是否重要订单是否重要。如果我还原第一个和第二个,第二个(在nginx.conf中设置为第一个)将始终显示(无论URL是什么)。我还强调,这两个都是子域重定向。我不知道这有什么关系。重定向是从外部服务器进行的,我添加了一些信息。这是一个简单的网页重定向。谢谢你的帮助。通过检查调试日志,我发现/test/地址没有指向/usr/share/nginx/test,而是指向/usr/share/nginx/test。这导致正确的重定向失败。