Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Nginx代理静态文件的fastcgi\u脚本\u名称不正确_Php_Nginx_Yii2 Advanced App - Fatal编程技术网

Php Nginx代理静态文件的fastcgi\u脚本\u名称不正确

Php Nginx代理静态文件的fastcgi\u脚本\u名称不正确,php,nginx,yii2-advanced-app,Php,Nginx,Yii2 Advanced App,-好的 -返回404 如何强制nginx正确处理静态文件?静态文件(如js和css)与fastcgi无关 但是,由于该文件位于/admin/assets/569a8b41/css中,但您的conf将/app/backend/web作为文档根,因此将永远找不到该文件,并且您将始终获得404,因为您要求nginx查找不存在的/app/backend/web/admin/assets/569a8b41/css/bootstrap.css 由于设置的复杂性,您可以添加另一个仅限内部的位置块来处理请求的实

-好的
-返回404


如何强制nginx正确处理静态文件?

静态文件(如js和css)与fastcgi无关

但是,由于该文件位于
/admin/assets/569a8b41/css
中,但您的conf将
/app/backend/web
作为文档根,因此将永远找不到该文件,并且您将始终获得404,因为您要求nginx查找不存在的
/app/backend/web/admin/assets/569a8b41/css/bootstrap.css

由于设置的复杂性,您可以添加另一个仅限内部的位置块来处理请求的实际服务,并从原始位置块重定向到此位置块

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/style.css
将查看
/admin/assets/569a8b41/css/style.css
,如果存在,它将提供此功能


请注意,这假设您在
/admin/assets/569a8b41
下有单独的
css
js
文件夹

获取请求的uri并将其发送到上游服务器。如果uri在上游服务器上的外观不同,则在将其传递给代理服务器之前,应
重写该uri(例如:去掉
/admin
部分)。但对于您的案例来说,这可能不是一个好主意,因为在我看来,后端php应用程序也需要
/admin
部分才能工作,因为它似乎在php框架中声明为一个路由。因此,最好的解决方案可能是只删除静态文件的
/admin
部分。更新后端服务器的nginx配置,以包含以下内容:

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

确保删除添加的echo。如果使用~sign,则表示以下模式是正则表达式,因此在本例中,nginx将获取所有URI并传递它,因此您需要在后端配置中去掉“admin”部分,我认为/app/backend/web在该路径下仅运行管理面板,因此我的建议如下:

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

location ~ ^/admin {
    proxy_pass http://127.0.0.1:81;     
}
在这种情况下,style.css文件应该位于/app/backend/web路径中

所以当你打电话的时候 loc.app/admin/style.js

在后台,nginx将发出此请求127.0.0.1:81/style.css 如果您在/app/backend/web中有一些css目录,那么您的主url应该是loc.app/admin/css/style.js=>127.0.0.1:81/css/style.css文件应该在/app/backend/web/css/style.css中


我希望这能给我一个想法。

这不是我需要的。使用代理,我试图避免“位置地狱”。
location /admin {
    proxy_pass http://127.0.0.1:81;     
}