G
G
greck102014-01-24 11:36:56
css
greck10, 2014-01-24 11:36:56

nginx. Processing css files with php interpreter

Please help with writing a regular expression in location for nginx.
There was a need to process css files from a certain directory by the php interpreter. (example: styles.css?423421423e3).
In .htaccess I wrote:

<Files styles.css>
  ForceType application/x-httpd-php
  AddHandler application/x-httpd-php .css
</Files>

but nginx gives css files bypassing apache.
In the nginx config I found the location:
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
root /var/www/myuser/data/www/site.ru;
access_log /var/www/httpd-logs/site.ru.access.log ;
access_log /var/www/nginx-logs/myuser isp;
}

and removed css from it| - everything worked.
But, now _all css files are processed by the php interpreter. How to make the php interpreter process css files only from a certain directory (and its subdirectories), and in other cases, these files would be given through nginx, as before?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2014-01-24
@greck10

Firstly, it is not clear now, the regular expression for file types is included in some kind of location or is registered at the top level under server.
I would write like this

location '/assets/' {
    location '/assets/do-not-serve-css' {
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
           root /var/www/myuser/data/www/site.ru;
           access_log /var/www/httpd-logs/site.ru.access.log ;
           access_log /var/www/nginx-logs/myuser isp;
        }
    }
    location '/assets/feel-free-to-serve-css' {
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
           root /var/www/myuser/data/www/site.ru;
           access_log /var/www/httpd-logs/site.ru.access.log ;
           access_log /var/www/nginx-logs/myuser isp;
        }
    }
}

Igor Sysoev, an nginx developer, recommends using this mechanism for isolating regexp locations.
According to my config, if you request a css-file at /assets/do-not-serve-css/any.css, then it will not be served through nginx and you will have to forward the processing of such a file somewhere further. If at /assets/feel-free-to-serve-css/any.css, then it will be given to nginx

N
Nikolai Vasilchuk, 2014-01-24
@Anonym

Return the location css as it was, and write a new location for your directory.

location ~* ^\/path\/to\/folder\/(.+)\.css$ {
    # Ваш конфиг обработчика php
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question