G
G
germn2012-09-05 16:20:42
Nginx
germn, 2012-09-05 16:20:42

How to use rewrite in Nginx to return the result of test.js.php execution instead of test.js?

Task: when requesting site.ru/test.js , the completed /php/test.js.php with content-type application/x-javascript should be returned .

In Apache it worked like this:

RewriteEngine on
RewriteRule ^test.js php/test.js.php [L]

It works: we drive site.ru/test.js into the browser , we get the completed test.js.php with content-type application/x-javascript .

I'm trying to do in Nginx:

location / {
...

rewrite ^/test.js /php/test.js.php break;
}

location ~ \.php$ {
 ...

fastcgi_pass   unix:/tmp/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}

php is given as text, content-type is the one specified in the config as default_type

I try it differently:

location / {
...
fastcgi_pass   unix:/tmp/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;

rewrite ^/test.js /php/test.js.php break;
}

Even better - /php/test.js.php is executed and returned when /test.js is requested , but content-type is still default_type , not application/x-javascript .

Can you please tell me how to solve the problem?

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Silver_Clash, 2012-09-05
@Silver_Clash

This may sound stupid (I've only been using nginx for a couple of weeks), but wouldn't it be easier/better to make a separate location for this file instead of "rewrite ^/test.js /php/test.js.php break;"?

D
Dmitry T., 2012-09-05
@tyzhnenko

location /test.js {
  fastcgi_pass fastcgi;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /php/test.js.php;
}

I haven't used fastcgi with nginx, so I can't tell you more. Perhaps it will be necessary to correct something with fastcgi parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question