How should I configure nginx to run as a reverse proxy websocket server in front of a tcp server?












0















I have a tcp server running on 127.0.0.1:1905



I have configured nginx like this



http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

upstream websocket {
server http://127.0.0.1:1906;
}

server {
listen 8020;
location / {
proxy_pass http://127.0.0.1:1905;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}


I have tried opening a socket using:



npm install -g wscat
wscat --connect ws://127.0.0.1:1906


I get



error: connect ECONNREFUSED 127.0.0.1:1906


What am I doing wrong here? I an new with both nginx and web sockets so I don't exactly understand what is happening nor where the problem might lie.










share|improve this question



























    0















    I have a tcp server running on 127.0.0.1:1905



    I have configured nginx like this



    http {
    map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
    }

    upstream websocket {
    server http://127.0.0.1:1906;
    }

    server {
    listen 8020;
    location / {
    proxy_pass http://127.0.0.1:1905;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    }
    }
    }


    I have tried opening a socket using:



    npm install -g wscat
    wscat --connect ws://127.0.0.1:1906


    I get



    error: connect ECONNREFUSED 127.0.0.1:1906


    What am I doing wrong here? I an new with both nginx and web sockets so I don't exactly understand what is happening nor where the problem might lie.










    share|improve this question

























      0












      0








      0








      I have a tcp server running on 127.0.0.1:1905



      I have configured nginx like this



      http {
      map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
      }

      upstream websocket {
      server http://127.0.0.1:1906;
      }

      server {
      listen 8020;
      location / {
      proxy_pass http://127.0.0.1:1905;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      }
      }
      }


      I have tried opening a socket using:



      npm install -g wscat
      wscat --connect ws://127.0.0.1:1906


      I get



      error: connect ECONNREFUSED 127.0.0.1:1906


      What am I doing wrong here? I an new with both nginx and web sockets so I don't exactly understand what is happening nor where the problem might lie.










      share|improve this question














      I have a tcp server running on 127.0.0.1:1905



      I have configured nginx like this



      http {
      map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
      }

      upstream websocket {
      server http://127.0.0.1:1906;
      }

      server {
      listen 8020;
      location / {
      proxy_pass http://127.0.0.1:1905;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      }
      }
      }


      I have tried opening a socket using:



      npm install -g wscat
      wscat --connect ws://127.0.0.1:1906


      I get



      error: connect ECONNREFUSED 127.0.0.1:1906


      What am I doing wrong here? I an new with both nginx and web sockets so I don't exactly understand what is happening nor where the problem might lie.







      node.js nginx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 14:39









      user1283776user1283776

      3,5802159116




      3,5802159116
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The upstream configuration looks faulty, can you try the bellow,



          upstream websocket {
          server http://127.0.0.1:1906;
          }
          server {
          listen 8020;
          location / {
          proxy_pass http://websocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          location /webserver {
          proxy_pass http://127.0.0.1:1905;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          }


          If you have another server also to be configured, you can configure another upstream or another location as given above for /webserver. So / will be your socket server and /webserver will be the other server(normal http server)






          share|improve this answer
























          • I tried with your config and still get the same error message. Thank you for your help regardless!

            – user1283776
            Nov 21 '18 at 15:02











          • Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

            – Jeevan
            Nov 21 '18 at 15:07











          • The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

            – user1283776
            Nov 21 '18 at 15:12











          • Your websocket server is running on port 1906 right?

            – Jeevan
            Nov 21 '18 at 15:15











          • I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

            – user1283776
            Nov 21 '18 at 15:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414473%2fhow-should-i-configure-nginx-to-run-as-a-reverse-proxy-websocket-server-in-front%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The upstream configuration looks faulty, can you try the bellow,



          upstream websocket {
          server http://127.0.0.1:1906;
          }
          server {
          listen 8020;
          location / {
          proxy_pass http://websocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          location /webserver {
          proxy_pass http://127.0.0.1:1905;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          }


          If you have another server also to be configured, you can configure another upstream or another location as given above for /webserver. So / will be your socket server and /webserver will be the other server(normal http server)






          share|improve this answer
























          • I tried with your config and still get the same error message. Thank you for your help regardless!

            – user1283776
            Nov 21 '18 at 15:02











          • Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

            – Jeevan
            Nov 21 '18 at 15:07











          • The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

            – user1283776
            Nov 21 '18 at 15:12











          • Your websocket server is running on port 1906 right?

            – Jeevan
            Nov 21 '18 at 15:15











          • I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

            – user1283776
            Nov 21 '18 at 15:17
















          0














          The upstream configuration looks faulty, can you try the bellow,



          upstream websocket {
          server http://127.0.0.1:1906;
          }
          server {
          listen 8020;
          location / {
          proxy_pass http://websocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          location /webserver {
          proxy_pass http://127.0.0.1:1905;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          }


          If you have another server also to be configured, you can configure another upstream or another location as given above for /webserver. So / will be your socket server and /webserver will be the other server(normal http server)






          share|improve this answer
























          • I tried with your config and still get the same error message. Thank you for your help regardless!

            – user1283776
            Nov 21 '18 at 15:02











          • Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

            – Jeevan
            Nov 21 '18 at 15:07











          • The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

            – user1283776
            Nov 21 '18 at 15:12











          • Your websocket server is running on port 1906 right?

            – Jeevan
            Nov 21 '18 at 15:15











          • I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

            – user1283776
            Nov 21 '18 at 15:17














          0












          0








          0







          The upstream configuration looks faulty, can you try the bellow,



          upstream websocket {
          server http://127.0.0.1:1906;
          }
          server {
          listen 8020;
          location / {
          proxy_pass http://websocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          location /webserver {
          proxy_pass http://127.0.0.1:1905;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          }


          If you have another server also to be configured, you can configure another upstream or another location as given above for /webserver. So / will be your socket server and /webserver will be the other server(normal http server)






          share|improve this answer













          The upstream configuration looks faulty, can you try the bellow,



          upstream websocket {
          server http://127.0.0.1:1906;
          }
          server {
          listen 8020;
          location / {
          proxy_pass http://websocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          location /webserver {
          proxy_pass http://127.0.0.1:1905;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
          }
          }


          If you have another server also to be configured, you can configure another upstream or another location as given above for /webserver. So / will be your socket server and /webserver will be the other server(normal http server)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:55









          JeevanJeevan

          1326




          1326













          • I tried with your config and still get the same error message. Thank you for your help regardless!

            – user1283776
            Nov 21 '18 at 15:02











          • Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

            – Jeevan
            Nov 21 '18 at 15:07











          • The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

            – user1283776
            Nov 21 '18 at 15:12











          • Your websocket server is running on port 1906 right?

            – Jeevan
            Nov 21 '18 at 15:15











          • I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

            – user1283776
            Nov 21 '18 at 15:17



















          • I tried with your config and still get the same error message. Thank you for your help regardless!

            – user1283776
            Nov 21 '18 at 15:02











          • Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

            – Jeevan
            Nov 21 '18 at 15:07











          • The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

            – user1283776
            Nov 21 '18 at 15:12











          • Your websocket server is running on port 1906 right?

            – Jeevan
            Nov 21 '18 at 15:15











          • I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

            – user1283776
            Nov 21 '18 at 15:17

















          I tried with your config and still get the same error message. Thank you for your help regardless!

          – user1283776
          Nov 21 '18 at 15:02





          I tried with your config and still get the same error message. Thank you for your help regardless!

          – user1283776
          Nov 21 '18 at 15:02













          Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

          – Jeevan
          Nov 21 '18 at 15:07





          Have you tried directly accessing 127.0.0.1:1906, it is highly possible that it might not be an issue with Nginx but with the websocket server itself.

          – Jeevan
          Nov 21 '18 at 15:07













          The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

          – user1283776
          Nov 21 '18 at 15:12





          The tcp server is on 127.0.0.1:1906. The following cli command succeeds and returns data from the server: nc localhost 1905

          – user1283776
          Nov 21 '18 at 15:12













          Your websocket server is running on port 1906 right?

          – Jeevan
          Nov 21 '18 at 15:15





          Your websocket server is running on port 1906 right?

          – Jeevan
          Nov 21 '18 at 15:15













          I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

          – user1283776
          Nov 21 '18 at 15:17





          I have a tcp server running on 1905. I want to connect to the tcp server from a js websockets server. But I have not build the websocket yet. Instead I am testing connecting to the tcp server using the command line tool wscat --connect ws://localhost:1906

          – user1283776
          Nov 21 '18 at 15:17




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414473%2fhow-should-i-configure-nginx-to-run-as-a-reverse-proxy-websocket-server-in-front%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?