Dynamic reverse proxy , by mapping port

Hi all,

I am trying to make a dynamic reverse proxy which proxies the local
webservers on different ports

I try doing this way :

#user nobody;
worker_processes 1;

#pid logs/nginx.pid;

events {
worker_connections 1024;

http {
include mime.types;
default_type application/octet-stream;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
server_name _;
access_log /var/log/nginx/proxy.access.log;

 location /
 set $rd_port  $arg_port;
 proxy_set_header        Host    $proxy_host;


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;


So when the page is requested using eg.
., it will redirect to http service running on 12345 . It work as expect
but only for one dynamic page / requested page, If that page have source
other static contents from its own it fails .

So to be make it possible need to put ?port=12345 on every links ,
images , etc.

How can i make it work as normally ?