Nginx forward /socket.io requests to proxied nodejs server - node.js

I am trying to forward all /socket.io requests to /broadcaster. Here is my nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /home/ubuntu/logs/broadcaster/access.log;
error_log /home/ubuntu/logs/broadcaster/error.log;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
# Enable WS
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
location /broadcaster {
rewrite ^/broadcaster/(.*) /$1 break;
proxy_pass http://127.0.0.1:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# simply forward socket.io requests.
# only one site can run socket.io on this server now
location /socket.io {
proxy_pass http://127.0.0.1:1337;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection $connection_upgrade;
}
}
When I (socket.io) makes a request like GET http://my-ec2-public-dns/socket.io/1/?t=1408981904508 I get the error 400 (Bad Request).
How can I nginx forward the /socket.io requests?

Related

I get 500 internal server error when I try to reach the website. I run nginx postgres and keycloak in docker container

500 internal server error
when checked the nginx container logs this is the error that was showing.
rewrite or internal redirection cycle while internally redirecting to "/index.html", client: x.x.x.x, server: *.domain.com, request: "GET / HTTP/1.1", host: "master.domain.com
In location /auth when mentioned localhost keycloak cannot be reached when localhost is replaced with ip in nginx config it works fine.
Below is the nginx config file
server {
listen 80;
listen [::]:80;
server_name domain.com;
#root /usr/share/nginx/html;
root /home/admin/newprodle/frontend/build/;
index index.html;
return 301 https://$host$request_uri;
}
server{
listen 443 ssl;
server_name *.domain.com domain.com;
ssl_certificate /etc/nginx/fullchain.pem;
ssl_certificate_key /etc/nginx/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:MD5;
root /home/admin/newprodle/frontend/build;
# root /usr/share/nginx/html;
index /index.html;
client_max_body_size 10M;
location / {
try_files $uri $uri/ /index.html;
}
location /auth {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
location /api {
proxy_pass http://localhost:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
location \~ .(jpg|png|jpeg|gif|pdf|xlxs|xl|doc❘docx) {
add_header 'Access-Control-Allow-Origin' '*' always;
root /home/admin/newprodle/backend/src/uploads;
}
location /socket.io {
proxy_pass http://localhost:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
}

Nginx reverse proxy to NodeJS App causing 502 bad gateway error

I am setting up nginx so that I can access my API built using express through a url like - example.com/api
Here is my nginx config
upstream appfrontend {
server localhost:9008 fail_timeout=0;
}
upstream api {
server localhost:3001;
}
server {
listen 80;
listen [::]:80;
server_name hospoline.com www.hospoline.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443;
server_name example.com; # replace this with your domain
root /var/www/html/example-certbot-webroot;
# The public and private parts of the certificate are linked here
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location /.well-known {
root /var/www/html/example;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://appfrontend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 900s;
}
location /api {
proxy_pass http://api;
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;
}
}
When I visit my site example.com the front end loads perfectly.
When I visit example.com/api/fetch_doctors, I get a 502 bad gateway error.
My API is working fine in localhost. When I send a request to localhost:3001 on my local computer, I get the list of doctors.
Both my front end server and backend server are run using forever.
I am really lost and breaking my head about this for one full day. I have no idea where I'm going wrong. Any help in the right direction would be great! Thank you all!

nginx node.js proxy 502

I have a node.js server running on port 5000 behind nginx and after reading several posts, I'm still getting a 502 from nginx. When accessing mydomain.com:5000 in a browser, everything works just fine. Can someone spot what I may be doing wrong here?
upstream backend {
server localhost:5000 max_fails=1 fail_timeout=3s;
keepalive 8;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.com;
ssl_certificate ssl/chained.crt;
ssl_certificate_key ssl/server.key;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection '';
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache off;
proxy_cache_bypass $http_upgrade;
}
}

nginx reverse proxy for nodejs express,works with ip address but not domain name

I have nginx works as a reverse proxy for a node.js express application.
I can access to
http://ip:8081/data/emoji.json
but it doesn't work with domain name
http://domain_name/data/emoji.json
doesn't work.
Here is the configuration of nginx:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name domain_name;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:8081;
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 /mongo {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
rewrite /mongo/(.+) /$1 break;
proxy_pass http://127.0.0.1:28017;
break;
}
}
}

Reverse proxy with sub-directories using nginx and nodejs

I am having trouble setting up reverse proxy with nginx forwarding requests to node sitting a the back end.
My nginx conf file is :
server{
listen 80;
server_name 192.168.50.176;
location /stretch {
rewrite /stretch/(.*) /$1 break
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Without subdirectories it works just fine.The below config works :
server{
listen 80;
server_name 192.168.50.176;
location / {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Resources