格式化 Nginx 服务器块时出现问题

格式化 Nginx 服务器块时出现问题

因此,我尝试将其设置为我的子域名:“forum.project-freedom.net”访问我服务器上的 /var/www/forum/ 目录。我希望主 URL“project-freedom.net”位于我服务器的 /var/www/html/ 目录中。我正在使用 nginx,这是我当前的配置文件:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name project-freedom.net www.project-freedom.net;

    ssl_certificate /etc/ssl/certs/1546559737repl_1.crt;
    ssl_certificate_key /etc/ssl/private/project-freedom.key;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

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

    location /forum {
        alias /var/www/forum;
        index index.php index.html index.htm;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Change to match your PHP version
        }
    }

    # Redirect non-www to www
    if ($host = 'project-freedom.net') {
        return 301 https://www.project-freedom.net$request_uri;
    }

    # Redirect HTTP to HTTPS
    if ($scheme != "https") {
        return 301 https://$server_name$request_uri;
    }
}

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name forum.project-freedom.net;

    ssl_certificate /etc/ssl/certs/1546559737repl_1.crt;
    ssl_certificate_key /etc/ssl/private/project-freedom.key;

    root /var/www/forum;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Change to match your PHP version
    }

    # Redirect HTTP to HTTPS
    if ($scheme != "https") {
        return 301 https://$server_name$request_uri;
    }
}

答案1

我的问题更多的是,我应该如何为 nginx 构建 .conf 文件?发布这个问题时,这是我第一次使用 Nginx 而不是 Apache。

对于将来遇到这种困境的任何人,只需将您的域名设置server_name为 subdomain.domain.com(显然将您的域名放到位)

然后将根设置为您希望子域名处于活动状态的目录。

相关内容