PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 2, 2022

[FIXED] How to pass custom header from NGINX Reverse Proxy to SockJs websocket connection in the server?

 September 02, 2022     nginx-reverse-proxy, websocket     No comments   

Issue

I am using NGINX as a reverse proxy. Below are the setings in nginx.conf file. I want to pass a custom header from the proxy to the back-end HTTP server. I do get the custom header in the HTTP server but I am not getting it in my sockJs connection.

   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-RealIPRemote $realip_remote_addr;
   proxy_set_header X-RemoteAddr $remote_addr;
   proxy_set_header Host $host;
   proxy_set_header mycustomhdr customname;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_pass http://192.168.1.1:18000;

   # WebSocket support
   proxy_http_version 1.1;
   proxy_set_header mycustomhdr customname;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";

In the http server to which the connection is proxied to:

sockServer = sockJs.createServer(
... 
 sockServer.on('connection', function (conn) {
      console.log("conn.headers")
      console.log(conn.headers)

Here is what I get:

{ 'x-forwarded-for': '192.168.1.222',
        'x-real-ip': '192.168.1.222',
               host: 'example.com',
       'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36',
  'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8' }

I am not getting the custom header mycustomhdr.

~


Solution

Yeah, you won't. You'll notice several of your other headers are missing too. Not all headers are exposed.

From the docs:

headers (object) Hash containing various headers copied from last receiving request on that connection. Exposed headers include: origin, referer and x-forwarded-for (and friends). We explicitly do not grant access to cookie header, as using it may easily lead to security issues (for details read the section "Authorisation").



Answered By - miknik
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing