nginx rewrite配置问题

配置一个vhost,开启rewrite隐藏index.php,页面能正常访问
但是发现css、js、jpg等静态资源路径有问题:变成了域名+文件绝对路径

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

请教一下该如何配置才能让静态资源正确访问呢?

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;
        }
}
慕少森
浏览 902回答 4
4回答

RISEBY

把你的 location @rewrite { rewrite ^/(.*)$ /index.php?_url=/$1; } 改成 location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } } 这样写 看看

墨色风雨

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { root $root_path; } 这一段的问题location匹配的不是目录,而是一个uri,所以nginx访问文件会出错从你的配置看来,完全不需要添加这段配置

肥皂起泡泡

这个配置实际上是我从一个laravel站拷贝过来的 只是改了文件root地址但是laravel站运行很正常 静态资源路径也正确这个就不知道为什么就不行

江户川乱折腾

这些静态连接是php动态生成的吧,和nginx应该没关系,检查下代码
打开App,查看更多内容
随时随地看视频慕课网APP