如何使用多个 Express 应用程序 + NGINX 作为反向代理服务器来提供静态文件 (CSS, ...)
How to serve static files (CSS, ...) with multiples Express app + NGINX as reverse proxy server
上下文
我在具有相同 IP 地址的同一服务器上运行多个 nodesJS/Express 应用程序。
我使用 Nginx 反向代理这些应用程序并将其重定向到子文件夹地址(而不是子域,我不想这样做)。
例如:http://123.0.0.1:8000 => http://monsite.com/Site1
问题
我的资产文件(css、图像等)未加载,页面加载时这些静态文件出现 404 错误。只有当我通过代理重定向访问网站时才会发生 http://monsite.com/Site1 and not when I use the IP adress : http://123.0.0.1:8000
如果在 nginx conf 中使用根目录的反向代理位置,我就不会遇到这个问题:
location / {
但我想从子文件夹地址访问该网站
我的积分
树文件:
var/www/html
|Site1/
| |server.js
| |Views/
| | |index.pug
| |Public/
| | |Css/
| | | |Style.css
|Site2/
|....
nodejs 服务器代码
const PORT = 8000;
const HOSTNAME = 'www.monsite.com';
// Dependencies.
const express = require('express');
const http = require('http');
// Initialization.
var app = express();
var server = http.Server(app);
app.set('port', PORT);
app.set('view engine', 'pug');
app.set('views','Views');
app.use(express.static('Public'));
app.use('/', (request, response) => {
response.render('index');
});
server.listen(PORT, HOSTNAME, function() {
console.log(`STARTING SERVER ON PORT ${PORT}`);
});
索引哈巴狗代码
doctype html
html
head
title Site 1
link(rel="stylesheet" href="/Css/style.css")
body
p Hello
nginx 配置文件
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name www.monsite.com;
location / {
#Reserved for another site
}
location /Site1/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://123.0.0.1:8000/;
}
}
PS :我尝试了几乎所有搜索此问题的解决方案和代码,但没有任何效果,这就是我直接在这里询问的原因。谢谢。
我认为问题在于 link 标签中的 url 加载 css,url 无效,因为 url 是实际上 /Site1/Css/style.css
.
上下文
我在具有相同 IP 地址的同一服务器上运行多个 nodesJS/Express 应用程序。
我使用 Nginx 反向代理这些应用程序并将其重定向到子文件夹地址(而不是子域,我不想这样做)。
例如:http://123.0.0.1:8000 => http://monsite.com/Site1
问题
我的资产文件(css、图像等)未加载,页面加载时这些静态文件出现 404 错误。只有当我通过代理重定向访问网站时才会发生 http://monsite.com/Site1 and not when I use the IP adress : http://123.0.0.1:8000
如果在 nginx conf 中使用根目录的反向代理位置,我就不会遇到这个问题:
location / {
但我想从子文件夹地址访问该网站
我的积分
树文件:
var/www/html
|Site1/
| |server.js
| |Views/
| | |index.pug
| |Public/
| | |Css/
| | | |Style.css
|Site2/
|....
nodejs 服务器代码
const PORT = 8000;
const HOSTNAME = 'www.monsite.com';
// Dependencies.
const express = require('express');
const http = require('http');
// Initialization.
var app = express();
var server = http.Server(app);
app.set('port', PORT);
app.set('view engine', 'pug');
app.set('views','Views');
app.use(express.static('Public'));
app.use('/', (request, response) => {
response.render('index');
});
server.listen(PORT, HOSTNAME, function() {
console.log(`STARTING SERVER ON PORT ${PORT}`);
});
索引哈巴狗代码
doctype html
html
head
title Site 1
link(rel="stylesheet" href="/Css/style.css")
body
p Hello
nginx 配置文件
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name www.monsite.com;
location / {
#Reserved for another site
}
location /Site1/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://123.0.0.1:8000/;
}
}
PS :我尝试了几乎所有搜索此问题的解决方案和代码,但没有任何效果,这就是我直接在这里询问的原因。谢谢。
我认为问题在于 link 标签中的 url 加载 css,url 无效,因为 url 是实际上 /Site1/Css/style.css
.