鍍金池/ 問(wèn)答/PHP  Linux/ nginx rewrite配置問(wèn)題

nginx rewrite配置問(wèn)題

配置一個(gè)vhost,開(kāi)啟rewrite隱藏index.php,頁(yè)面能正常訪問(wèn)
但是發(fā)現(xiàn)css、js、jpg等靜態(tài)資源路徑有問(wèn)題:變成了域名+文件絕對(duì)路徑

http://www.guestbook-dev.com/Users/haotao/www/Study/guestbook-dev/Public/asset/vendor/bootstrap3/css/bootstrap.min.cs

請(qǐng)教一下該如何配置才能讓靜態(tài)資源正確訪問(wèn)呢?

server {
        listen  80;
        server_name www.guestbook-dev.com;
        set $root_path '/Users/haotao/www/Study/guestbook-dev/Public';
        root $root_path;

        index index.php index.html index.htm;

        try_files $uri $uri/ @rewrite;

        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }

        location ~ \.php {

            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index /index.php;

            fastcgi_split_path_info       ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO       $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include                       fastcgi_params;
        }

        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }

        location ~ /\.ht {
            deny all;
        }
}
回答
編輯回答
礙你眼

把你的

location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }

改成

location /
         {
             if (!-e $request_filename) {
                 rewrite ^(.*)$ /index.php?s=/$1 last;
                 break;
             }
         }

這樣寫(xiě) 看看

2017年7月27日 01:07
編輯回答
心悲涼

這個(gè)配置實(shí)際上是我從一個(gè)laravel站拷貝過(guò)來(lái)的 只是改了文件root地址
但是laravel站運(yùn)行很正常 靜態(tài)資源路徑也正確
這個(gè)就不知道為什么就不行

2018年3月10日 01:00
編輯回答
心夠野

這些靜態(tài)連接是php動(dòng)態(tài)生成的吧,和nginx應(yīng)該沒(méi)關(guān)系,檢查下代碼

2017年3月17日 12:25
編輯回答
兔囡囡
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }

這一段的問(wèn)題
location匹配的不是目錄,而是一個(gè)uri,所以nginx訪問(wèn)文件會(huì)出錯(cuò)
從你的配置看來(lái),完全不需要添加這段配置

2018年8月15日 02:13