Node JS and Nginx routing - node.js

I'm having a little bit of trouble rerouting my server:80 to my node js app running in port 9999.
I have my nginx setup like so:
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://principal-domain.com:9999/;
}
location ~/\.ht {
deny all;
}
}
But obviously only / is being rerouted. If I make a request to /api I get a 502 Bad Gateway.
Anyone knows how to fix this? Apply the rerouting for all routes in mydomain.com?

Related

Nginx reverse proxy to node application API

I've got an NGINX reverse proxy on my server handling requests for http://apcompdoc.com. It listens on port 80, and can successfully return the Vue Dist, however, I have a backend node API running on port 8081 and another node process running on port 8082. The user never directly requests anything on 8082, but rather the process on 8081 sometimes requests the process on 8082, so I'm assuming I never have to even expose that to Nginx at all, but I'm not too sure. However, the main problem is that the API is never reached I believe. I have it so that when you hit the endpoint http://apcompdoc.com/api/* it should proxy over to the node process. I am using PM2 to keep the process alive and monitor it, and am assured it's running. This is my NGINX apcompdoc.com config file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name: apcompdoc.com www.apcompdoc.com;
charset utf-8;
root /var/www/apcompdoc/dist;
index index.html index.htm;
# Always serve index.html for any request;
location / {
root /var/www/apcompdoc/dist;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:8081;
}
error_log /var/log/nginx/vue-app-error.log;
access_log /var/log/nginx/vue-app-access.log;
}
I am trying to get all requests to my API at /api/* to get redirected to the API at localhost:8081 and then returned to the user. I saw something about redirecting the proxy back, do I have to do that? I also don't know if I have to do /api/* in the NGINX config file.
I'm really new to NGINX but I just want the requests to http://apcompdoc.com/api/* to be redirected to the node process on port 8081.
Bad or good practice, I'm not sure, but I always defining my backend as upstream.
For example, your file will look like this:
upstream nodeprocess {
server localhost:8081;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name: apcompdoc.com www.apcompdoc.com;
charset utf-8;
root /var/www/apcompdoc/dist;
index index.html index.htm;
# Always serve index.html for any request;
location / {
root /var/www/apcompdoc/dist;
try_files $uri /index.html;
}
location ^~ /api {
proxy_pass http://nodeprocess;
}
error_log /var/log/nginx/vue-app-error.log;
access_log /var/log/nginx/vue-app-access.log;
}
Please note I added ^~ in the location of the api and removed the trailing /

Node.js app and Apache php back-end at the same server

I have a VPS which runs under CentOS 7.
The idea is: to have under maindomain.com node.js front-end app deployed while under api.maindomain.com to have php back-end deployed. Is it possible? Say, add server blocks to Nginx: reverse proxy localhost:4000 for node.js app and the other block for localhost:80 for php back-end.
Maybe there exists the other solution, I don't know, I would appreciate any ideas! The main goal: to have both app at the same server.
Solution 1 with www.maindomain.com + api.maindomain.com
Frontend
server {
listen 80;
server_name www.maindomain.com;
location / {
root /path/to/your/files;
try_files /index.html;
}
}
Backend php API
server {
listen 80;
server_name api.maindomain.com;
location / {
proxy_pass http://localhost:4000;
}
}
Solution 2 everything on same domain, www.maindomain.com
server {
listen 80;
server_name www.maindomain.com;
location /api {
proxy_pass http://localhost:4000/api;
}
location / { # always at the end, like wildcard
root /path/to/your/files;
try_files /index.html;
}
}

Configuring Multiple Nodejs Apps to nginx conf

How to configure multiple virtual hosted nodejs apps on same machine with different port to nginx conf? I tried location tag with different proxy_pass but it throws no css js error on one location and on another it’s get method error..
Please help me out. thanks!!
You can do it simple like this for instance:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name yourotherdomain.com;
location / {
proxy_pass http://localhost:3001;
}
}

Nginx pass proxy to another nodejs server

Node js part:
app.listen(4000,"localhost");
Nginx default:
server{
server_name "mydomain";
location /{
proxy_pass http://localhost:4000;
}
}
When i set it with localhost it's working.
When i run the node script on another server and set nginx configuration like this:
server{
server_name "mydomain";
location /{
proxy_pass http://192.168.1.30:4000;
}
}
This is not working. I'm getting 502 Bad Gateway error when i try to connect "mydomain".
I've solved the problem with changing the node script.
app.listen(4000,"localhost"); to app.listen(4000);

redirect from node js server to nginx

I am trying to setup an nginx server in front of my nodejs server(with express).
This is my server config for nginx :
server {
listen 8080;
server_name localhost:8080;
root /usr/share/nginx/appHtml;
autoindex on;
location / {
proxy_pass http://localhost:3000;
}
}
My node server verifies the user and redirects the request to http://localhost:3000/u/user. Something like :
app.get('/',function(req,res){
//verify user
res.redirect('/u/user');
})
I want my window location to read localhost:8080/u/user instead of just localhost:8080 after this redirect as is the case when i am running my nodejs server without nginx.
Similarly i want the same behaviour for all my server redirects.
How do i achieve this?

Resources