I have a node app (using PM2) listening on http://127.0.0.1:3000 on my DigitalOcean droplet running Ubuntu. 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. I have no idea why.
Here is what my Nginx conf file looks like:
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. However, all my outgoing HTTP posts are 404.
Any help would be greatly appreciated. Thanks ahead of time.
EDIT: My node.js file is like any other. 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. The problem was that when i called sudo apt-get install nginx
, it would install NginX version 1.4.6 automatically. However, the latest stable release of NginX is version 1.8.0. Here are the steps to installing the latest version:
sudo add-apt-repository ppa:nginx/stable
add-apt-repository
isn't available, then do the following:
sudo apt-get install python-software-properties
, then re-run the first command 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 update
sudo apt-get install nginx
Next, configure 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. sudo vi YOUR_APP
and configure your web server to listen correctly. Here is a sample of the web server code:
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");
or this: app.listen(3000);
That should be it! Make sure to daemonize the app as well. You can use something like PM2 for that. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.