nginx html 未在子目录中显示

nginx html not showing in subdirectories

如果我访问 jitsi.example.com:

,我会显示一个默认的 html 模板,而不是 nginx 404 默认模板

<h1> Not found </h1>

当我想在像 jitsi.example.com/foo 这样的子路径中显示相同的 html 页面时,问题就来了,例如,我得到了 nginx 404 默认模板。我硬编码 foo 作为一个例子,但我的想法是匹配任何子目录/子路径。

这些是我的配置文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

http {

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

这是我的默认文件:

upstream backend{
    server localhost:8012;
    server localhost:8013;
}

server {

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;
    
    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    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
    server_name jitsi.example.com; # managed by Certbot
    #return 404; # managed by Certbot

    listen [::]:443 ssl; # managed by Certbot
    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
    location / {
        root /home/example/nginx-templates;
    }

    location /foo {
        root /home/example/nginx-templates;
    }
}

而且我知道它正在到达 /foo 路线,因为如果我 return 200;在 nginx 中,我得到了那个状态码。但是,我想从 /home/example/nginx-templates.

渲染模板

好的。我创建了一个 error_404.html:

sudo vim /usr/share/nginx/html/error_404.html

与:

<h1> Not found </h1>

那我在里面引用一下/etc/nginx/sites-enabled/default:

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

error_page 404 =404 /error_404.html;
location = /error_404.html {
    root /usr/share/nginx/html;
    internal;
}

location / {
    root /home/example/nginx-templates;
}