Nodejs не работает на сервере - PullRequest
0 голосов
/ 27 апреля 2018

Я пытаюсь запустить nodejs на моем сервере. это мой код node.js:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, 'xx.xx.xx.xx');
console.log('Server running at http://xx.xx.xx.xx:8080/');

это мой /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;


events {
    worker_connections  1024;
}


http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server{
        location / {
            proxy_pass http://xx.xx.xx.xx:8080;
            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;
        }
    }
}
#   

Я также удалил несколько строк из default.conf, потому что он отображал ошибку nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) и следовал за @ ответом rednaw

Я пытаюсь запустить node.js на моем xx.xx.xx.xx сервере. Теперь он просто показывает это в http://xx.xx.xx.xx/ и This site can’t be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSED в http://xx.xx.xx.xx:8080/

1 Ответ

0 голосов
/ 27 апреля 2018

Вы должны указать номер порта , т.е. он не используется для прослушивания.

Сделай что-нибудь подобное ----

var http =  require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8083);

В моем сценарии номер порта 8080 уже использовался .... Поэтому я использовал

другой номер порта, например 8083 .... Это работает ...

...