NginX代理Nodejs / Express - 404静态文件 [英] NginX proxying Nodejs/Express - 404 on static files

查看:191
本文介绍了NginX代理Nodejs / Express - 404静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NginX的设置,它从/ var / www的根目录提供一个php网站,并在我的服务器的一个特定子目录上代理Nodejs / Express。以下是 server {} 块中 / etc / nginx / sites-enabled / default 中Nodejs的相关配置:

  location / subdir / {
proxy_pass http:// localhost:8000;
proxy_redirect关闭;
proxy_http_version 1.1
proxy_set_header X-Real-IP $ remote_addr;
proxy_redirect关闭;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
proxy_set_header主机$ http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header升级$ http_upgrade;
proxy_set_header连接'升级';
proxy_cache_bypass $ http_upgrade;
}

这是我的Nodejs应用程序的代码:

  var port = 8000; 
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app).listen(port);
var io = require('socket.io')。listen(server);

app.use(express.static(__ dirname +'/ site'));
app.get('/ subdir /',function(req,res){
res.sendFile(__ dirname +'/site/index.html');
});

server.listen(port,function(){
console.log(Listening on port+ port +...);
});

io.on('connection',function(socket){
console.log(A user connected);
});

在大部分情况下,一切正常,当我在 http:// server / subdir / ,我得到我希望的index.html文件。但是,我在其他静态内容上收到404错误,如 http://server/subdir/css/main.css 404(未找到)我的浏览器中直接访问文件时,即使文件存在,也无法GET /sudir/css/main.css 错误。



为什么Node / Express找不到我的静态内容?



或者,如何让NginX提供静态内容?我甚至尝试在 location / subdir / {} 块中添加一个语句来指向Node / Express项目目录,但仍然有404错误。

解决方案

如果要通过不同的路由访问静态文件,请将路由作为第一个参数:



您可以使用 app.use('/ static',express.static(__ dirname +'/ public'));



和GET到 http://server/static/css/main.css 将服务main.css



另外如果你想通过nginx提供静态文件,这篇文章是一个很好的开始:
使用NGINX来避免节点.JS LOAD


I have a setup with NginX which serves a php website off the root from /var/www and proxies Nodejs/Express on a particular subdirectory on my server. Here is the relevant configuration for Nodejs in /etc/nginx/sites-enabled/default inside the server{} block:

location /subdir/ {
    proxy_pass http://localhost:8000;
    proxy_redirect off;
    proxy_http_version 1.1
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
}

This is the code for my Nodejs application:

var port = 8000;
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app).listen(port);
var io = require('socket.io').listen(server);

app.use(express.static(__dirname + '/site'));
app.get('/subdir/', function(req, res) {
    res.sendFile(__dirname + '/site/index.html');
});

server.listen(port, function() {
    console.log("Listening on port " + port + "...");
});

io.on('connection', function(socket) {
    console.log("A user connected");
});

For the most part, everything works fine, when I access my server at http://server/subdir/, I get the index.html file that I expect. However, I get 404 errors on the other static content like http://server/subdir/css/main.css 404 (Not Found) or a Cannot GET /sudir/css/main.css error when I directly access the file in my browser even though the file is there.

Why is it that Node/Express cannot find my static content?

Alternatively, how can I get NginX to serve my static content? I even tried adding a root statement in the location /subdir/ {} block to point to the Node/Express project directory but still I get 404 errors.

解决方案

If you want to access the static files through a different route, pass the route as the first argument :

You can use app.use('/static', express.static(__dirname + '/public'));

and a GET to http://server/static/css/main.css will serve main.css

Also if you want to serve static files through nginx this article is a good start: USING NGINX TO AVOID NODE.JS LOAD

这篇关于NginX代理Nodejs / Express - 404静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆