快遞 - server.js
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')));
HTML(嘗試有/沒有)
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
nginx - 默認
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;
}
}
添加location = /favicon.ico {...}到nginx默認檔案后,我不再得到404,但檔案仍然沒有被服務。
- 如果有 / 的位置,不應該提供檔案嗎?
- proxy_pass 是否增加了我不知道如何解決的復雜性。
編輯:
嘗試了以下建議,但仍然沒有喜悅。
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;
}
}
編輯2:
我可以在以下設定中使用 nginx 和 server.js 在本地查看 favicon.ico:
/etc/nginx/sites-available/default
location = /favicon.ico {
alias /var/www/html/favicon.ico;
}
服務器.js
app.use("/favicon.ico", express.static("./favicon.ico"));
訪問時提供圖示,http://localhost:5000但訪問網路地址時不提供,訪問外部站點地址時不提供
編輯3:
我不確定如何,但它已經自發地開始使用 express 作業。
- 我有快遞服務的檔案
- 我用 nginx 給了檔案一個 proxy_pass
- 似乎任何路線都必須像這樣明確布局才能作業
- HTML 中參考的檔案的鏈接似乎不是必需的
uj5u.com熱心網友回復:
為此,您不需要額外的依賴項。
第一種方法:
使用快遞服務。(不建議使用 Node/Express 提供靜態檔案。但對于小型專案來說還可以)
- favicon.ico 位于根目錄中:
快遞 - server.js:
app.use("/favicon.ico", express.static("./favicon.ico"));
nginx - 默認值:
location = /favicon.ico {
proxy_pass http://localhost:5000/favicon.ico;
}
第二種方法:
使用 Nginx 服務。
- favicon.ico 在
/var/www/html/目錄中:
nginx - 默認值:
location = /favicon.ico {
alias /var/www/html/favicon.ico;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511196.html
