1
votes

I have been dealing with the problem for about several hours without any success. The problem that I cannot server static files from my project.

Previously I had the following rules for the Apache server.

RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]

I have tried the following location rules in my nginx configuration

  location ~ ^/css/?(.*)$ {
        #return 200 $document_root/web/css/$1;
        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
       #try_files $uri $uri/ /index.php =404;
       try_files $document_root/web/css/$1 =404;
    }

By the way #return 200 $document_root/web/css/$1; returns the valid path.

But the request still fails.

Also I tried to use alias as follows

  location ~ ^/css/?(.*)$ {
        alias /var/wwww/myproject/web/css;
        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
        try_files $uri $uri/ /index.php =404;
    }

The same situation 404, if I change 404 in the location above it will also return another code, accordingly location statement is reached but try_files doesn't work

Please help to solve the issue, I don't know whatever to try. Thanks

1

1 Answers

0
votes

If your path to files can be constructed by concatenating the value of the root with the current URI, you should be using root and not alias. See this document for details.

Assuming that /css/foo.css is physically located at /var/www/myproject/web/css/foo.css the following example should suffice.

location /css/ {
    root /var/www/myproject/web;
    ...
    try_files $uri $uri/ /index.php;
}

You do not need to use a regular expression location and you do not need to capture part of the URI.

The try_files statement needs to end with =404 or /index.php, not both. See this document for details.