Nginx proxy with node. weird rewrites when ran on port 80 - node.js

location /api{
rewrite /api(.*) $1 break;
proxy_pass http://api;
}
so I have a node server running inside a container and using Nginx as a proxy it works fine if the container listens on any other port than 80. But when on 80 it only rewrites/reroutes two routs /api/v1/vendors and /api/v1/products to /v1/vendors/ and /v1/products/. but all other routes work fine. and the issue is fixed when I append a trailing / to /api/v1/vendors and /api/v1/products. can anyone explain why this might be happening? and it also fixes itself when I change the name of the routes even taking the s off works
you can try this behavior on https://api.oroshops.com/api/v1/vendors and https://api.oroshops.com/api/v1/vendors/
you can try this behavior on https://api.oroshops.com/api/v1/users and https://api.oroshops.com/api/v1/users/

Related

need help on nginx configuration for nodejs api

my node api starts on port 1337 and works fine. if I browse localhost:1337 my api return nothing and if I browse localhost:1337/products my api actually works fine and return my products list as Json.
now service nginx installed and configured well to reverse-proxy of localhost:1337 to localhost
so after opening localhost it somehow works and says nothing as before but if I try browsing localhost/products again says nothing
and don't care about products.
I just have found the issue.
in nginx default configuration removed everything in section location /api/ just copy proxy_pass http://localhost:1337/; lonely. and it works correctly.

Where do I put my Node JS app so it is accessible via the main website?

I've recently installed a nodejs app (keystone) app in my home/myusername/myappname directory.
When I visit www.mydomain.com, nothing displays - even after turning on my nodejs app.
Where should these files be?
I am running ubuntu 16.04.
In the past I have worked with a var/www folder, but I am not using apache - do I need to manually create this folder?
Thanks!
For your app to be visible it has to be running (obviously) and accessible on port 80 (if you want it to be available without adding a port number to the URL).
It doesn't matter where it is on the disk as long as it's running.
You don't need Apache or nginx or any other server. Your Node app may listen on port 80. But alternatively it can listen on some other port and your other server (Apache, nginx, etc.) can proxy the requests to that port.
But if your app is listening on, e.g. port 3000 then you should be able to access it as http://www.example.com:3000/.
Also, make sure that your domain is configured correctly. It's A record for IPv4 (or AAAA for IPv6) of the www subdomain should be equal to the publicly accessible IP address of your server.
And make sure that the port you use is not blocked by the firewall.
Update
To see how you can set the port with Keystone, see:
http://keystonejs.com/docs/configuration/#options-server
It can be either changed in the config or you can run your app with:
PORT=80 node yourApp.js
instead of:
node yourApp.js
but keep in mind that to use the port number below 1024 you will usually need the program to run as root (or add a special privilege which is more complicated).
It will also mean that this will be the only application that you can run on this server, even if you have more domain names.
If you don't want to run as root or you want to host more application, it is easiest to install nginx and proxy the requests. Such a configuration is called a "reverse proxy" - it's good to search for info and tutorials using that phrase.
The simplest nginx config would be something like this:
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://localhost:3000;
}
}
You can set it in:
/etc/nginx/sites-available/default
or in a different file as e.g.:
/etc/nginx/sites-available/example
and then symlinked as /etc/nginx/sites-enabled/example
You need to restart nginx after changing the config.
You can find more options on configuring reverse proxies here:
https://www.nginx.com/resources/admin-guide/reverse-proxy/
You need to make a proxy between Apache and your Node.js application because Node.js has a built-in server. Supose your Node.js app is served on 9000 port. Then you need to make a proxy to redirect all trafic in 80 port to 9000 port where the Node.js app is running.
1. Enable mod_proxy
You can do this through a2enmond.
sudo a2enmod proxy
sudo a2enmod proxy_http
2. Set the proxy
Edit the /etc/apache2/sites-available/example.com.conf file and add the following lines:
ProxyRequests Off
Order deny, allow from All
ProxyPass / http://0.0.0.0:9000 ProxyPassReverse / http://0.0.0.0:9000
This basically say: "Redirect all traffic from root / to http://0.0.0.0:9000. The host 0.0.0.0:9000 is where your app is running.
Finally restart apache to enable changes.

node.js application running on a port should have no port visible in the URL bar

I have 5 domains pointing to the same machine, let's say these domains are:
example1.com
example2.com
example3.com
example4.com
example5.com
My server has a virtual host file managed by apache, which controls these 5 domains.
I have a node.js app running on port 3000.
If I connect to any of the 5 domains, and type :3000 at the end, I can see the node.js app splash screen.
For example, if I connect to example1.com:3000 it serves me the same page (the node.js app) that can be reached by connecting to example2.com:3000 and example3.com:3000, etc. I understand why this is happening.
However, I am trying to edit my configuration such that the following criteria are met:
Users can only connect to the node app when they type in example5.com, and not any of the other domains.
Users do not have to type in the port number, example5.com:3000, when trying to reach the node app.
I have searched and found some information which leads me to believe this is normally achieved by setting up a reverse proxy, but most examples I find are using nginx or node. It would be nice to figure out a reverse proxy solution with apache so I don't have to re-write all my virtual host logic with node or nginx.
You need to set up a reverse proxy. An easy way to do this is to use nginx. Install and start nginx and in your config file put the following:
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://localhost:3000;
}
}
And then run your node server on port 3000.

Nginx and multiple domains

I bought some domains at godaddy.com (i.e mydomain.com) for my droplet at digitalocean.com (i.e 199.216.110.210). I run a nodejs application on port 80 on the droplet. From godaddy.com, I forward with masking mydomain.com to 199.216.110.210 and I could see may app.
Now I want to run on 199.216.110.210 several node applications on different ports, using ngnix as reverse proxy. I followed the instructions here (www.digitalocean.com/community/articles/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab).
My nginx .conf file is
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://localhost:3000;
# same as in the link above
}
}
(and I am sure it is read: when ngnix start if I put an error there, ngnix reports it).
I start the nodejs application on port 3000:
I try mydomain.com, but ngnix shows always the welcome page.
Also doing mydomain.com: 3000 does not work,
it works only with 199.216.110.210:3000.
From godaddy.com, if I forward with masking the mydomain.com to 199.216.110.210:3000 I can see may app.
But I do not like this solution. I would like domains pointing to my droplet, without specifing the port and admin them with nginx.
How can I get a domain name to use with ngnix as reverse proxy to select my apps, mapped on different domains on different ports? I suppose that forwarding from godaddy.com is somehow limited.
In your server go to /var/log/nginx and do a tail -F *log. Now in another shell restart nginx.
I suspect that your domain name is too long and nginx will complain about its hash_bucket_size is too small. If this is the case open /etc/nginx/nginx.conf and make sure that the line
server_names_hash_bucket_size 64;
exists, has a value of 64 and is uncommented. Then do sudo service nginx reload, and check if all works as expected.
I am going to detail step by step how I am able to do it in my aws ec2 instance;
I set up a DNS record to my instance, so i can set mydomain.com to 192.168.123.123 (my specific IP).
Inside my instance I have forever running my node.js app in port 3000 (I test it work by issuing curl localhost:3000 from the command line)
I then download this .sh file in order to properly intantiate nginx; curl -o nginxStarter.sh https://gist.githubusercontent.com/renatoargh/dda1fbc854f7957ec7b3/raw/c0bc1a1ec76e50cdb4336182c53a0b222edb6c0e/start.sh
I configure nginx with this configuration file. Put this file in; /etc/nginx/nginx.conf
Start nginx with this command; sudo sh nginxStarter.sh start
PS.: For multiple apps just replicate the lines that routes the requests to specific ports, very easy...! If not needed you can eliminate lines regarding out SSL.

Can't get to my nodejs server through web browser

Alright, so I setup a node.js server quite a while ago on a AWS EC2 micro server. I was completely new to it and followed various tutorials to get it up and running. It used nginx as a reverse proxy (I believe) and the server was listening on port 8124.
Now, the instance got restarted and I can't for the life of me get access to my server back. I can ssh to it. I can start the server. I can send POST/PUT requests to it through my local command line, but my web browser gives me the 404 nginx page.
This is driving me up the wall - where in the browser/nginx/nodejs chain are things breaking down?
Please help - I'm horribly new at this at it must be a single line somewhere that's broken. I just don't know enough to find it.
My /etc/nginx/sites-enables/default file simply contains
location / {
proxy_pass http://127.0.0.1:8124/;
}
Okay I figured it out. I had to go directly into /etc/nginx/nginx.conf and in the server that was there
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
I added the line
proxy_pass http://127.0.0.1:8124/;
Oh thank god. That was going to kill me.

Resources