Alaa Badran
Alaa Badran

Reputation: 1858

Enable/Disable PHP on Nginx for CDN

I have a server with Nginx installed.
I also have 2 domains pointing to that server. (domain1.com and domain2.com). The first domain (domain1.com) is the front website. The other domain (domain2.com) is the CDN for static content like: JS, CSS, images and font files.

I setup domains config files and everything is running fine. The nginx server has PHP running on it.

My question is: How to disable PHP on the second domain (domain2.com) unless the request has "?param=something" in the GET request?!
It will be something like:

// PHP is disabled
if($_GET['param']){  
   // Enable PHP  
}

or should I use:

location ~ /something {
  deny all
}

And keep PHP running?!

Note: I need php to process the param i pass to output some JS or CSS.

Upvotes: 2

Views: 2829

Answers (1)

Joe Sniderman
Joe Sniderman

Reputation: 434

PHP with nginx is very different than PHP with Apache, since there is no mod_php equiv for nginx (AFAIK).

PHP is handled by totally separate daemon (php-fpm, or by passing the request to an apache server, etc.) As a result, you can bypass php completely simply by letting nginx handle the request without passing it off to php-fpm or apache. There is a good chance that your nginx configuration already is setup only handoff .php files to php-fpm.

Now, if you're trying to have requests such as /some-style.css?foo=bar get handled by php, then I'd suggest simply segregating static resources from dynamic ones.

You could create a third domain, or simply use two separate directories.

/static/foo.css

vs

/dynamic/bar.css?xyz=pdq

You could then handoff to php inside the location blocks.

location ~ /static {
   try_files $uri =404;
}

location ~ /dynamic {
   try_files $uri =404;
   include /etc/nginx/fastcgi_params;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

With the above configuration, requests starting with /static will bypass php regardless of file extension (even .php) and requests starting with /dynamic will be passed on the php-fpm regardless of file extension (even .css)

Upvotes: 1

Related Questions