POST requests to Node.js app behind nginx not getting through - node.js

POST requests to http://mysite.io:5003/GetProjects, works fine(http). But POST requests to https://mysite.io:5003/GetProjects returns 'Could not get any response' in Postman and 'ERR_CONNECTION_REFUSED' on my site at http://mysite.io/Projects. I'm using PM2 to run my backend.js and am also just running node backend.js to debug. OS is Ubuntu 16.04 in a EC2 instance, which has port 443 open.
nginx config is:
server {
listen 443 ssl;
server_name mysite.io www.mysite.io;
root /var/www/html/mysite-service/build;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
try_files $uri /index.html;
}
ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Related

MERN Stack App with NGINX: Timeout when react app tries to connect to server side API

I have a MERN stack app that I am trying to put into production.
I am able to get the client side running using NGINX as a reverse proxy to port 3000.
The issue I am having is when I am trying to get a response from my server running on port 5000. This is where I have my API to query against my database.
I believe the issue lies in my server block I have set up for my site. Below is an example for my signin endpoint that I am getting a TIMEOUT from. I have replaced my URL with example.com
server {
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost: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;
}
location /users/signin {
proxy_pass http://localhost:5000/;
proxy_buffering on;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
Any help would be appreciated. I believe I just need help trying to expose these endpoints properly.
Thanks!

Nginx multiple DNS to same webserver

GOAL https://imgur.com/a/A04qrXz
^ Trying to get multiple (different) domains pointing at the same IP, serving the same files to bypass DNS filtering agent GoGaurdian.
Webserver SRC : https://github.com/Zaydo123/html-game-server
Why: trying to bypass a DNS filtering service
My /etc/nginx/conf.d/physics.conf (works fine)
server {
server_name physics-central.com;
location / {
proxy_pass http://localhost:3000/;
proxy_set_header X-Real-IP $remote_addr;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/physics-central.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/physics-central.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = physics-central.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen *:80 default_server;
listen 8000 default_server;
listen [::]:80 default_server;
server_name physics-central.com;
return 301 https://physics-central.com$request_uri; # managed by Certbot
}
Problem
From my little knowledge of Nginx, I assume I need to create one .conf file for each domain. Each is essentially the same but has different SSL certs and domain names. I tried doing this but upon reloading I received this warning/error.
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/conf.d/physics.conf:27
My question
Am I going about this wrong? Am I supposed to even be making conf.d files for each domain? It would be amazing if you could help me out. Thanks.

How to redirect HTTP to HTTPS while using an NGINX reverse proxy?

I am trying to use NGINX on AWS for a reverse proxy to run a Node server. If I go to https://example.com/ , my connection is secure and everything is fine. But, when I go to http://example.com/ , no reroute occurs, and my connection is not secure. I am also using pm2 to run the Node server in the background.
I have tried the default server block reroutes that come up when I google the issue, but nothing has worked so far. My guess is that Node is handling requests on port 80, since my website comes up the way it did before I had my site fully set up. But I have no clue how to fix that.
Here are my server blocks in /etc/nginx/nginx.conf:
server {
# if ($host = www.example.com) {
# return 301 https://$host$request_uri;
# } # managed by Certbot
listen 80;
listen [::]:80;
server_name _;
return 301 https://$host$request_uri; # managed by Certbot
}
server {
server_name www.example.com example.com; # managed by Certbot
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost: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;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
listen [::]:443 ssl ipv6only=on default_server; # managed by Certbot
listen 443 ssl default_server; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # mana$
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # ma$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
Would appreciate any suggestions, as this is for a portfolio website and most places won't link directly to HTTPS.
If anyone else has had this issue, I managed to fix the problem. After trying everything under the sun, I remembered that I messed with my iptables when following an online guide to remove the port number from the address. I fixed my issue by wiping my iptables config, and since I was using a proxy I didn't need to reroute the port.

Nginx subdomain shows same Node.js app that domain does

I am pretty new to nginx, I managed to get web server running and I can access my Node.js app (port 5000) on my domain. However I would like to add a subdomain to access static website.
At the moment with my config, if I go to my subdomain I see my node.js application.
My nginx config files inside /etc/nginx/sites-available are the following:
default
subdomain.example.com.conf
Default config:
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com; # managed by Certbot
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://localhost:5000; #whatever port your app runs on
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;
}
}
Subdomain config:
server {
listen 443 ssl;
listen [::]:443 ssl;
root sites/example;
index test.example.com.html;
server_name test.example.com www.test.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
try_files $uri $uri/ =404;
}
}
How could I see different content other than my Node.js app from my subdomain?
Thank you in advance.

How do I setup NGINX for both Angular and Node?

I'm currently trying to host both an angular app and a reverse proxy to a node backend. Using Lets Encrypt, I've been able to set up the Angular app without any problems. However, I'm lost on how to configure Nginx to also act as a reverse proxy to my node app running on a specific port. I find examples of reverse proxies for nginx, but nothing that incorporates both.
Here is my config that is working with angular:
server {
server_name example.com;
root /var/www/example.com;
index index.html index.htm;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
What I'm trying to add:
server{
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
I've tried several combinations of server blocks and location blocks, but I simply get a 404 when I go to api.example.com.
I usually solve that kind of issue like this.
Endpoints of all back-end APIs are started with "v1".
I serve the angular project using pm2.
location / {
proxy_pass http://localhost:3000;
...
}
location ~ ^/(v1)/ {
proxy_pass http://localhost:3001;
...
}

Resources