簡體   English   中英

如何在Node.js Nginx中允許http請求?

[英]How to allow http requests in Node.js Nginx?

我有一個節點應用程序(使用PM2)在運行Ubuntu的DigitalOcean Droplet上監聽http://127.0.0.1:3000 但是,我有一個問題。 一切正常,除了我的所有http帖子請求均收到404 Not Found錯誤。 我不知道為什么。

這是我的Nginx conf文件的樣子:

server {
    listen 0.0.0.0:80;
    root /var/www/app_folder;

    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

它加載一切正常。 靜態圖片,CSS,html,甚至是javascript文件。 但是,我所有傳出的HTTP帖子都是404。

任何幫助將不勝感激。 提前謝謝。

編輯:我的node.js文件與其他文件一樣。 這是總結。

app.post('/someURL', function(req, res) {...}

app.listen(3000, "127.0.0.1");

所以我解決了!

我在DigitalOcean Droplet上運行Ubuntu 14.04。 問題是,當我調用sudo apt-get install nginx ,它將自動安裝NginX版本1.4.6。 但是,最新的NginX穩定版本是1.8.0版。 以下是安裝最新版本的步驟:

  1. 添加NginX PPA sudo add-apt-repository ppa:nginx/stable
    1. 如果add-apt-repository不可用,請執行以下操作:
      1. 對於Ubuntu版本v12.04或更低版本: sudo apt-get install python-software-properties ,然后重新運行第一個命令sudo add-apt-repository ppa:nginx/stable
      2. 對於高於v12.04的Ubuntu版本: sudo apt-get install software-properties-common ,然后重新運行第一個命令sudo add-apt-repository ppa:nginx/stable
  2. 現在運行更新: sudo apt-get update
  3. 最后,安裝NginX: sudo apt-get install nginx

接下來,配置NginX:

  1. 導航到/etc/nginx/
  2. cd sites-available
  3. touch YOUR_APP注意:“ YOUR_APP”應替換為您希望調用Node.js應用的任何內容。
  4. sudo vi YOUR_APP並配置您的Web服務器以正確收聽。

這是Web服務器代碼的示例:

server {  
    server_name your.domain.com;
    listen 80;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://YOUR_APP_LOCAL_IP:YOUR_APP_PORT;
        proxy_redirect off;
    }
}

在您的Node.js文件中,將有一行類似於以下內容: app.listen(3000, "127.0.0.1");
或這個: app.listen(3000);

  • 如果您使用的是第一個版本,請在“ YOUR_APP_LOCAL_IP”中填寫“ 127.0.0.1”或您在node.js文件中使用的版本,並在“ YOUR_APP_PORT”中填寫“ 3000”或您設置的任何端口聽。
  • 如果您使用的是第二個版本,請在“ YOUR_APP_LOCAL_IP”中填寫“ localhost”
    並在“ YOUR_APP_PORT”中填寫“ 3000”或您設置用於監聽的任何端口。

就是這樣! 確保還守護程序。 您可以為此使用PM2之類的東西。 希望這對像我一樣處於類似位置的人有所幫助。

編輯: 是一個很好的鏈接,總結了所有這些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM