Node.js/Ubuntu 20 服务器配置错误:111 连接被拒绝

Node.js/Ubuntu 20 服务器配置错误:111 连接被拒绝

当我刷新页面时,出现以下三行错误:

2020/10/07 16:38:55 [error] 315270#315270: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 66.249.66.221, server: akadjg.com, request: "GET /robots.txt HTTP/1.1", upstream: "http://127.0.0.1:3000/robots.txt", host: "akadjg.com"
2020/10/07 16:38:58 [error] 315270#315270: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 71.218.84.112, server: akadjg.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "akadjg.com"
2020/10/07 16:38:58 [error] 315270#315270: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 71.218.84.112, server: akadjg.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3000/favicon.ico", host: "akadjg.com", referrer: "http://akadjg.com/"

运行“netstat -plant”得到以下结果:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      315140/mongod       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      -                   
tcp        0      0 157.245.172.144:42856   172.67.168.253:80       TIME_WAIT   -                   
tcp        0    612 157.245.172.144:22      71.218.84.112:52609     ESTABLISHED -                   
tcp        0      0 157.245.172.144:80      66.249.66.93:37947      TIME_WAIT   -                   
tcp        0      0 157.245.172.144:22      71.218.84.112:51914     ESTABLISHED -                   
tcp        0      0 127.0.0.1:27017         127.0.0.1:55302         ESTABLISHED 315140/mongod       
tcp        0      0 127.0.0.1:55302         127.0.0.1:27017         ESTABLISHED 315186/node /home/d 
tcp6       0      0 :::80                   :::*                    LISTEN      -                   
tcp6       0      0 :::8080                 :::*                    LISTEN      315186/node /home/d 
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 :::443                  :::*                    LISTEN      -                

在 /etc/nginx/sites-available/akadjg.com 中我有:

server {
    listen 80;
    listen [::]:80;

    root /var/www/akadjg.com/html;
    index index.html index.htm index.nginx-debian.html;

    server_name akadjg.com www.akadjg.com;

    location / {
        proxy_pass http://localhost:3000;
        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;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/akadjg.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/akadjg.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

我哪里做错了?几年前,我在 Ubuntu 14 上运行过这台服务器;这次迭代是在 Ubuntu 20 上。我认为它的配置相同,但也许我在按照 DigitalOcean 上的设置教程时做错了什么。

哦,我的 server.js 文件是:

var express = require('express') //Require express for middleware use
const app = express()
var http = require('http').createServer(app)
var io = require('socket.io')(http) //IO is the server

var bodyParser = require('body-parser')

var MongoClient = require('mongodb').MongoClient
var assert = require('assert')
var mongoose = require('mongoose')
mongoose.set('debug', true)

var url = 'mongodb://localhost:27017/test'

//BASICS
app.use(express.static(__dirname + "/served")) //Serve static files

app.get('/', function(req, res){
  res.sendFile('./index.html');
});

io.on('connection', function(socket){
  // socket.on('chat message', function(msg){
    // io.emit('chat message', msg);
  // });
  console.log("A user connected: ", socket.id)
});

http.listen(8080, function(){
  console.log('listening on *:8080');
});

//HANDLE USER LOGIN:
app.use(bodyParser.json())       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded()) // to support URL-encoded bodies

mongoose.connect(url)
var db = mongoose.connection
db.on('error', console.error)
var User
db.once('open', function() {
  var userSchema = mongoose.Schema({
    username: String,
    level: Number,
    onePoRank: Number,
    twoPoRank: Number,
    type: String,
    tutDone: Boolean
  })

  // userSchema.statics.findOne = function(id, callback){
  //   return this.findOne({ _id: new RegExp(id, 'i') }, callback);
  // }

  User = mongoose.model('User', userSchema)

  // db.close()
})

app.post('/menu', function(req, res){ // Save new user or load existing one, and send that data back to the client.
  var username = req.param('username')
  console.log('username "'+username+'" parsed from form')

  //check the database for a user with that name:
  User.findOne({username: username}, function(err, user){
    if (err) {console.log("The user search caused an error")}
    if (user === null) {
      console.log("No user by name %s found--creating new user", username)
      var newUser = new User({username: username, level:0, onePoRank:0, twoPoRank: 0, type: 'human'})
      newUser.save(function (err, newUser) { //Add the new user to the database
        if (err) return console.error(err);
        console.log("New user %s saved to database", username)
        res.json(newUser)
      })
    } else { //User was found in the database
      console.log("User %s found.", username)
      res.json(user)
    }
  })

  db.once('close', function(){console.log("database closed.")})
  // res.sendFile('./menu.html', {root: __dirname+"/served"})
});

app.post('/user', function(req, res){ // Update the user in the database:
  var what = req.param('what')
  var username = req.param('user').username
  // var username = req.param('username')
  // console.log('username "'+username+'" parsed from form')

  User.findOne({username: username}, function(err, user){
    if (err) {console.log("The user search caused an error")}
    if (user === null) {
      console.log("No user by name %s found. That was unexpected.", username)
    } else {
      console.log("User %s found. Updating.", username)
      user[what] = req.param('user')[what]
      user.save(function(err) {
        if (err) throw err
        console.log('User successfully updated!')
      });
    }
  })

  res.json({'a':0}) //just to prevent an empty response error
});

答案1

我必须在 /etc/nginx/sites-available/akadjg.com 中将 3000 替换为 8080。

相关内容