如何配置nginx来托管多个子域

问题描述 投票:0回答:2

我目前在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;
}

当我删除listen 443 part时,它会重定向到主域。

我想从var / www / test.subdomain.com提供子域test.example.com的请求,但似乎无法通过404错误

nginx dns webserver subdomain nginx-config
2个回答
0
投票

您需要执行以下操作 -

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

0
投票

问题出在php.conf文件中。我应该意识到,因为我能够加载index.html文件但不能加载.php文件。包含正确的php.conf文件后,它们都开始工作了。

© www.soinside.com 2019 - 2024. All rights reserved.