javascript - How to allow http requests in Node.js Nginx? -
i have node app (using pm2) listening on http://127.0.0.1:3000 on digitalocean droplet running ubuntu. however, have problem. works fine except fact of http post requests recieve 404 not found error. have no idea why.
here 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 fine. static images, css, html, , javascript files. however, outgoing http posts 404.
any appreciated. ahead of time.
edit: node.js file other. here's rundown.
app.post('/someurl', function(req, res) {...} app.listen(3000, "127.0.0.1");
so solved it!
i running ubuntu 14.04 on digitalocean droplet. problem when called sudo apt-get install nginx
, install nginx version 1.4.6 automatically. however, latest stable release of nginx version 1.8.0. here steps installing latest version:
- add nginx ppa
sudo add-apt-repository ppa:nginx/stable
- if
add-apt-repository
isn't available, following:- for ubuntu version v12.04 or lower:
sudo apt-get install python-software-properties
, re-run first commandsudo add-apt-repository ppa:nginx/stable
- for ubuntu version greater v12.04:
sudo apt-get install software-properties-common
, re-run first commandsudo add-apt-repository ppa:nginx/stable
- for ubuntu version v12.04 or lower:
- if
- now run update:
sudo apt-get update
- finally, install nginx:
sudo apt-get install nginx
next, configure nginx:
- navigate
/etc/nginx/
cd sites-available
touch your_app
note: "your_app" should replaced whatever wish call node.js app.sudo vi your_app
, configure web server listen correctly.
here sample of 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 node.js file, there either line similar this: app.listen(3000, "127.0.0.1");
or this: app.listen(3000);
- if have first version, fill in "your_app_local_ip" "127.0.0.1" or whatever have in place of in node.js file, , fill in "your_app_port" "3000" or whatever port have set listen to.
- if have second version, fill in "your_app_local_ip" "localhost"
, fill in "your_app_port" "3000" or whatever port have set listen to.
that should it! make sure daemonize app well. can use pm2 that. hope helps in similar position me pulling hair out.
edit: here link summarizes of this.
Comments
Post a Comment