简体   繁体   中英

Express.js app on nginx

Ich have installed nginx+mysql+nodejs on centos7 (ec2-instance from aws). A simple node app works fine. It can be accessed both via curl and via the web browser. But a simple express app does not work via webbrowser (response 502). It works only via curl http://MY-PRIVATE-IP:8080 . On my local installed server every think is fine. Note: I did not installed express global. May be that's m. mistake?

Any idea? I would appreciate your ideas. Thanx.

here is details about my nginx configuration an the node apps:

nginx.conf

user                nginx;
worker_processes    2;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

#worker_rlimit_nofile 30000;

events {
    worker_connections  1024;
}


http {
    index index.php index.htm index.html;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    gzip                on;

    #include /etc/nginx/conf.d/*.conf;

    upstream node_upstream  {
       server MY-PRIVATE-IP:8080;
    }

    server_tokens off;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /var/www/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        location /myapp {
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_pass http://node_upstream;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

nodejs app : hallo.js

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hallo Welt\n');
}).listen(8080, 'MY-PRIVATE-IP');

console.log('Server running at http://MY-PRIVATE-IP:8080/');

expressjs app : hello.js

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

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(8080, function () {
  console.log('Example app listening on port 8080!');
});

Try changing the server_name directive to the url you are using to access your app, so that Nginx would know where to redirect your request.

If this fails, check your access and error logs for more details about Nginx operations being performed during your access try.

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