简体   繁体   English

如何在Node.js Nginx中允许http请求?

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

I have a node app (using PM2) listening on http://127.0.0.1:3000 on my DigitalOcean droplet running Ubuntu. 我有一个节点应用程序(使用PM2)在运行Ubuntu的DigitalOcean Droplet上监听http://127.0.0.1:3000 However, I have a problem. 但是,我有一个问题。 Everything works fine except for the fact that all of my http post requests recieve a 404 Not Found error. 一切正常,除了我的所有http帖子请求均收到404 Not Found错误。 I have no idea why. 我不知道为什么。

Here is what my Nginx conf file looks like: 这是我的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;
    }
}

It loads everything fine. 它加载一切正常。 Static images, CSS, html, and even the javascript files. 静态图片,CSS,html,甚至是javascript文件。 However, all my outgoing HTTP posts are 404. 但是,我所有传出的HTTP帖子都是404。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks ahead of time. 提前谢谢。

EDIT: My node.js file is like any other. 编辑:我的node.js文件与其他文件一样。 Here's the rundown. 这是总结。

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

app.listen(3000, "127.0.0.1");

So I solved it! 所以我解决了!

I was running Ubuntu 14.04 on my DigitalOcean Droplet. 我在DigitalOcean Droplet上运行Ubuntu 14.04。 The problem was that when i called sudo apt-get install nginx , it would install NginX version 1.4.6 automatically. 问题是,当我调用sudo apt-get install nginx ,它将自动安装NginX版本1.4.6。 However, the latest stable release of NginX is version 1.8.0. 但是,最新的NginX稳定版本是1.8.0版。 Here are the steps to installing the latest version: 以下是安装最新版本的步骤:

  1. Add NginX PPA sudo add-apt-repository ppa:nginx/stable 添加NginX PPA sudo add-apt-repository ppa:nginx/stable
    1. If add-apt-repository isn't available, then do the following: 如果add-apt-repository不可用,请执行以下操作:
      1. For Ubuntu version v12.04 or lower: sudo apt-get install python-software-properties , then re-run the first command sudo add-apt-repository ppa:nginx/stable 对于Ubuntu版本v12.04或更低版本: sudo apt-get install python-software-properties ,然后重新运行第一个命令sudo add-apt-repository ppa:nginx/stable
      2. For Ubuntu version greater than v12.04: sudo apt-get install software-properties-common , then re-run the first command sudo add-apt-repository ppa:nginx/stable 对于高于v12.04的Ubuntu版本: sudo apt-get install software-properties-common ,然后重新运行第一个命令sudo add-apt-repository ppa:nginx/stable
  2. Now run an update: sudo apt-get update 现在运行更新: sudo apt-get update
  3. Finally, install NginX: sudo apt-get install nginx 最后,安装NginX: sudo apt-get install nginx

Next, configure NginX: 接下来,配置NginX:

  1. Navigate to /etc/nginx/ 导航到/etc/nginx/
  2. cd sites-available
  3. touch YOUR_APP NOTE: "YOUR_APP" should be replaced with whatever you wish to call your Node.js app. touch YOUR_APP注意:“ YOUR_APP”应替换为您希望调用Node.js应用的任何内容。
  4. sudo vi YOUR_APP and configure your web server to listen correctly. sudo vi YOUR_APP并配置您的Web服务器以正确收听。

Here is a sample of the web server code: 这是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;
    }
}

In your Node.js file, there will either be a line similar to this: app.listen(3000, "127.0.0.1"); 在您的Node.js文件中,将有一行类似于以下内容: app.listen(3000, "127.0.0.1");
or this: app.listen(3000); 或这个: app.listen(3000);

  • If you have the first version, fill in "YOUR_APP_LOCAL_IP" with "127.0.0.1" or whatever you have in place of that in your node.js file, and fill in "YOUR_APP_PORT" with "3000" or whatever port you have it set to listen to. 如果您使用的是第一个版本,请在“ YOUR_APP_LOCAL_IP”中填写“ 127.0.0.1”或您在node.js文件中使用的版本,并在“ YOUR_APP_PORT”中填写“ 3000”或您设置的任何端口听。
  • If you have the second version, fill in "YOUR_APP_LOCAL_IP" with "localhost" 如果您使用的是第二个版本,请在“ YOUR_APP_LOCAL_IP”中填写“ localhost”
    and fill in "YOUR_APP_PORT" with "3000" or whatever port you have it set to listen to. 并在“ YOUR_APP_PORT”中填写“ 3000”或您设置用于监听的任何端口。

That should be it! 就是这样! Make sure to daemonize the app as well. 确保还守护程序。 You can use something like PM2 for that. 您可以为此使用PM2之类的东西。 Hope this helps anyone who was in a similar position like me pulling their hair out. 希望这对像我一样处于类似位置的人有所帮助。

EDIT: Here is a good link that summarizes all of this. 编辑: 是一个很好的链接,总结了所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM