Score:0

nginx setup with conditional reverse proxy for some routes

my flag

I have a laravel server fronting a wordpress hosted on another server. The laravel server is receiving all traffic. I would like to direct specific routes to the local index.php (laravel) and all others to the wordpress. Like this:

/a/* --> laravel server (localhost)
/b/* --> laravel server (localhost)
/* --> wordpress (separate server)

My nginx conf currently has this:

index index.html index.htm index.php;

location ~* /[a|b]/.+ {
  try_files $uri $uri/ /index.php?$query_string;
}

location ^~ / {
  proxy_pass https://10.0.1.44;
  proxy_set_header   Host             $host;
  proxy_set_header   X-Real-IP        $remote_addr;
  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}

The wordpress is receiving all routes. How can I force /a/* and /b/* to get served by index.php on the local server?

Score:2
us flag

The problem is location ^~ / directive. This makes the prefix match explicit and prevents nginx from looking at regular expression matches.

Try the following:

location ~*^/[ab] {
     try_files $uri $uri/ /index.php?$query_string;
}

location / {
    proxy_pass https://10.0.1.44;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}
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.