rewrite rule on nginx for css js files not work

mohammad6006 asked:

I use laravel framework for my website on nginx server

I include html file now I need to rewrite url with another

I need this rewrite in nginx -->
or -->

my website root path is


root directories:


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;


        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;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /\.ht {
                deny all;

My answer:

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.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.