我在本地计算机上安装了 nginx 服务器和 node.js。我有以下 Node.js 服务器:
//app.js
var fs = require("fs");
var express = require("express");
var app = express();
//app.use(express.static(__dirname + "/public"));
app.get("/", function(request, response){
var content = fs.readFileSync("index.html");
content = content.toString("utf8").replace("{{TEXT}}", "Home");
response.setHeader("Content-Type", "text/html");
response.send(content);
});
app.get("/hello/:text", function(request, response) {
var content = fs.readFileSync("index.html");
content = content.toString("utf8").replace("{{TEXT}}", request.params.text);
response.setHeader("Content-Type", "text/html");
response.send(content);
});
app.listen(1337, "127.0.0.1");
如果我运行 nginx 服务器,localhost 我会得到 HTML 页面,但“{{TEXT}}””不会被替换。如果我运行 localhost/hello/hi 我收到以下消息:500 内部服务器错误。
因此,在我的文件夹中,有文件 app.js 和另一个名为 public 的文件夹。在public中我有一个index.html文件。
我做错了什么?
此外,这是我的 nginx 配置:
upstream app_nodejs {
server 127.0.0.1:1337;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
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_pass http://app_nodejs;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
}
最佳答案
Nginx 正常运行,但 Node 可能无法正常运行。在大多数情况下,您无法在同一端口上运行两个服务器,这似乎是导致问题的原因。 当文件不是静态时,找出正在运行的端口 Node ,并使 nginx 通过管道连接到该 Node 。
关于javascript - nginx服务器和node.js运行故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23724586/