Score:-1

Nginx: proxy_pass path of domain to different web server

cn flag

We plan to upgrade our web application from php native based application to PHP Framework based (Laravel) to enhance application security and performance. My task is to split traffic where every request pointed to domain app.localhost without postfix /v3 still forwarded to old application on php-native web server node, and proxy all request with /v3 path to laravel web server node. Below is my configuration that resulting all asset (css and js) and URL generated by Laravel pointed to root path.

Laravel generated URL pointed to old application

Front Proxy (Public Network)

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    # PHP Native APP
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://php-native/;
    }

    # Laravel (APP v3)
    location /v3/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://laravel/;
    }
}

Web Servers (Private Network)

php-native Web Server

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    root   /usr/share/nginx/html/webapp/app;

    location / {
        index index.php index.html index.htm;

        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php56-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/webapp/app/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

laravel Web Server

server {
    listen       80;
    server_name  app.localhost;

    root   /usr/share/nginx/html/webapp/app-v3/public;
    index index.php index.html index.htm;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   php74-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/html/webapp/app-v3/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Thanks

Update

My question is: how to split the traffic, so any request pointed to app.localhost still forwarded to php-native web server and all request pointed to app.localhost/v3 pointed to laravel web server ?

djdomi avatar
za flag
im unsure but what is the error or question that you may have? i dont see logs or issues.
Ivan Shatsky avatar
gr flag
It is your laravel app that should generate links to all the assets either using the `/v3` prefix or as the relative ones. You can also check [this](https://stackoverflow.com/a/62840133/7121513) answer (however every approach described there is just a better or worse workaround).
cn flag
@djdomi i have updated my question. thanks
Score:0
cn flag

i'm already found my solution.

Step 1: Add X-Frowarded-Prefix to Front Proxy

server {
    listen       80;
    listen  [::]:80;
    server_name  app.localhost;

    # PHP Native APP
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://php-native/;
    }

    # Laravel (APP v3)
    location /v3/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Prefix "/v3";
        proxy_pass http://laravel/;
    }
}

Step2: Overide getTrustedHeaderNames function

protected function getTrustedHeaderNames()
{
    switch ($this->headers) {
        case 'HEADER_X_FORWARDED_AWS_ELB':
        case Request::HEADER_X_FORWARDED_AWS_ELB:
            return Request::HEADER_X_FORWARDED_AWS_ELB;

        case 'HEADER_FORWARDED':
        case Request::HEADER_FORWARDED:
            return Request::HEADER_FORWARDED;

        case 'HEADER_X_FORWARDED_FOR':
        case Request::HEADER_X_FORWARDED_FOR:
            return Request::HEADER_X_FORWARDED_FOR;

        case 'HEADER_X_FORWARDED_HOST':
        case Request::HEADER_X_FORWARDED_HOST:
            return Request::HEADER_X_FORWARDED_HOST;

        case 'HEADER_X_FORWARDED_PORT':
        case Request::HEADER_X_FORWARDED_PORT:
            return Request::HEADER_X_FORWARDED_PORT;

        case 'HEADER_X_FORWARDED_PROTO':
        case Request::HEADER_X_FORWARDED_PROTO:
            return Request::HEADER_X_FORWARDED_PROTO;

        // add this to support x-forwarded-prefix
        case 'HEADER_X_FORWARDED_PREFIX':
        case Request::HEADER_X_FORWARDED_PREFIX:
            return Request::HEADER_X_FORWARDED_PREFIX;

        // add | Request::HEADER_X_FORWARDED_PREFIX
        default:
            return Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB;
    }

    return $this->headers;
}

this is based on Symfony issu symfony-issues-44572

Nikolay Baranenko avatar
jp flag
not working, returned 500 error
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.