1

I've tried already several ways that I've found over the internet but I still didn't manage to work.

What I want is, i have a server configuration for the url: www.domain.com where I have installed and configured a Drupal website.

I also have a subdomain, api.domain.com where I have installed and running a Laravel application.

IMPORTANT:

  • And each of this projects, have it's own nginx configurations.
  • Both projects are siblings projects at my Root directory.

What I need

That all requests for the URL www.domain.com/api are requested to the subdomain api.domain.com and if is a specific url like www.domain.com/api/option1/options2 request api.domain.com/option1/option2 without redirecting the user.

So I basically I need that the NGINX configuration for the Drupal ignore all the requests to the /api URL and request to the subdomain

Why I need

I need to have all subdomains and everything else under the same domain, because I only have the SSL to use on www.domain.com and not *.domain.com


I don't know if this is the best approach for my problem, but if you have any other suggestions I'm open.

Thanks in advance.


My own Answer

I couldn't reply my own question so here is the answer I manage to work.

location   ^~ /api/  {
    rewrite ^/api/?(.*)$   /$1  break;
    proxy_pass http://api.domain.com;
}

And this will also force the final trailing slash.

If you don't want to force the trailing slash just remove the last one at the location

1 Answers1

0

well there's 2 ways, simplest would be creating a location in the root domain that proxy's to the api subdomain

location /api {
  rewrite ^/api(.*)$ $1 last; # removing the `/api` prefix from the URI before proxying
  proxy_pass http://api.example.com;
}

The second method is to write the whole configuration of the subdomain inside that location, to avoid doing that proxying

location ^~ /api(.*) {
  root /path/to/api/root;
  # add any config you believe should be here
  try_files $uri $uri/ /index.php$1; # depends on your api way of functioning
}

Tell me if I misunderstood something or got something wrong.

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • Thanks for your quick reply. So I decided to go with the first option, so I can keep the NGINX configurations for each project in separated files, but I got this error for the first option: 2014/02/01 15:59:01 [error] 21942#0: *4 the rewritten URI has a zero length, client: 127.0.0.1, server: www.domain.com, request: "GET /api HTTP/1.1", host: "www.domain.com" – Gabriel Passarelli Feb 01 '14 at 18:00
  • ok try `domain.dom/api/` with an extra slash, does that work ? – Mohammad AbuShady Feb 01 '14 at 18:02
  • No, same error: 1 the rewritten URI has a zero length, client: 127.0.0.1, server: www.domain.com, request: "GET /api/ HTTP/1.1", host: "www.domain.com" – Gabriel Passarelli Feb 01 '14 at 18:07
  • forgot using `last` in the rewrite, fixed it in my answer, does that make any difference ? – Mohammad AbuShady Feb 01 '14 at 18:11
  • Mohammad, thanks again for helping me with this, but still happening the same error. – Gabriel Passarelli Feb 01 '14 at 18:15
  • ok, if this last change doesn't work then I don't know why it's not working, so i hope it does – Mohammad AbuShady Feb 01 '14 at 18:17
  • Still having the same error...But than I decided to start trying everything and managed something to work, I'll answer bellow – Gabriel Passarelli Feb 01 '14 at 18:28
  • I couldn't answer because I have a new account, could you please see my updated question and see if what I got is correct? Also how do I force the trailing slash at the end, to make www.domain.com/api be forced to www.domain.com/api/ – Gabriel Passarelli Feb 01 '14 at 18:32