nodejs+express+nginx,由服务器向github发出GET请求,访问绑定该服务器的域名504错误

yeahyeahyoV
  • 2
新手上路,请多包涵

环境情况:
1.阿里云服务器
2.centos7系统
3.nodejs + express + nginx
以下是nginx的nginx.conf文件


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.4waifu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://888.88.88.888:8080;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server {
        listen       80;
        server_name  secret.4waifu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://888.88.88.888:3000;
            index  index.html index.htm;
        }     
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

express的app.js

var express = require('express');
var app = express();
var http = require('http');
var qs = require('querystring');
var data = {
  client_id : '7aae72fc409dd7915676',
  scope: 'user%20admin:gpg_key',
  time: new Date().getTime()
};
var content = qs.stringify(data);
var options = {
  path: 'https://github.com/login/oauth/authorize',
  method: 'GET'
};
function httpReq (x) {
  http.request(options, function (res) {
    console.log(res)
    x(res.responseText);
  })
};
app.get('/', function (req, res) {
  console.log('1');
  httpReq(function (x) {
    res.send(x);
  });
});
var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
});

目的:做一个用github账号登录的webapp。
1.webapp通知服务器需要github登录
2.服务器向github发送GET
3.进入github账号登录页面
4.github返回code并跳转回服务器
5.拿返回的code和服务器上的client_secret再发送请求
6.github返回token
7.服务器把token发送给webapp

问题:
1.请问以上1-7的逻辑我的理解有错吗?
2.如果逻辑正确的话,我现在进入secret.4waifu.com就会出现504错误,是什么原因导致的呢?是因为nginx把github的返回挡住了吗?

请各位大佬赐教!

回复
阅读 2.8k
1 个回答

504 是网关超时错误,就是nginx收到了客户端浏览器的请求,要转发到内部web服务器时,内部web服务器没有响应。于是等待一段时间后,nginx就会向客户端浏览器返回"网关超时"错误。

上面代码中

proxy_pass   http://888.88.88.888:3000;

这个地址是不符合ip地址的格式的。

宣传栏