[英]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:
以下是安装最新版本的步骤:
sudo add-apt-repository ppa:nginx/stable
sudo add-apt-repository ppa:nginx/stable
add-apt-repository
isn't available, then do the following: add-apt-repository
不可用,请执行以下操作:
sudo apt-get install python-software-properties
, then re-run the first command sudo add-apt-repository ppa:nginx/stable
sudo apt-get install python-software-properties
,然后重新运行第一个命令sudo add-apt-repository ppa:nginx/stable
sudo apt-get install software-properties-common
, then re-run the first command sudo add-apt-repository ppa:nginx/stable
sudo apt-get install software-properties-common
,然后重新运行第一个命令sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get update
sudo apt-get install nginx
sudo apt-get install nginx
Next, configure NginX: 接下来,配置NginX:
/etc/nginx/
/etc/nginx/
cd sites-available
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应用的任何内容。 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);
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.