2

I use laravel framework for my website on nginx server

I need to apply rewrite rule on nginx

its a sample:

site.com/play/gamename/css.css --> site.com/uploads/games/gamename/css.css
or
site.com/play/gamename/js.js --> site.com/uploads/games/gamename/js.js

and I need to know How can I define rule to do it?

my website root path is

/var/www/html/siteuser/

root directories:

app
storage
public
-uploads
,...

in my old server with apache .htaccess:

RewriteCond %{REQUEST_URI} \.(jpg|png|css|js|appcache|xml|ogg|m4a)$
RewriteRule ^play/(.+) siteuser/public/uploads/games/$1 [L]

I test this but not work:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html/siteuser/public;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php;

        server_name .site.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                if ($uri ~ ".(jpg|png|css|js|appcache|xml|ogg|m4a)$"){
                        set $rule_0 1$rule_0;
                }    
        }
        if ($uri ~ ".(jpg|png|css|js|appcache|xml|ogg|m4a)$"){
                set $rule_0 1$rule_0;
        }
        if ($rule_0 = "1"){
                rewrite ^/play/(.+) /siteuser/public/uploads/games/$1 last;
        }


        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}
2
  • What is the problem you are having? – Michael Hampton Aug 8 '18 at 15:01
  • @MichaelHampton rewrite rule doesn't work – mohammad6006 Aug 8 '18 at 15:47
0

You should not use a rewrite for this at all. Use an aliased location instead.

For example:

location ~* ^/play/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)$ {
    alias /var/www/html/siteuser/public/uploads/games/;
}

This will serve only static files with the given file extensions for URLs beginning with /play/, from the path given in the alias, using case insensitive matching. No PHP code can run from inside /play/ in this configuration.

5
  • I need to apply it only for js css and image files – mohammad6006 Aug 8 '18 at 16:22
  • @mohammad6006 So, other files in /play/ should not be served from the uploads directory? They should be served directly from the document root? – Michael Hampton Aug 8 '18 at 16:23
  • for example there is route url : site.com/play/gameid – mohammad6006 Aug 8 '18 at 16:25
  • @mohammad6006 IN that case, you can just do a case insensitive regex match as it is now. – Michael Hampton Aug 8 '18 at 16:27
  • site.com/play/gamename/js.js 404 not found it should be site.com/upload/games/gamename/js.js – mohammad6006 Aug 8 '18 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.