Express + Nginx.无法提供静态文件

rel*_*der 7 nginx node.js express

这是我的项目文件夹

/
  public/
    index.html
    main.js
    adaptor.js
    main.css
  node_modules/
    socket.io/
  index.js
Run Code Online (Sandbox Code Playgroud)

这是我的index.js中的静态文件配置

app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/node_modules')));
app.get('/', (req, res)=>{
    res.sendFile(path.join(__dirname, 'public', '/index.html'));
})
Run Code Online (Sandbox Code Playgroud)

这是我的index.html

  <script src="/socket.io/socket.io.js" charset="utf-8"></script>
  <script src="/adapter.js" charset="utf-8"></script>
  <script src="/main.js" charset="utf-8"></script>
Run Code Online (Sandbox Code Playgroud)

这是我的nginx配置

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html =404;
            proxy_pass http://localhost:8080;
    }
Run Code Online (Sandbox Code Playgroud)

但是我在所有脚本上都获得了404.另一个奇怪的事情是那些文件上的mime-type被设置为text/HTML

我在这做错了什么?

我有一个项目,具有相同的项目结构,并且具有相同的配置,但它适用于它,并且在这种情况下它不起作用.

Con*_*oad 17

您无需配置Nginx Express来提供静态文件.两者都能够独立完成工作,但您可以自行选择.

对于这些示例,我假设您的问题中提供了相同的文件结构.

在这两种配置中,从/中加载HTML文件:

<script src="/main.js"></script> <!-- loads from myapp/public/main.js -->
Run Code Online (Sandbox Code Playgroud)

Express作为静态文件服务器,Nginx作为反向代理

快递应用

const express = require('express');
const app = express();

app.use(express.static('public')); // notice the absence of `__dirname`, explained later on

// if the request URL doesn't match anything in the 'public' folder,
// it will start searching here next.
app.use(express.static('some_other_folder'));

// from my testing, express will automatically locate index.html if
// it is in a static folder. Declaring a route is not required.

/* 
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
*/

app.listen(8080);

// GET / => index.html
// GET /main.js => main.js
Run Code Online (Sandbox Code Playgroud)

快速注意:不需要使用__dirnamein express.static().引擎盖下(实际上,这是在这里第65行),快速使用本机的Node.js path.resolve() .来自文档:

path.resolve()方法将一系列路径或路径段解析为绝对路径.

使用path.resolve(__dirname, 'public')实际返回相同path.resolve('public').我认为你的问题确实告诉Nginx提供静态文件并代理相同的Express请求.好的,继续我的答案.

Nginx配置

server {
  listen 8081; # must be different port from Express
  server_name example.com;
  location / {
    # hand ALL requests back to express
    proxy_pass http://localhost:8080; 
  }
}
Run Code Online (Sandbox Code Playgroud)

Nginx作为静态文件服务器,Express作为API服务器

Nginx配置

server {
  listen 8081;
  server_name example.com;
  location / {
    root /path/to/website/public;
    index index.html;
    try_files $uri $uri/ @express; # instead of 404, proxy back to express using a named location block;
    # source: https://stackoverflow.com/a/15467555/8436941
  }
  location @express {
    proxy_pass http://localhost:8080;
  }
}
Run Code Online (Sandbox Code Playgroud)

快递应用

const express = require('express');
const app = express();

// actually, Nginx has already taken care of static files. You can still define other routes for API functions for example.
app.get('/my/api', (req, res) => {/* query database, etc. */});

app.listen(8080);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 这是有史以来最好的答案!过去 24 小时我几乎被困住了,几乎没有睡过 3 个小时。然后我找到了这个答案。谢谢@ContinouslyLoad。我的路线都无法访问,只有主页 / 可以访问。现在,一切正常。谢谢。 (2认同)

小智 7

try_filesnginx 配置文件中删除指令为我解决了这个问题。