How to open wss: or ws: port on my website?

How to open wss: or ws: port on my website?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to open wss: or ws: port on my website?

You are currently running a WebSocket server locally using Ratchet and Socket.IO, likely behind an Nginx or Apache reverse proxy that exposes it on localhost:8080. Your JavaScript client connects successfully because the connection is local, bypassing network security restrictions like firewalls. However, when you deploy to production, simply changing the URL to wss://mywebsite is insufficient. The underlying infrastructure must be configured to terminate SSL/TLS and forward traffic to your application server.

The core issue isn't "opening a port" in the traditional sense of firewall management alone; it is about establishing a secure tunnel from the client to your backend. When you use localhost, your machine handles the encryption. On a public domain, the network perimeter (your hosting provider's load balancer or your own server) must handle the termination of the HTTPS connection and establish the WebSocket handshake.

Configuring the Reverse Proxy

The most common setup involves placing an Nginx reverse proxy in front of your PHP-FPM or Node.js application. Since you are using Ratchet/libsocketo, your backend is likely PHP. You need to ensure Nginx listens on port 443 (HTTPS) and forwards the raw TCP stream to the port where your Ratchet server is listening, often a specific internal port like 8080.

Here is an example Nginx configuration block that enables WSS:

server {
    listen 443 ssl;
    server_name mywebsite.com;

    # SSL Certificates
    ssl_certificate     /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    location / {
        # Forward the request to your local Ratchet port
        proxy_pass http://127.0.0.1:8080;
        
        # Essential headers for WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_read_timeout 86400;
    }
}

The critical lines here are proxy_pass and the headers setting Upgrade and Connection. Without these, Nginx will treat the WebSocket handshake as a standard HTTP request and return a 502 Bad Gateway or 403 Forbidden, causing your JavaScript onopen event to never fire.

Updating Your Client Code

Once the server is configured, you must update your client-side code to match the public URL. The source material shows a local connection using localhost. In production, browsers will block connections to localhost if they originate from a public domain due to security policies (mixed content or simple DNS resolution issues).

Update your JavaScript initialization as follows:

var conn = new WebSocket('wss://mywebsite.com'); // Replace with your actual domain

conn.onopen = function(e) {
    console.log("Connection established!");
};

Note that you should not include the port in the URL unless you explicitly set up a custom port (e.g., wss://mywebsite.com:8443). Standard practice is to use port 443 for HTTPS and let the browser handle the rest. If you do need a custom port, ensure your Nginx configuration listens on that specific port instead of 443.

Handling Subdomains and Pathing

A frequent pitfall involves path mismatches. If your Ratchet server is configured to listen on /chat internally, but your Nginx proxy forwards everything to the root (/), the handshake will fail. The Location header sent by the server during the upgrade must match the client's request path exactly.

If you are using a subdomain like chat.mywebsite.com, ensure your DNS records point to the same load balancer or IP address, and that your Nginx configuration is specific to that subdomain. Do not mix HTTP and HTTPS; if your site uses HTTPS for pages, WebSocket connections must also use WSS. Mixing ws:// with an SSL site triggers browser security warnings and blocks the connection automatically.

Scaling Considerations

If you are running on a shared hosting environment or a platform like Laravel Forge, the approach might differ slightly. You may need to configure your virtual host in cPanel or edit the nginx.conf file provided by your host. In such environments, ensure that the firewall (often ufw or cloud provider security groups) allows inbound traffic on port 443.

For high-traffic applications, consider using a managed WebSocket service like Pusher or Ably if maintaining a raw TCP server becomes cumbersome. However, for standard chat applications using Ratchet, the reverse proxy method described above is the industry standard and offers full control over your data pipeline.

Our network