Click here to Skip to main content
15,896,526 members

How to extract subdomain and pass it as a parameter in Node.js and Haproxy

Vygandas Pliasas asked:

Open original thread
Hello,

I have configured my dedicated server to link domains with default port (80) to my nodejs ports accordingly to domain name. E.g. site1.com to port 2222 and site2.com to port 3333.

My question is how to pass a subdomain name as a parameter, e.g.:
param1.site1.com:80 should go to 127.0.0.1:2222/param1

My haproxy config:
global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    maxconn 4096
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    option redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

frontend http-in
    bind *:80
    acl is_www_site1_com hdr_end(host) -i site1.com
    acl is_www_site2_com hdr_end(host) -i site2.com
    
    use_backend www_site1_com if is_www_site1_com
    use_backend www_site2_com if is_www_site2_com
    default_backend www_site1_com

backend www_site1_com
    balance roundrobin
    cookie SERVERID insert nocache indirect
    option httpchk HEAD /check.txt HTTP/1.0
    option httpclose
    option forwardfor
    server Server1 127.0.0.1:2222 cookie Server1

backend www_site2_com
    balance roundrobin
    cookie SERVERID insert nocache indirect
    option httpchk HEAD /check.txt HTTP/1.0
    option httpclose
    option forwardfor
    server Server1 127.0.0.1:3333 cookie Server1


I hope someone knows answer :) Thanks!
Tags: Linux, Node.js, Proxy, Routing, Haproxy, Subdomain

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900