Home > database >  nginx html not showing in subdirectories
nginx html not showing in subdirectories

Time:03-01

I have a default html template that I show instead of the nginx 404 default one if I visit jitsi.example.com:

<h1> Not found </h1>

The problem comes when I want to show that same html page in a subroute like jitsi.example.com/foo for example and I get the nginx 404 default template. I hardcoded foo as an example but the idea is to match any subdirectory / subroute.

These are my config files:

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/*;
}

And this is my default file:

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;
    }
}

And I know it's getting to /foo route because if I return 200; in nginx I get that status code. However, I want to render the template from /home/example/nginx-templates.

CodePudding user response:

Ok. I created an error_404.html:

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

with:

<h1> Not found </h1>

Then I reference it inside /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;
}

  • Related