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;
        }
}
阅读 6.6k
4 个回答

把你的

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应该没关系,检查下代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题