简体   繁体   中英

nginx server and node.js running faulty

I have nginx server installed and node.js on my local machine. I have the following node.js server:

//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");

If I run nginx server, I get the HTML page,but the "{{TEXT}}"" is not replaced. If I run I get the following message: 500 Internal Server Error. 会显示HTML页面,但不会替换“ {{TEXT}}”,如果运行则会显示以下消息:500 Internal Server Error。

So, in my folder I have the file app.js and another folder called . 文件夹。 In I have an index.html file. 我有一个index.html文件。

What am I doing wrong?

Also, here is my nginx configuration:

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 is running correctly on, but node probably isn't. In most cases, you can't run two servers on the same port, and that seems to be causing the problem. Figure out which port node is running on, and make nginx pipe to it, when the file is not static.

Posted on behalf of OP.

Solved!

In the nginx config, I added a location for the custom route:

location /hello/ {
    location ~* {

proxy_pass http://app_nodejs;

    }
}

Also, for the other routes that are not defined, I caught them in app.js:

app.get('*', function(req, res){
   res.send('Not found! Send the 404.html', 404);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM