question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Primus js on sub path (sub uri) on Nginx Proxy Server

See original GitHub issue

Version: 3.2.3

Environment:

  • Operating system: Mac OSX
  • Browser: Crome
  • Node.js: v0.12.0

Expected result: Primus should be work on sub path of Nginx.

Actual result: It is falling back to localhost.

Steps to reproduce: I want to use Primus with Node JS server behind Nginx at sub path (or sub uri) as real-time. So I have made changes in nginx.conf as below.

upstream realtime {
        ip_hash;
        server localhost:1967;
}
        location /real-time/ {
          proxy_pass http://realtime/;
          proxy_redirect      off;
          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;

          # WebSocket specific
          proxy_http_version 1.1;
          proxy_set_header    Upgrade           $http_upgrade;
          proxy_set_header    Connection        "upgrade";

          #
          # Specific for comet or long running HTTP requests, don't buffer up the
          # response from origin servers but send them directly to the client.
          #
          proxy_buffering     off;

          #
          # Bump the timeout's so someting sensible so our connections don't
          # disconnect automatically. We've set it to 12 hours.
          #
          proxy_connect_timeout 43200000;
          proxy_read_timeout    43200000;
          proxy_send_timeout    43200000;
        }

Now I am able to get primus.js file at http://localhost/real-time/primus/primus.js. But when I will try make connection as below.

primus = Primus.connect("http://localhost/real-time?some_query_string, {
   transformer: 'SockJS'
});

It fall back to localhost as below and getting following error.

GET http://localhost/primus/info?some_query_string&_primuscb=LDur-1u&t=1458021326978 404 (Not Found)

Without Nginx, It works fine using http://localhost:1967. (I am running the node js server on 1967 port.)

How can I properly configure Primus with Nginx? Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lpincacommented, Mar 15, 2016

If you configured the pathname to be /real-time/primus, your location location in nginx should be /real-time/primus/.

On the client you don’t have to specify the pathname and the transformer option is only valid on the server.

0reactions
mauron85commented, Oct 15, 2017

I think pathname should be also configurable from client. The reason for it is following. Imagine nginx as reverse proxy with following configuration:

upstream ws_upstream {
  server localhost:8882;
}

server {
  listen *:80 default_server;

  location /ws/ {
    proxy_pass  http://ws_upstream/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

So basically nginx will proxy any request from http://myremotedomain.com/ws -> http://localhost:8882 but /ws part of path is actually not sent to local primus server.

If client wants to connect it will:

  1. download client side script from url: http://myremotedomain.com/ws/primus/primus.js
  2. initiate connection with Primus.connect() to url http://myremotedomain.com/primus as defined by server pathname option.

But here is the problem. Auto generated primus.js client side script has no notion about /ws subpath (which is server from). Only nginx is aware of that subpath and actually strip /ws before proxying request to local server . There is no way with single nginx location how to proxy such a scenario.

Currently only way to @workaround is by adding another nginx location direction

  location /primus {
    proxy_pass  http://ws_upstream/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

I suggest Primus.connect to obey whole url as is and not only domain part of it as it is now. eg. Primus.connect(’ http://myremotedomain.com/ws/primus’).

Quote from nginx

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive: location /name/ { proxy_pass http://127.0.0.1/remote/; }

Read more comments on GitHub >

github_iconTop Results From Across the Web

NginX: Serve application under sub path - proxy - Server Fault
Generally running the app under an URI prefix when the app itself does not expect it is a tricky thing, and the only...
Read more >
NodeJS and Primus.io WebSocket opening handshake was ...
It seems to me that you've configured your nginx server incorrectly and that it's blocking WebSocket connections causing a 400 error to be ......
Read more >
How to set up reverse proxy with nginx to host express js app ...
I want to host an express js app at ... How to set up reverse proxy with nginx to host express js app...
Read more >
primus
There are a lot of real-time frameworks available for Node.js and they all have different ... If you're running your server behind a...
Read more >
Ws | npm.io
requires-port, http-proxy-middleware, isomorphic-ws, js-message, rpc-websockets, express-ws, simple-websocket, ccxt, primus, koa-websocket, websocket-
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found