Akhilesh
Akhilesh

Reputation: 1104

Nginx secure link module not working with php files but working on static files

I am using http://nginx.org/en/docs/http/ngx_http_secure_link_module.html Nginx secure link module to secure the static file downloads. It is working fine for static files.

But when I tried to implement this with php files, its not working. Basically I am using it through ajax request like

http://www.example.com/dev/serve.php?h=hash&t=timestamp

When I checked below, it was working without any restriction although it doesn't have hash and timestamp:

http://www.example.com/dev/serve.php

And for static files, it work normally i.e. when accessing any image without hash and timestamp its returning the specified bad request response:

http://www.example.com/dev/image.png

Server Config:

#
# The default server
#

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /box;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /box;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root   /box;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location /dev/ {
        root   /box;
        secure_link $arg_h,$arg_s;

        secure_link_md5 "secret$uri$secure_link_expires$remote_addr";

        if ($secure_link = "") {
            return 403;
        }

        if ($secure_link = "0") {
            return 410;
        }
    }
}

My questions:

  1. Is it not applicable for dynamic files like php?
  2. If applicable then how to implement it?

Upvotes: 2

Views: 2652

Answers (1)

Akhilesh
Akhilesh

Reputation: 1104

After a while I found the solution:

#
# The default server
#

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /box;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /box;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root   /box;

        secure_link $arg_h,$arg_s;

        secure_link_md5 "secret$uri$secure_link_expires$remote_addr";

        if ($secure_link = "") {
            return 403;
        }

        if ($secure_link = "0") {
            return 410;
        }

        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

Please note that the solution is for php files, with few modification it will work for other files also.

Upvotes: 1

Related Questions