将子域添加到现有的 nginx 域配置?

时间:2021-05-06 05:56:34

标签: nginx certbot

我认为这将是一个简单的过程,但我无法启动并运行它。

我在我的域管理器“dev.example.com”上添加了一个 A 记录。

我在 /etc/nginx/sites-available/dev.example.com 中做了一个新的 nginx 配置

此配置包含以下内容(这是在我运行 certbot 命令之后):

server {
        root /var/www/html/dev.example.com;
        index index.php index.html index.htm;

        server_name dev.example.com;

        location / {
                try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/dev.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/dev.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = dev.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name dev.example.com;
    return 404; # managed by Certbot


}

我运行了 $ sudo certbot,选择了我的域并得到了确认。

这是 /etc/nginx/sites-available/example.com 配置文件:

server {
    location /nginx_status {
        stub_status;
    }

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html/example.com/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm;

        server_name example.com www.example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri /index.php$is_args$args;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php {
#               try_files $uri =404;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
        # pass PHP scripts to FastCGI server
        #
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
   listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = example.com) {
               return 301 https://$host$request_uri;
    } # managed by Certbot
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    server_name example.com www.example.com;
    listen 80;
    return 404; # managed by Certbot
}

我跑了

  • sudo ln -s /etc/nginx/sites-available/dev.example.com /etc/nginx/sites-enabled/
  • sudo nginx -t
  • sudo services nginx restart

我的最终结果是使用与 dev.example.com 相同的证书访问 example.com

0 个答案:

没有答案