Nginx 不会将子域名的 http 重定向到 https

Nginx 不会将子域名的 http 重定向到 https

我在 Linode 实例上托管了一个子域名(主域名托管在另一台服务器上)。我希望始终将此子域名重定向到 HTTPS。

这是我的 /etc/nginx/sites-available/myconf

    server {
    listen 80;
    listen [::]:80;
    server_name sub.example.com

    return 301 https://sub.example.com$request_uri; 
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name sub.example.com;

        ssl on;
        ssl_certificate /etc/letsencrypt/live/sub.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/sub.example.com/privkey.pem;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    root /var/www/production/public;

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

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm:
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}

nginx -c /etc/nginx/sites-available/myconf -t输出以下内容:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2018/02/01 22:52:40 [emerg] 6816#6816: "server" directive is not allowed here in /etc/nginx/sites-available/myconf:1
nginx: configuration file /etc/nginx/sites-available/myconf test failed

我不知道我做错了什么,因为这似乎在其他网站上有效。

感谢您的任何帮助!

编辑:http 版本仅显示默认的 nginx 欢迎页面

答案1

看来我的问题是;之后缺失的server_name sub.example.com

相关内容