2016-04-29 20 views
0

我在centos7(aws的ec2-instance)上安裝了nginx + mysql + nodejs。 一個簡單的節點應用程序工作正常。它可以通過捲曲和通過網絡瀏覽器訪問。但一個簡單的快速應用程序不能通過webbrowser工作(響應502)。它只能通過捲曲http://MY-PRIVATE-IP:8080。 在我本地安裝的服務器上,每個人都認爲沒問題。 注意:我沒有安裝express express。可能是米。錯誤?nginx上的Express.js應用程序

有什麼想法?我會很感激你的想法。感謝名單。

這裏是我的nginx的配置的詳細信息節點的應用程序:

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: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應用: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!'); 
}); 

回答

0

嘗試將server_name指令更改爲您用於訪問您的應用程序的url,以便Nginx知道重定向您的請求的位置。

如果失敗,請檢查您的訪問和錯誤日​​志,以獲取有關在您的訪問嘗試期間執行的Nginx操作的更多詳細信息。

+0

更改server_name 52.38.xx.xx後,不幸地訪問失敗,並顯示此日誌: 79.248.x.xx - - [30/Apr/2016:20:15:57 +0000]「GET/myapp HTTP/1.1 「404 19」 - 「」Mozilla/5.0(Windows NT 6.1; WOW64; rv:45.0)Gecko/20100101 Firefox/45.0「」 - 「 – Asman

+0

服務器名稱應該是您的url,而不是服務器的IP地址(除非您使用當試圖訪問您的網站,這是不推薦的IP)。您是否嘗試過將proxy_pass指令指向端口8080,應用程序正在偵聽? –