title: NGinx forward websocket from 80 to websocket port
body: <p>I am using Nginx as a web host and proxy for a websocket running on the same device listening on port 8888.  Trying to find a way to have nginx listen on 80 and forward the websocket requests to the internal port.  Without exposing a new port to the outside.  Is this even possible?</p>
  
  <p>UPDATE:</p>
  
  <p>This is my current configuration:</p>
  
  <pre><code>error_log       /var/log/nginx/error_log.log    warn;
  
  map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
  }
  
  upstream websocket {
      server localhost:8888;
  }
  
  
  server {
      listen 80;
      #listen [::]:80 default_server;
  
      # SSL configuration
      #
      # listen 443 ssl default_server;
      # listen [::]:443 ssl default_server;
      #
      # Self signed certs generated by the ssl-cert package
      # Don't use them in a production server!
      #
      # include snippets/snakeoil.conf;
  
      root /var/www/html/EncoderAdmin;
  
      # Add index.php to the list if you are using PHP
      index index.php index.html index.htm index.nginx-debian.html;
  
      server_name _;
  
      location / {
              # First attempt to serve request as file, then
              # as directory, then fall back to displaying a 404.
              try_files $uri $uri/ =404;
              auth_basic "Restricted Content";
              auth_basic_user_file /etc/nginx/.htpasswd;
  
  
      }
  
      location /ws {
              proxy_pass              http://localhost:8888;
              proxy_http_version      1.1;
              proxy_set_header        Host $http_host;
              proxy_set_header        X-Real-IP $remote_addr;
              proxy_set_header        Upgrade $http_upgrade;
              proxy_set_header        Connection "upgrade";
      }
  
  }
  </code></pre>
  
  <p>When I try to connect to it with ws://[address]/ws  I get:</p>
  
  <p>WebSocket connection to 'ws://[address]/ws' failed: Error during WebSocket handshake: Unexpected response code: 400</p>