NGINX not routing calls from react app to backend app - nginx

I have an AWS Ubuntu server that hosts a react front end running at 127.0.0.1:4100 and makes api calls to a Go app using port 127.0.0.1:1323. I installed Nginx and setup proxy pass for these two ports in /etc/nginx/sites-available/default config file but I only get the front end getting called by Nginx. Using chrome inspect to check why the Go app is not serving some of the functionalities from the react app, I see this error
client.js:772 GET http://127.0.0.1:1323/api/ net::ERR_CONNECTION_REFUSED
ERROR Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
What am I doing wrong? Below is my default config file
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:4100;
}
location /api {
proxy_pass http://127.0.0.1:1323/;
}
}

Your server is listening to port 80:
listen 80 default_server;
listen [::]:80 default_server;
So, you should make your request to that port:
GET http://127.0.0.1/api/ => http://127.0.0.1:1323/
GET http://127.0.0.1:80/api/ => http://127.0.0.1:1323/
GET http://127.0.0.1/ => http://127.0.0.1:4100/
GET http://127.0.0.1:80/ => http://127.0.0.1:4100/
Then nginx should correctly proxy your requests.
Update
To be more clear about nginx config.
server {
listen 80 default_server; // The port nginx is listening to ipv4
listen [::]:80 default_server; // The port nginx is listening to ipv6
server_name _;
location / { // When you call this location...
proxy_pass http://127.0.0.1:4100; // You'll be redirected to this location
}
location /api { // When you call this location...
proxy_pass http://127.0.0.1:1323/; // You'll be redirected to this location
}
}
Your configuration is okay according to nginx docs.
You said your client is trying to reach http://127.0.0.1:1323/api/ but It should be requesting http://127.0.0.1/api/ (whitout the port) to be redirected to http://127.0.0.1:1323/.
Here's another example:
server {
listen 80;
server_name localhost anywebsite.com;
location ~* ^/MyApp {
proxy_pass http://localhost:5130;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 2m;
proxy_read_timeout 2m;
}
}
In this case, everytime my url ends with /MyApp ex.: http://anywebsite.com/api/MyApp I'm being proxyed to http://localhost:5130. But if I try to access http://localhost:5130 or http://anywebsite.com:5130/api/MyApp I won't be able because nginx is listening to port 80 only. If you want to access another port you need to specify like this:
server {
listen 80;
listen 5130;
[...]

Related

Nginx wrongly routes to subdomain instead of "main" domain

I have a setup where Nginx acts as an entrance to some other services down the line. Specifically, there are multiple apps and a WordPress instance running in docker containers. My configuration looks like this:
server {
listen 80 default_server;
server_name domain.com;
location / {
resolver 127.0.0.11 valid=30s;
set $upstream_wp wordpress;
proxy_set_header Host $host;
proxy_pass http://$upstream_wp;
proxy_redirect off;
}
}
server {
listen 80;
server_name app1.domain.com;
location / {
resolver 127.0.0.11 valid=30s;
set $upstream_app1 app1;
proxy_set_header Host $host;
proxy_pass http://$upstream_app1;
proxy_redirect off;
}
}
What happens now is the following:
domain.com: gets routed to app1 (should be wordpress)
domain.com/anything: gets routed to wp (correct)
app1.domain.com: gets routed to app1 (correct)
app1.domain.com/anything: gets routed to app1 (correct)
I don't understand why the first one does get routed to the app. Any suggestions?

Nginx local proxy with backup to prod server

I am trying to setup nginx proxy on my local machine.
What I want to achieve is like this:
site.local -> proxy_pass -> HTTP://localhost:3000
if localhost:3000 is down, then proxy to -> site.com
So far this is my config:
upstream mysite {
server localhost:3000 fail_timeout = 5 max_fails = 1;
server example.com backup max_fails = 2;
}
server {
server_name site.local;
listen ssl;
ssl_certificate /path/to/site-local.crt;
ssl_certificate_key /path/to/site-local.key;
ssl_ciphers HIGH: !aNULL: !MD5;
location / {
proxy_pass https://mysite;
proxy_set_header Host site.com;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
And I've made entry in hosts file for site.local 127.0.0.1.
I am getting multiple errors with slightly different configurations:
I am doing proxy_pass HTTP://mysite, then it is 301 redirecting to https://example.com, and not proxying to https.
If I did proxy_pass: https://mysite, then it is giving 502.
If I am doing server site.com:443 then it is saying non https traffic to 443 server is not allowed.
Any ideas around how to achieve this.

Nginx - Redirect domain to localhost:port content

I installed Nginx on my server (my server uses WHM). And on this server has two accounts. Each account will run a server a NextJS site and each account has its own domain.
Site1 will run on port 3000
Site2 will run on port 3004
What I want to do is:
I want to access domain1 I see the content of my site1 in NextJS that runs on localhost:3000
And when I access domain2 I see the content of my site2 on NextJS running on localhost:3004
I tried to do a Nginx implementation for site1. But when I accessed it I saw a Cpanel screen, and the url was dominio1/cgi-sys/defaultwebpage.cgi
Here's the Nginx implementation I tried to do:
server {
listen 80;
server_name computadorsolidario.tec.br www.computadorsolidario.tec.br ;
location / {
proxy_pass http://localhost:3004;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
}
So how do I do this setting for nginx to have this behavior? And I'm changing the correct file?
Note: I created the configuration file in /etc/nginx/conf.d/users/domain1/domio1.conf And within /etc/nginx/conf.d/users have several configuration files with the name of the accounts you have on the server. (They are already implemented.)
Try
server {
listen 80;
server_name www.domain1.com;
proxy_pass http://127.0.0.1:3000;
}
server {
listen 80;
server_name www.domain2.com domain2.com;
proxy_pass http://127.0.0.1:3004;
}
Each domain listens on same port and reverse-proxies to local network on the ports you specify. To differentiate between hosts, specify the server_name field.
server {
listen 80;
server_name www.domain1.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name www.domain2.com domain2.com;
location / {
proxy_pass http://127.0.0.1:3004;
}
}

how to redirect my domain to localhost: 3000 using ngnix

I'm new to all of this.
I'm going to put you in context. I bought a domain miweb.pe and an instance in aws. Currently my domain redirects to my aws instance because I have registered the dns servers of my amazon instance in myweb.pe.
I bought an ssl certificate and am trying to install it on my amazon instance, where I also installed nginx. I am unable to make any request to myweb.pe redirect to the aws instance that currently has a nodejs service active under port 3000.
this is my current configuration. What am I doing wrong?
server {
listen 443;
server_name myweb.pe;
ssl on;
ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/beekey.key;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name www.myweb.pe;
return 301 https://myweb.pe$request_uri;
}
# Redirige de https://www.tudominio.com a https://tudominio.com
server {
listen 443;
server_name www.miweb.pe;
return 301 $scheme://myweb.pe$request_uri;
}
in summary, I want that when accessing myweb.pe it actually accesses thelocalhost: 3000 which is running on my amazon instance.
So, what is the issue you are facing, I can see one issue in your nginx rule for servername you need to type domain name and not localhost. The other thing is I am assuming your service on port 3000 should already be running.

location "/app" cannot be inside the named location

I want to configure a nginx reverse-proxy server to redirect requests to different servers depending on :
the endpoint
whether it is a plain web request or a websocket upgrade request
I know I can use locations to manage the first point, and named locations to manage the second point, but how can I do both?
server {
listen 80 default_server;
listen [::]:80 default_server;
location /app {
location #web {
proxy_pass http://127.0.0.1:9080/app;
}
location #ws {
proxy_pass http://127.0.0.1:9081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}
I get the error message location "/app" cannot be inside the named location "#web"
What am I supposed to do to managed that kind of mixed trafic

Resources