3
server {
    listen loc.app:80;    

    root /app/frontend/web;

    index index.php;

    location / {        
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ ^/admin {
        proxy_pass http://127.0.0.1:81;     
    }

    location ~* \.php$ {    
        #php conf
    }                    
}

server {
    listen 127.0.0.1:81;

    root /app/backend/web;

    index index.php;

    location / {        
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.(js|css)$ {    
        echo "$document_root"; # /app/backend/web;

        echo "$fastcgi_script_name"; # /admin/assets/569a8b41/css/bootstrap.css         
        ############################## ISSUE ##############################
        # $fastcgi_script_name must be /assets/569a8b41/css/bootstrap.css #
        ###################################################################
    }

    location ~* \.php$ {    
        #php conf
    }
}

http://loc.app/admin - ok
http://loc.app/admin/style.css - returns 404

How to force nginx to correctly handle static files ?

3 Answers 3

3
+50

Static files such as js and css have nothing to do with fastcgi.

However, since the file is in /admin/assets/569a8b41/css but your conf has /app/backend/web as the document root, then this will never be found and you will always get a 404 since you are asking nginx to look for /app/backend/web/admin/assets/569a8b41/css/bootstrap.css which doesn't exist.

Because of the comlexity of your setup, you can add another internal only location block to handle the actual serving of the request and redirect to this from the original location block.

location ~ /admin/assets/569a8b41 {
    root /admin/assets/569a8b41;        
}
location ~ ^/admin/(.+)\.(js|css)$ {    
    rewrite ^/admin/(.+)\.css$ /admin/assets/569a8b41/css/$1.css last;
    rewrite ^/admin/(.+)\.js$ /admin/assets/569a8b41/js/$1.js last;
}

A request such as http://loc.app/admin/style.css will look in /admin/assets/569a8b41/css/style.cssand if present, it will serve this.

Note that this assumes you have separate css and js folders under /admin/assets/569a8b41.

Sign up to request clarification or add additional context in comments.

1 Comment

That's not what I need. Using a proxy, I tried to avoid "location hell".
1

İf you use ~ sign it means the following pattern is a Regular Expression so in this case nginx will take the all URI and pass it so you need to get rid of the "admin" part at your backend config, i am asuming that /app/backend/web at this path only admin panel runs so here is my advise:

location ~ ^/admin {
    proxy_pass http://127.0.0.1:81;     
}

Change this to this:

location /admin {
    proxy_pass http://127.0.0.1:81;     
}

in this case your style.css file shoulbe in /app/backend/web path

as a result when you call loc.app/admin/style.js

in the background nginx will make this request 127.0.0.1:81/style.css if you have some css dir in the /app/backend/web then your main url should be loc.app/admin/css/style.js => 127.0.0.1:81/css/style.css file should be in /app/backend/web/css/style.css

i hope this gives an idea.

Comments

0

proxy_pass takes the requested uri and sends it to the upstream server. If the uri looks differently on the upstream server you should rewrite the uri before passing it to proxy(e.g.: get rid of the /admin part). But this might not be a good idea for your case, because it seems to me that you also need the /admin part for your backend php application to work, since it seems to be declared as a route in your php framework. So, maybe the best solution for you would be to remove the /admin part only for static files. Update your nginx configuration for the backend server to contain something like this:

location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
    rewrite ^/admin/(.*) /$1 last;
}

make sure you remove the echos which you added.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.