NodeJs Auto start in business server with systemd as different user - node.js

I have my business app running in Development env and inside that, 2 folders named Client and Backend.
Client (ReactJS) running in port 5000
Backend (Node.JS) running in Port 6000
Server Nginx.
So in Nginx default.conf file, listening 80 and I've proxy_pass http://localhost:5000.
Its working fine in the Development.
Please note, some redirections are configured like ${host}:3000/xxx in the backend and client scripts
But while doing the production deployment, I found difficulty in doing so.
I have the static build client file and placed it in the nginx root folder.
Below is the .conf file
server {
listen 80;
listen 5000;
server_name xx.xxx.xxx.xxx;
location / {
root /usr/share/nginx/html/client/build;
index index.html index.htm;
try_files $uri $uri/ #backend;
}
location ~ ^/([A-Za-z0-9]+) {
proxy_pass http://localhost:6000;
}
}
I Also have SSO enabled, when I navigate the address, it send the index.html file which is the login page.
When I press login, first it will navigate to "/login/abc/" which is routed in "backend" script.
But it responds with 404 error.
What am I doing wrong?

Related

Can't redirect traffic to localhost with nginx and docker

I'm new to Docker and nginx so this may be a simple question but I've been searching through question/answers for a while and haven't found the correct solution.
I'm trying to run an nginx server through docker to reroute all requests to my.example.com/api/... to localhost:3000/api/...
I have the following Dockerfile:
FROM nginx
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
and the nginx.conf file:
server {
server_name my.example.com;
location / {
proxy_pass http://localhost:3000/;
}
}
When I make the calls to the api on localhost:3000 this works fine but when I try to run against my.example.com I get a network error that the host isn't found. To be clear the domain I want to 'redirect' traffic from to localhost host is a valid server address, but I want to mock the api for it for development.
This isn't working because your nginx proxying request to localhost which is container itself but your app is running on host's port 3000 -- outside of container. check this article
change
proxy_pass http://localhost:3000/;
  to
proxy_pass http://host.docker.internal.
add 127.0.0.1 example.com my.example.com in etc/hosts

Connect a docker container to another docker container with nginx

I currently have a VueJS application that has 2 parts. One being a static front end page and the other as a NodeJS express backend.
In the backend server, it will load the static(dist) folder as the front end view. The backend also contains some MongoDB api's to load and save some configuration.
I am currently trying to separate the two as each own docker container. I've managed to make the backend running but I am currently having some issues on running the front end. I'm having issues with configuring the nginx that will allow me to upload files to the server.
Here is my current nginx config
server{
listen 80;
root /var/www/html;
index index.html;
location /models/ {
root /app/upload;
}
location / {
try_files $uri $uri/ /index.html;
}
}
So in the frontend, it will create a post request to the backend to upload some models but it seems that it is throwing: 405 (Not Allowed) error.
Is the issue because there is something wrong with the configuration or it cannot connect to the backend as it is in a separate container?
Any help/opinions with this is greatly appreciated. Thank you

Nginx nodejs problems

I am trying to use nginx to direct a website hosted on port 8080 to domain exemple1.com and another one on port 8081 that i want to redirect to domain exemple2.com.
On the file /etc/nginx/sites-available/default i puted this code:
location ~/example1/ {
proxy_pass http://example1.com;
}
location ~/example2/ {
proxy_pass http://example2.com;
}
but i couldn make it work . I am running 2 nodejs servers on the ports i talked about (port 8080 and 8081).
What i am doing wrong and how to "fix "
it?
Because the downstream app server running on different ports(listen) than coming in, you need to specify ports in proxy_pass. So I think
listen 8080;
location ~/example1/ {
proxy_pass http://example1.com:8080;
}
location ~/example2/ {
proxy_pass http://example2.com:8081;
}

How to connect nginx to local mongodb

I've got nginx to run my (node.js/react) application on the server. But I can't seem to connect to the database.
In the nginx.conf file I've added the following inside http.
http {
...
server {
listen 80;
server_name localhost;
...}
...}
And above the http section I have the following,
stream {
server {
listen 4000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass stream_mongo_backend;
}
upstream stream_mongo_backend {
server 127.0.0.1:27017;
}
}
I start the nginx server, the application runs on localhost, opens up the login page but I can't login because it's still not connected to the database (mongodb).
I'm not sure if I've got my port numbers wrong or if I'm missing some configuration line inside nginx.conf.
EDIT: Ok, I had it wrong before. I wasn't supposed to connect to mongodb at this point. I was supposed to connect to the backend server of my application which would run at 4000. So now I've added a new location for /api/ inside http and proxied all requests to 4000. I still have one question though. I have to run my backend server separately for this to work. For the frontend I've created a folder and put all my build files in there so nginx starts up the webserver from there. Doing the same for the backend did not start up the server. Is there a way to get nginx to start the backend server as well?
Also can I get the frontend to run directly without the build files ? Like node would with npm start?
the port number is right. try to open up a mongo shell and see if you are able to access a mongo instance. if not, you will need to run sudo service mongodb start to start it up.
Guess it's knida late but you don't need to setup nginx for your backend to connect local mongodb.
And you need to run the frontend and backend server first by yarn start, node run or something like that if you want to run it without build files.
And then bypass the calls from 80 port to the local host servers.
For example, your FE run at 3000 port, BE run at 5000 port.
Then your nginx should be:
http {
...
server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass localhost:5000;
}
location / {
proxy_pass localhost:3000;
}
...}
...}

Prevent Nginx from caching nodejs responses

I am using Nginx to proxy requests to server based on directory user want to access
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
location / {
proxy_pass http://****.***/;
}
location /app/{
proxy_no_cache '1';
proxy_cache_bypass '1';
proxy_buffering off;
include proxy_params;
proxy_pass http://localhost:3000/;
}
}
This is nginx configuration.
A Node app is running on 3000 port
Problem I am facing is
Users access "/app"
Server sends login.html from node app.
User logins from the page.
Node sends home.html after successful login.(Problem Lies here)
Although Node is sending home.html but Nginx responds to request with 304 code and browser shows same login page again.
example of Node app
.....
app.get("/",function(req,res){
***Sends login page or home page based on session***
});
app.get("/processLogin",function(req,res){
***redirects to / after setting session****
});
.....
In proxy mode nginx is using Expires header to reduce load on the backend server...
So simply set expires off; in the proxy location block and caching should be gone.
In case the caching occurs in the browser, you'll need to set the cache control header to no-cache:
add_header Cache-Control no-cache;
Adding no-cache headers in nodejs helped to solve the problem.
One question: do you use pm2 for your Node application ? Many use Nginx + pm2.
For the development phase of pm2, you need to put the --watch flag while launching your App. pm2 load all the Node.js in memory and dont check file change on the hardisk. You have then a cache phenomenon.
So, during the development phase, in place of
pm2 start MyApp.js
do
pm2 start MyApp.js --watch
Honestly, I dont see how a browser cache or Nginx can cache variable responses given by node.js programs. It had to be the pm2, in my case.

Resources