我是Nginx的新手,我正试图让子域工作.
我想做的是拿我的域名(让我们称之为example.com)并添加:
sub1.example.com,sub2.example.com,也有www.example.com 可用.我知道如何用Apache做到这一点,但Nginx是一个真正的头脑.
我正在运行Debian 6.
我目前的/etc/nginx/sites-enabled/example.com:
server {
    server_name www.example.com example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;
    location / {
        index  index.html index.htm;
    }
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}
它正在努力为example.com和www.example.com提供服务.
我试图在同一个文件中添加第二个服务器块,如:
server {
        server_name www.example.com example.com;
        access_log /srv/www/www.example.com/logs/access.log;
        error_log /srv/www/www.example.com/logs/error.log;
        root /srv/www/www.example.com/public_html;
        server {
            server_name sub1.example.com;
            access_log /srv/www/example.com/logs/sub1-access.log;
            error_log /srv/www/example.com/logs/sub1-error.log;
            root /srv/www/example.com/sub1;
    }
        location / {
            index  index.html index.htm;
        }
        location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
        }
}
没运气.有任何想法吗?我非常感谢任何反馈.
Moh*_*ady 75
错误是将服务器块放在服务器块内,您应关闭主服务器块,然后为子域打开一个新服务器块
server {
    server_name example.com;
    # the rest of the config
}
server {
    server_name sub1.example.com;
    # sub1 config
}
server {
    server_name sub2.example.com;
    # sub2 config
}
Vin*_*san 14
为 DNS 提供商中的sub1.example.com和sub2.example.com添加一个字段
设置服务器。最后保留example.com
如下
server {
    server_name sub1.example.com;
    # sub1 config
}
server {
    server_name sub2.example.com;
    # sub2 config
}
server {
    server_name example.com;
    # the rest of the config
}
sudo systemctl restart nginx您只需要添加以下行来代替您的 server_name
server_name xyz.com  *.xyz.com;
并重新启动 Nginx。就是这样。
小智 7
您必须为您的子域创建另一个带有 serverblock 的 nginx 配置文件。像这样:
/etc/nginx/sites-enabled/subdomain.example.com