Forward http port to another port in same machine according http header?

as you have two https sites you need to have two virtual hosts so no need to examine http header again. As in reply pointed by Gerald above the best is to setup it this way (for nginx):

server {
  listen 443;
  server_name foo.me;
  root /var/www/html;
  
  # SSL options left out for simplicity

  location / {
    proxy_pass http://localhost:8433/;
  }
}

server {
  listen 443;
  server_name bar.me;
  
  # SSL options left out for simplicity
  
  location / {
    proxy_pass http://localhost:9433/;
  }
}

same for port 80 - hust replace number in "listen" and don't put SSL related directives