在Nginx上为CDN启用/禁用PHP [英] Enable/Disable PHP on Nginx for CDN

查看:84
本文介绍了在Nginx上为CDN启用/禁用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台安装了Nginx的服务器.
我也有2个指向该服务器的域.(domain1.com和domain2.com).第一个域(domain1.com)是前端网站.另一个域(domain2.com)是CDN,用于存储诸如JS,CSS,图像和字体文件之类的静态内容.

我设置了域配置文件,一切运行正常.Nginx服务器上运行有PHP.

我的问题是:如何在第二个域(domain2.com)上禁用PHP,除非该请求在GET请求中包含?param = something" ?!
会是这样的:

 //PHP已禁用if($ _ GET ['param']){//启用PHP} 

或者我应该使用:

 位置〜/某物{全部拒绝} 

让PHP继续运行吗?!

注意:我需要php来处理传递给我的参数,以输出一些JS或CSS.

解决方案

使用nginx的PHP与使用Apache的PHP非常不同,因为nginx(AFAIK)没有mod_php等效项.

PHP是由完全独立的守护程序(php-fpm或将请求传递到apache服务器等)处理的.因此,您可以简单地通过让nginx处理请求而不将其传递给php来完全绕过php.-fpm或apache.很有可能您的nginx配置已经设置为仅将.php文件切换到php-fpm.

现在,如果您试图让诸如/some-style.css?foo=bar之类的请求由php处理,那么我建议您简单地将静态资源与动态资源分开.

您可以创建第三个域,或仅使用两个单独的目录.

 /static/foo.css 

vs

 /dynamic/bar.css?xyz=pdq 

然后您可以将其切换到位置块内的php.

 位置〜/static {try_files $ uri = 404;}位置〜/动态{try_files $ uri = 404;包括/etc/nginx/fastcgi_params;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;} 

通过上述配置,以/static开头的请求将忽略php,而不考虑文件扩展名(甚至是.php),而以/dynamic开头的请求将在php-fpm上传递,而不考虑文件扩展名(甚至.css)

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.

解决方案

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)

这篇关于在Nginx上为CDN启用/禁用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆