Showing posts with label request-forwarding-nginx. Show all posts
Showing posts with label request-forwarding-nginx. Show all posts

Saturday, September 30, 2017

Make nginx to pass hostname of the upstream when reverseproxying How to add a response header on nginx when using proxy_pass | Forward Custom Header from Nginx Reverse Proxy | Nginx request forward | Forward request using Nginx

Actually you can do that via proxy_set_header.

For more details look here: 

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header 


#Upstream is useful for same server
upstream localhost1 {
   server 127.0.0.1:80;  # this is new server, by IP address
}

server {
    listen   8500;
    server_name  localhost local.host;
    #access_log off;
    
    location / {
        proxy_pass http://dragon71.000webhostapp.com/;
        proxy_set_header    Nginx-Host             $host;
        proxy_set_header    Nginx-X-Real-IP        $remote_addr;
        proxy_set_header    Nginx-X-Forwarded-for  $remote_addr;
        proxy_set_header    Nginx-Target           base;
    
        location /pojo/ {
            proxy_pass http://localhost1/pkm/;
            proxy_set_header    Nginx-Host             $host;
            proxy_set_header    Nginx-X-Real-IP        $remote_addr;
            proxy_set_header    Nginx-X-Forwarded-for  $remote_addr;
            proxy_set_header    Nginx-Target           pojo;
        }
    }
    
    error_page  404               /404.html;
    error_page  500 502 503 504   /50x.html;
    location = /50x.html {
        root   html;
    }
}


And in php script:

Array
(
    [MIBDIRS] => ........
    [HTTP_HOST] => localhost
    [HTTP_NGINX_HOST] => localhost
    [HTTP_NGINX_X_REAL_IP] => 127.0.0.1
    [HTTP_NGINX_X_FORWARDED_FOR] => 127.0.0.1
    [HTTP_NGINX_TARGET] => base
    [HTTP_CACHE_CONTROL] => ....
)


If the proxy_pass directive is specified without a URI,



location /app/ {
    proxy_pass      http://127.0.0.1;
}

test.com/app/xxxxx =>  http://127.0.0.1/xxxxx

If the proxy_pass directive is specified with a URI:

location /app/ {
    proxy_pass      http://127.0.0.1/maped_dir/;
}

test.com/app/xxxxx =>  http://127.0.0.1/maped_dir/xxxxx

Forward the requested Host header:

By default, the Host header from the request is not forwarded, but is set based on the proxy_pass statement. To forward the requested Host header, it is necessary to use:

proxy_set_header Host $host;

If the location is given by regular expression, can not be a URI part in proxy_pass directive, unless there are variables in the directive.

location  ~ ^/app/(.*)$ {
    #proxy_pass   http://127.0.0.1/some_dir;    #error
    proxy_pass   http://127.0.0.1/some_dir/$1r;    #ok
}

variables in proxy_pass directive:

location ~ ^/app/(.*)$ {
    proxy_pass       http://127.0.0.1:$1;
}

test.com/app/8081 => http://127.0.0.1:8081

and:

location ~ ^/app/(.*)$ {
    proxy_pass       http://127.0.0.1:9999/some_dir/$1;
}

test.com/app/request/xxxxx => http://127.0.0.1:9999/some_dir/xxxxx

with a rewrite directive in the location:

If the rewrite rule is hit, the URI specified in the directive is ignored and the full changed request URI is passed to the server:

location  /app/ {
    rewrite    ^/app/hit/(.*)$ /hit_page.php?path=$1 break;
    proxy_pass   http://127.0.0.1:9999/some_dir/;
}
/app/hit/some/request/?name=xxxxx

=> http://127.0.0.1:9999/hit_page.php?path=some/request/&name=xxxxx

/app/not_hit/some/request/?name=xxxxx

=> http://127.0.0.1:9999/some_dir/not_hit/some/request/?name=xxxxx