Nginx, Node, Angular - Subfolder API/URL configuration - node.js

I currently have a node/angular app that runs as expected when pointed directly to the port configured (8081 for the purposes of explaining my situation). I'm able to post,get,put,delete as expected.
My goal is to have the node application running at mydomain.com/subfolder. When nginx is configured with the location of '/', everything works as expected. Config below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
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://app_yourdomain/;
proxy_redirect off;
}
}
As soon as I change the location to /subfolder, however, my get,post,put,delete requests return 404 responses. The index.html configured in the node application is returned though. Configuration below:
upstream app_yourdomain {
server 127.0.0.1:8081;
}
server {
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /subfolder {
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://app_yourdomain/;
proxy_redirect off;
}
}
In my angular factory, I have my requests structured like return $http.get('/subfolder'); or return $http.post('/subfolder', {data: data});.
And, within my node application, I have the routes defined like app.get('/subfolder', somefunction); or app.post('/subfolder', somefunction);
Again, when I have the application running from the root of the domain, it works fine. When I have it configured to be in a subfolder of the domain, however, the requests no longer work.
My end goal would be to have multiple node applications running from sub-folders of a main domain. I've been fighting with this for a while, and found several articles for hosting mutliple node apps on a single server, but they seem geared toward having separate domains. I'd like (if possible) for these to run as separate apps for the same domain.
Any thoughts/tricks/pointers? Thanks!

Modify Your Nginx File to look like this:
upstream node{
server 127.0.0.1:3000;
}
listen 0.0.0.0:80;
server_name yourdomain.com yourdomain;
access_log /var/log/nginx/yourdomain.log;
location /node {
rewrite /node(.*) $1 break;
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://node;
proxy_redirect http://node/ /node;
}
I got the above from here: http://skovalyov.blogspot.com/2012/07/deploy-multiple-node-applications-on.html and it works for me

Related

Deployment of an e-learning project based on freecodecamp code

I made a fork of the freecodecamp project on github and modified the design to meet my client's requirements. Locally, everything works fine, but when I deploy online on a digitalocean droplet with Nginx as a proxy, There is a problem that occurs when authenticating with Auth0, the access-token is not sent to the client. Basically, the freecodecamp application uses auth0 to handle all the authentication.
Since everything is working fine locally, I thought that my online Nginx configurations might be the problem.
I created two configuration files on Nginx, one for the client and one for the api.
The configuration file for the api has the following content:
server {
listen 80;
listen [::]:80;
root /var/www/html/freeCodeCamp;
server_name my_domain_name.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_redirect http://localhost:3000/ http://$server_name/;
}
}
The configuration file for the client has the following content:
server {
listen 80;
listen [::]:80;
root /var/www/html/freeCodeCamp;
server_name my_domain_name.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost:8000/ http://$server_name/;
}
}
I would like to have your opinion on the subject. Thanks in advance.

Nginx "Cannot GET /index.html" on proxy_pass to working domain

Let me jump right in to it: I'm trying to proxy_pass my base URL (test.co) to another domain (webflow.test.co). But, to put it simply, I'm having trouble. I've solved a lot of the issues so far, but am stuck here again. The domain (webflow.test.co) is working fine, so no issues there... but when I try to use the proxy_pass (shown below) i get an error in-browser of "Cannot GET /index.html".
A look at Chrome Dev Tools shows that the redirect to webflow.test.co isn't happening (I think, I'm no expert with this area of the devtools) and maybe isn't able to retrieve index.html for that reason.
Here's my nginx config file (I'm routing all requests other than the home to my NodeJS server):
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
# Redirect all non-HTTPS to non-WWW HTTPS
server {
listen 8080;
server_name "~^(?:www\.)?(.*)$";
return 301 https://$host$request_uri;
}
# Redirect WWW HTTP to non-WWW HTTP
server {
listen 4430;
server_name "~^www\.(.*)$";
return 301 https://$1$request_uri;
}
server {
location / {
proxy_pass https://webflow.test.co;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
}
# Reverse-proxy to http://nodejs
server {
listen 4430;
server_name "~^(?!www\.).*$";
client_max_body_size 50M;
location ~ /(?<all>.+) {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Any help is greatly, greatly appreciated. I'll accept the first answer that works and is sensible. Thanks so much!
EDIT 1: I just tested without using SSL at all, and the response is the same - Cannot GET /index.html, while the actual domain webflow.test.co works fine.

Handling CORs inside of node behind nginx

At the moment I'm handling CORs within node.js which seems to be working pretty well; I can setup multiple domains and hit my node.js code just fine.
The trouble is once I put node.js inside of forever and run a proxy from nginx to my application it no longer handles any CORs requests. In fact they never even make it to the server and I'm having trouble figuring out why. Below is my nginx configuration; any suggestions? If it isn't obvious I'm trying to hit the services.org from the webapp.org. In this environment they're on the same box but in other environments and when consumed by third parties we need CORs working.
server {
listen 80;
server_name webapp.org;
location / {
proxy_pass http://localhost:3000/Site/;
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;
}
}
server {
listen 80;
server_name services.org;
location / {
proxy_pass http://localhost:3000;
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;
}
}
Try 127.0.0.1 instead of localhost in the nginx.conf
Try adding an upstream server like this:
upstream my_app {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name webapp.org;
access_log /var/log/nginx/my_app.log;
# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
root /var/www/my_app/public;
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_redirect off;
if (!-f $request_filename) {
proxy_pass http://my_app;
break;
}
}
}
You may need to enable CORS for the service via:
app.enableCors() in your bootstrap() function in main.ts

Serving multiple node apps with nginx on same domain

I would like to host 2 different node applications with nginx from the same domain and am having some trouble. I would like to have:
mydomain.com point to node app firstApp and otherapp.mydomain.com point to node app otherapp
Right now, I can access firstApp just fine, but I cannot access otherapp via otherapp.mydomain.com.
My config for firstApp looks like this:
upstream firstApp{
server 127.0.0.1:8123;
}
server{
server_name mydomain.com;
access_log /var/log/nginx/me.log;
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://firstApp/;
proxy_redirect off;
}
}
My config for otherapp looks like this:
upstream otherapp{
server 127.0.0.1:8124;
}
server{
server_name otherapp.mydomain.com;
access_log /var/log/nginx/me.log;
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://otherapp/;
proxy_redirect off;
}
}
I have created both configurations in the nginx sites-available directory, they are both linked in the sites-enabled directory, and I have restarted nginx. Can someone tell me what I'm doing wrong?
Thanks,
Swaraj
Just found out what the problem was. Though my nginx configs were correct, I had not added my desired subdomain to my domain name provider (namecheap). I added my subdomain on namecheap, and everything is working correctly now.
you should config your nginx file like this
server {
listen 80;
server_name biger.yourdomain.cn;
access_log /data/log/nginx/access_ab.log;
error_log /data/log/nginx/error_ab.log;
location /firstApp {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:8001/;
}
}
maeby you need add this code to your project
app.enable('trust proxy');
I was facing the same problem, after spending time on research I wrote a blogpost where I explained with details how I solved it, I hope it helps. Here it is: http://blog.donaldderek.com/2013/08/cf-i-configure-your-staging-machine-with-node-js-and-nginx/

using nginx to serve multiple apps from unique IPs

I've got two IPs associated with my VPS, and am trying to set this up to serve two node apps. Here's my configuration:
In /etc/nginx/sites-enabled/domain1:
upstream app_domain1 {
server 127.0.0.1:4000;
}
server {
listen 0.0.0.0:80;
server_name IP1.xxx.xxx.xxx;
access_log /var/log/nginx/domain1.log;
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-Nninx-Proxy true;
proxy_pass http://app_domain1/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled/domain2
upstream app_domain2 {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name IP2.xxx.xxx.xxx;
access_log /var/log/nginx/domain2.log;
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-Nninx-Proxy true;
proxy_pass http://app_domain2/;
proxy_redirect off;
}
}
And in /etc/nginx/sites-enabled, I ran:
ln -s /etc/nginx/sites-available/domain1 domain1
ln -s /etc/nginx/sites-available/domain2 domain2
Now, when I go to /var/www/domain1 and run "node app.js" on the correct port, I can visit the relevant IP address and see the app running, but the same is not true for domain2 (I checked that it's running on the correct port for this config. The request just times out - no response at all.
So how can I troubleshoot this?
Update:
If I go to the ports directly, I see both apps available on both IPs, so:
IP1.xxx.xxx.xxx:4000 gives me the app for domain1
IP1.xxx.xxx.xxx:3000 gives me the app for domain2
and
IP2.xxx.xxx.xxx:4000 gives me the app for domain1
IP2.xxx.xxx.xxx:3000 gives me the app for domain2
So it's treating each IP address as the same.
server_name accepts only domains but not IP addresses.
You have typo mistake in proxy headers, change this:
proxy_set_header X-Nninx-Proxy true;
with this:
proxy_set_header X-NginX-Proxy true;

Resources