如何使用Express和NGINX设置路由? [英] How to setup routes with Express and NGINX?
本文介绍了如何使用Express和NGINX设置路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用NGINX作为反向代理配置Express服务器. NGINX提供静态文件,而Express提供动态内容.
I'm trying to configure an Express server with NGINX as a reverse proxy. NGINX to serve static files, and Express for the dynamic content.
问题:正常的根链接有效(website.com),但是当我导航至(website.com/api)时,我从NGINX收到404
Problem : The normal root link works (website.com) , but when I navigate to (website.com/api), I get a 404 from NGINX
这是我的server.js:
This is my server.js :
var express = require("express");
var app = express();
var server = app.listen(process.env.PORT || 5000);
console.log("Server Running");
app.get("/",function(req,res){res.send("HOME PAGE")});
app.get("/api", function(req, res) {
res.send('API PAGE');
});
这是我的NGINX Config文件:
This is my NGINX Config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name website.com www.website.com;
location ~ ^/(assets/|images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /home/foobar/public; #this is where my static files reside
access_log off;
expires 24h;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
推荐答案
尝试删除此行:
try_files $uri $uri/ =404;
使用此伪指令,nginx尝试提供静态文件(或目录),如果没有这样的文件,则返回404
.
With this directive nginx tries to serve a static file (or directory), and returns 404
if there is no such file.
这篇关于如何使用Express和NGINX设置路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文