如何配置Nginx托管多个子域

时间:2019-04-15 23:10:21

标签: nginx dns webserver subdomain nginx-config

我目前在NGINX上设置了example.com域,并且该域名有效。我想设置一个子域test.example.com,但按照多个不同的说明进行操作后,我要么继续获取404,要么被重定向到主域。

我的服务器配置和文件结构如下:

/usr/local/nginx
├── conf
│   ├── fastcgi_params
│   ├── includes
│   ├── nginx.conf
│   ├── nginx.conf.save
│   ├── sites
│   │   ├── example.com.conf
│   │   ├── test.example.com.conf
│   ├── uwsgi_params
│   └── win-utf
├── html
├── logs
└── snippets
    ├── ssl-example.com.conf
    └── ssl-params.conf

/var/www
├── example.com
│   └── ...
└── test.example.com
    └── index.html

我已经更新了DNS记录,以便“ * .example.com”指向同一IP

默认域example.com.conf文件如下所示:

server {
    listen 80;
    server_name exmaple.com www.example.com;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    location ~ /.well-known {
        root /var/www/lets-encrypt/example.com;
        access_log off;
        expires max;
        break;
    }
    include /usr/local/nginx/conf/includes/m2-php71.conf;
}

和子域test.example.com.conf文件:

server {
    listen 80;
    server_name test.example.com;
    return 301 https://$server_name$request_uri;    
}

server {
    listen 443 ssl;
    server_name test.example.com;
    ssl_certificate /etc/letsencrypt/live/test.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/test.exampe.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    location ~ /.well-known {
        root /var/www/lets-encrypt/test.example.com;
        access_log off;
        expires max;
        break;
    }
    include /usr/local/nginx/conf/includes/m2-php71.conf;
}

当我删除监听443部分时,它会重定向到主域。

我希望从var / www / test.subdomain.com提供对子域test.example.com的请求,但似乎无法解决404错误

2 个答案:

答案 0 :(得分:0)

您需要执行以下操作-

  1. 从两个配置之一中删除www.example.com
  2. 在服务器部分添加两个配置的alias

答案 1 :(得分:0)

问题出在php.conf文件。我应该意识到,因为我能够加载index.html文件,但无法加载.php文件。包含正确的php.conf文件后,所有这些都开始工作。