nginx rewrite does not work without "permanent" directive

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

nginx rewrite does not work without "permanent" directive

Alexander Trofimchouk

Hello!

Please would you mind helping me.

My nginx rewrite works only if I add "permanent" directive. Without it there is no rewrite seen even in browser's network log.

I have an image cache system which works this way:

- there is a folder for uploaded images ( )

- folder for small thumbnails of uploaded images ( )

- php script (Symfony controller).

If the user requests not yet existing thumbnail for already uploaded image, than the php script is called and it generates a thumbnail and stores it in a corresponding folder.

For example: User requests http://site.com/files/imagecache/thumb/1.jpg,
Nginx tries to find the file or redirects to http://site.com/www2/web/app_dev.php/image/cache?path=thumb/1.jpg

But instead I get 404 not found /files/imagecache/thumb/1.jpg – this message is provided by Symfony (PHP), not by nginx itself.

If I add “permanent” I get Symfony controller output in browser – which is OK. What did I do wrong?

 

Full nginx config with folders, symfony config and ordinary php config follows.

 

Thank you in advance!

 

server {


            root /home/anima/projects/sfedu/sfedu-php;

...       

            # SYMFONY DEV

            location ~ ^/www2/web/(app_dev|config)\.php(/|$) {

                                    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

                                    fastcgi_split_path_info ^(.+\.php)(/.*)$;

                                    include fastcgi_params;

                                    fastcgi_param  SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;

                                    fastcgi_param DOCUMENT_ROOT $realpath_root;

            }

            # PROD

            location ~ ^/www2/web/app\.php(/|$) {

                                    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

                                    fastcgi_split_path_info ^(.+\.php)(/.*)$;

                                    include fastcgi_params;

                                    fastcgi_param  SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;

                                    fastcgi_param DOCUMENT_ROOT $realpath_root;

                                    internal;

            }

            location ~ ^/www2/web {

                        allow all;

                        try_files $uri /www2/web/app.php$is_args$args;

            }

            location ~ ^/www2 {

                        deny all;

            }

    # END OF SYMFONY BLOCK

 

    location ~ (\.php$|\.php/|\.php\?) {

                        fastcgi_pass   unix:/run/php/php7.0-fpm.sock;

                        fastcgi_index  index.php;

                        include        fastcgi_params;

 

                        set $path_info "";

                        set $real_script_name $fastcgi_script_name;

                        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {

                           set $real_script_name $1;

                           set $path_info $2;

                        }

                        fastcgi_param SCRIPT_FILENAME  $document_root$real_script_name;

                        fastcgi_param SCRIPT_NAME $real_script_name;

                        fastcgi_param PATH_INFO $path_info;

                        fastcgi_param PATH_TRANSLATED $document_root$real_script_name;

            }

 

    location /files/imagecache {

                        root    /home/anima/projects/http-upload;   

                        try_files $uri @imagecache;

            }

 

            location /files {

                        root    /home/anima/projects/http-upload;          

            }

 

            location @imagecache {

                        rewrite ^/files/imagecache/(.*)$  /www2/web/app_dev.php/images/cache?path=$1 permanent; #Here should be no "permanent"

            }

 

}

 

 

Regards,

Alexander Trofimchouk.

 

 


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: nginx rewrite does not work without "permanent" directive

Francis Daly
On Fri, Dec 15, 2017 at 03:48:20PM +0300, Alexander Trofimchouk wrote:

Hi there,

> My nginx rewrite works only if I add "permanent" directive. Without it there
> is no rewrite seen even in browser's network log.

http://nginx.org/r/rewrite

"rewrite" does not directly lead to "http redirect" without specific
configuration.

> For example: User requests http://site.com/files/imagecache/thumb/1.jpg,
> Nginx tries to find the file or redirects to
> http://site.com/www2/web/app_dev.php/image/cache?path=thumb/1.jpg

No, that's not what your config says.

nginx *rewrites* to /www2/web/app_dev.php/images/cache?path=thumb/1.jpg

That rewritten request (depending on the omitted config) probably is
handled within

  location ~ ^/www2/web/(app_dev|config)\.php(/|$) {

which does the fastcgi_pass to PHP.

If you want nginx to *redirect*, you have to tell it to, using one of
the three documented methods.

> If I add "permanent" I get Symfony controller output in browser - which is
> OK. What did I do wrong?

You left off "permanent" or "redirect" or didn't start the replacement
string with "http://", if you wanted nginx to issue a redirect.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: nginx rewrite does not work without "permanent" directive

Alexander Trofimchouk
Thank you for advice!
I will add the "redirect".

Nevertheless I wonder why the following (almost similar) config works?
I get php output when requesting for http://site.com//files/imagecache/small/1.jpg
Though rewrite goes to another location with some additional parameters the configurations looks to me similar to one in the first message.

Config that works(nginx 1.8):

location ~ (\.php$|\.php/|\.php\?) {
fastcgi_pass  127.0.0.1:9002;
fastcgi_index  index.php;
include        fastcgi_params;

set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
   set $real_script_name $1;
   set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME  $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$real_script_name;
}
location /files/imagecache {
root   /.mnt/www-sfedu-data;
try_files $uri @imagecache;
}
location /files {
root   /.mnt/www-sfedu-data;
}
location @imagecache {
rewrite ^/files/imagecache/(.*)$ /php_j/imagecache/index.php?q=$1;    #no permanent or redirect here !
}


Thank you in advance!
Regards,
Alexander Trofimchouk

2017-12-15 16:18 GMT+03:00 Francis Daly <[hidden email]>:
On Fri, Dec 15, 2017 at 03:48:20PM +0300, Alexander Trofimchouk wrote:

Hi there,

> My nginx rewrite works only if I add "permanent" directive. Without it there
> is no rewrite seen even in browser's network log.

http://nginx.org/r/rewrite

"rewrite" does not directly lead to "http redirect" without specific
configuration.

> For example: User requests http://site.com/files/imagecache/thumb/1.jpg,
> Nginx tries to find the file or redirects to
> http://site.com/www2/web/app_dev.php/image/cache?path=thumb/1.jpg

No, that's not what your config says.

nginx *rewrites* to /www2/web/app_dev.php/images/cache?path=thumb/1.jpg

That rewritten request (depending on the omitted config) probably is
handled within

  location ~ ^/www2/web/(app_dev|config)\.php(/|$) {

which does the fastcgi_pass to PHP.

If you want nginx to *redirect*, you have to tell it to, using one of
the three documented methods.

> If I add "permanent" I get Symfony controller output in browser - which is
> OK. What did I do wrong?

You left off "permanent" or "redirect" or didn't start the replacement
string with "http://", if you wanted nginx to issue a redirect.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: nginx rewrite does not work without "permanent" directive

Francis Daly
On Fri, Dec 15, 2017 at 05:32:25PM +0300, Александр Трофимчук wrote:

Hi there,

> Nevertheless I wonder why the following (almost similar) config works?

I don't see it as similar. Am I missing something?

> I get php output when requesting for
> http://site.com//files/imagecache/small/1.jpg
> Though rewrite goes to another location with some additional parameters the
> configurations looks to me similar to one in the first message.

> Config that works(nginx 1.8):
>
> location ~ (\.php$|\.php/|\.php\?) {
> fastcgi_pass  127.0.0.1:9002;
> fastcgi_index  index.php;
> include        fastcgi_params;
>
> set $path_info "";
> set $real_script_name $fastcgi_script_name;
> if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
>    set $real_script_name $1;
>    set $path_info $2;
> }
> fastcgi_param SCRIPT_FILENAME  $document_root$real_script_name;
> fastcgi_param SCRIPT_NAME $real_script_name;
> fastcgi_param PATH_INFO $path_info;
> fastcgi_param PATH_TRANSLATED $document_root$real_script_name;
> }

The configuration that did not work for you was

location ~ ^/www2/web/(app_dev|config)\.php(/|$) {
  fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  fastcgi_split_path_info ^(.+\.php)(/.*)$;
  include fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
  fastcgi_param DOCUMENT_ROOT $realpath_root;
}

which looks very different from the location{} above that does work for you.

Both do a fastcgi_pass, so the real question is: what does your fastcgi
server do that is different between the two calls?

Which fastcgi_param values does your fastcgi server care about, and do
they have different values in the two cases?

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx