I’m trying to get Websockets to work locally. I have a frontend and backend application which do both regular HTTPS calls and websocket connections. I’ve also deployed these applications to AWS as well, where both type of connections work just fine. So I think the problem is in the local configuration.
I’m using Docker in a WSL Ubuntu instance for a frontend pod, a backend pod and an Nginx pod. Nginx handles all incoming traffic and directs it to the right pod. In the case of a call starting with /api
, it will be directed to the backend. The websocket connection also starts with /api.
Locally (using my Windows machine) I can use https connections. I created a certificate for *.mywebsite.com
and edited my hosts file on my Windows machine to point to the Ubuntu IP address when I navigate to https://mywebsite.com
. So that’s why I’m also using wss locally.
I also tried opening Chrome in WSL and navigating to the website. After adjusting the /etc/hosts
file I can navigate to my local running website, load the whole website and do API calls, but the websocket-connection remains stuck. Below is my nginx.conf
file, can anyone see if I’m missing something or doing something wrong?
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log debug;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Frontend and API Server Configuration
server {
listen 443 ssl;
server_name *.mywebsite.com mywebsite.com;
ssl_certificate /etc/nginx/ssl/_wildcard.mywebsite.com+1.pem;
ssl_certificate_key /etc/nginx/ssl/_wildcard.mywebsite.com+1-key.pem;
# Frontend location
location / {
proxy_pass http://10.90.1.4:8081; # Use the static IP of the frontend pod
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_verify off; # Disable SSL verification for internal services
proxy_read_timeout 86400;
proxy_buffering off;
}
# Backend API location
location /api {
proxy_pass http://10.90.1.3:8080; # Use the static IP of the backend pod
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 86400;
proxy_buffering off;
}
}
}
stream {
upstream postgres {
server 10.90.1.2:5432; # Use the static IP of the PostgreSQL container
}
server {
listen 5432;
proxy_pass postgres;
proxy_connect_timeout 1s;
proxy_timeout 3s;
}
}