[英]JS and Image directories loading in localhost but not when deployed express
我使用 nginx 作為 web 服務器,使用 cloudflare 作為 ddns。
JS 和圖像目錄似乎加載得很好localhost:5000/
。 但是,當我在aarth.in上查看時,導演不見了。 有很多解決方案,但沒有一個對我有用。 任何幫助,將不勝感激!
我的目錄如下
- node_modules
- server.js
- public
- index.html
- images/
- base.png
- sub
- scripts
- base.js
- sub
- css
- base.css
- sub/
- sub.css
服務器.js
const express = require('express');
const app = express();
app.use(express.static("public"));
app.get((req, res) => {
});
app.listen(5000, () => console.log('http://localhost:5000/'));
我在瀏覽器控制台中看到的 404 錯誤。
對於任何面臨同樣問題的人,結果證明server.js
中沒有錯誤。 我不得不更改 nginx 默認配置並注釋掉根目錄。
這是我的配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
#root /var/www/html; --> Commenting this did the trick!
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:YOUR_PORT;
try_files $uri $uri/ =404;
}
#Nginx handles all the static files instead of node
location ~^\/(images|css|fonts|js|icons|scss) {
expires 1M;
access_log off;
add_header Cache-Control "public";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET;
add_header Access-Control-Allow-Headers X-Requested-With,content-type;
add_header Access-Control-Allow-Credentials true;
root /home/pi/Documents/Website/public;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.