简体   繁体   中英

Nginx, always use specific file for specific extension

Not sure how to word this or the best way to ask but I am looking for a way in nginx to always run a specific file for php.

Basically if the file is a php file ALWAYS run another file instead.

So the request is to www.domain.com/info.php then always run /home/user/system/request.php instead.

The reason for this is I have an older tool I need to do adjustments to and want to split off my code to gradually make it compatible with laravel. So rather then adding a require at the top of all 3000+ files I would rather load my code, then by using the path load that specific file (this way gradually bits and pieces can be rebuilt in laravel seamlessly).

Any ideas?

Try a try_files directive:

location ~ \.php$ {
    try_files $uri /home/user/system/request.php;
    # your php-fpm config
}

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

I assume that the actual php files exist on your server but despite this, you want to use request.php instead for the reasons you outlined.

If this is the case, then the try_files solution given will result in a redirect loop or just serve the original file.

What you need is a solution that will only redirect a php request if not for request.php as shown below (The order is important).

location ~ (/request\.php)$ {
    # Your PHP params go here
}
location ~ \.php)$ {
    return 301 http://example.com/request.php;
}

This assumes request.php is under the document root.

With this, a request for request.php will match the two locations blocks but the first in the conf file will be triggered and will run PHP.

A request for SomeOtherFile.php will match the second location block and be redirected to request.php along with the incoming GET params.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM