I deploy first time my app in a VPS with a new domain.
Till now my project axios url was 'http://localhost:5000/posts'
My node server is running in 5000 port and i use proxy in react to listen to5000 and not 3000.
So, how do i transfer thoses urls to my machine ? do i have to use my new server IP or example.com domain to axios url ?
I am able to understand that you want to have react and nginx application running on same server under same IP and you have react application running on 3000 port and nodejs on 5000.
You can have following configuration in your default.conf
server {
root /var/www/html;
server_name _;
listen 3000;
# this is the react application endpoints
location / {
try_files $uri $uri/ /index.html;
}
}
In your react application, use the api endpoint as http://127.0.0.1:5000/posts.
So now, react application will be dealt by NGINX and api by nodejs listening on 5000.
Related
I have question about nginx configuration - I want to pass error handling into nodejs application (frontend app), so for eg. 404 error (and others) will be handled by nodejs app.
The "problem" is, that the app is running directly via nginx (it doesn't use it's internal mini-web server as many nodejs apps).
nginx configuration is as simply as possible:
server {
listen 80;
server_name *.somedomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name *.somedomain.com;
ssl_certificate /etc/letsencrypt/live/somedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/somedomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/somedomain.com/chain.pem;
root /usr/share/nginx/html;
}
Want to configure error handling via nodejs application, instead nginx.
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 /
I am deploying my app on an ec2 machine. There is a node app which corresponds to myapp.com/api. This is working fine. The Client app is built on React and users visit it on myapp.com.
The client app is getting rendered and i am able to navigate to different paths. However when I refresh a page, eg. myapp.com/profile it always resolves to myapp.com.
server {
listen 80;
server_name myapp.com;
root /home/ec2-user/projects/synaid/client/build;
passenger_enabled on;
index index.html;
location / {
root /home/ec2-user/projects/synaid/client/build;
try_files $uri /index.html;
}
location /api {
root /home/ec2-user/projects/synaid/server/build;
rewrite ^/api/(.*) /$1 break;
passenger_enabled on;
passenger_app_type node;
passenger_app_root /home/ec2-user/projects/synaid/server/build;
passenger_startup_file index.js;
passenger_env_var APP_ENV staging;
}
}
What I thing is happening is that when nginx server gets a request it is slicing the url from base path and rendering the index.html to browser. It should also be passing the url forward to the app, which it doesnt seem to be doing.
A possible solution could be to forward url to the app from inside the configuration.
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?
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?