I went through this fireship tutorial to setup my own http server on an rPi, and thought it would be cool to add a favicon, but so far nothing works. I am a novice with nginx/express so have probably made some elementary mistake.
serve-favicon
dependency installed.const { readFileSync, writeFileSync } = require('fs')
const express = require('express')
const app = express()
const path = require('path')
app.listen(5000, () => console.log('http://localhost:5000/'))
const favicon = require('serve-favicon')
app.use(favicon(path.join(__dirname,'favicon.ico')));
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://localhost:5000;
try_files $uri $uri/ =404;
}
location = /favicon.ico {
try_files $uri =204;
log_not_found off;
access_log off;
}
}
After adding location = /favicon.ico {...}
to the nginx default file, I no longer get a 404, but the file is still not being served.
edit:
Tried below suggestion but still no joy.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
proxy_pass http://localhost:5000;
}
location = /favicon.ico {
alias /var/www/html/favicon.ico;
}
}
edit 2:
I am able to see favicon.ico locally with nginx and server.js in the following setup:
location = /favicon.ico {
alias /var/www/html/favicon.ico;
}
app.use("/favicon.ico", express.static("./favicon.ico"));
the icon is served when visiting http://localhost:5000
but not when visiting the network address, and not when visiting the external site address
edit 3:
I am not sure how, but it has spontaneously started working using express.
You don't need extra dependencies for this.
Serve using express. (It is not recommended to serve static files using Node/Express. But it is okay for tiny projects)
Express - server.js:
app.use("/favicon.ico", express.static("./favicon.ico"));
nginx - default:
location = /favicon.ico {
proxy_pass http://localhost:5000/favicon.ico;
}
Serve using Nginx.
/var/www/html/
directory:nginx - default:
location = /favicon.ico {
alias /var/www/html/favicon.ico;
}