【问题标题】:Nodejs not running in serverNodejs没有在服务器中运行
【发布时间】:2018-10-08 02:04:17
【问题描述】:

我正在尝试在我的服务器中启动 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's answer

我正在尝试在我的 xx.xx.xx.xx 服务器中运行 node.js。现在它只是在http://xx.xx.xx.xx/This site can’t be reached xx.xx.xx.xx refused to connect. ERR_CONNECTION_REFUSEDhttp://xx.xx.xx.xx:8080/ 显示this

【问题讨论】:

    标签: node.js nginx server centos centos-6.9


    【解决方案1】:

    您必须提及端口号即未使用才能收听。

    做一些这样的事情----

    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....它可以工作...

    【讨论】:

    • 我也应该把 8083 放在 nginx.conf 文件中吗?
    • 是的,您必须更改指定端口号的位置。
    • 它没有用。上面相同的代码在我的电脑-centos7 中完美运行。在我的服务器 centos6.9 中不工作
    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2019-11-10
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多