Nginx proxy invalid fastcgi_script_name for static files

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 process static files?

+4
source share
3 answers

Static files like 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/webas the document root, this will never be found, and you will always get 404, since you are asking nginx to look for /app/backend/web/admin/assets/569a8b41/css/bootstrap.cssone that does not exist.

- .

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;
}

, http://loc.app/admin/style.css, /admin/assets/569a8b41/css/style.css, , .

, , css js /admin/assets/569a8b41.

+4

~, , , nginx URI , "admin" backend, /app/backend/web , :

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

:

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

style.css shoulbe /app/backend/-

, loc.app/admin/style.js

nginx 127.0.0.1:81/style.css css dir /app/backend/web, url loc.app/admin/css/style.js = > 127.0.0.1:81/css/style.css /app/backend/web/css/style.css

, .

+1

proxy_pass uri . uri - , rewrite (: /admin). , , /admin backend php-, , , ​​ php-. , , /admin . nginx backend, - :

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

, echo, .

0

All Articles