为了账号安全,请及时绑定邮箱和手机立即绑定

nginx rewrite配置问题

nginx rewrite配置问题

PHP
慕少森 2019-03-13 17:56:11
配置一个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; } }
查看完整描述

4 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

把你的

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

改成

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

这样写 看看

查看完整回答
反对 回复 2019-03-18
?
墨色风雨

TA贡献1853条经验 获得超6个赞

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

这一段的问题
location匹配的不是目录,而是一个uri,所以nginx访问文件会出错
从你的配置看来,完全不需要添加这段配置

查看完整回答
反对 回复 2019-03-18
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

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

查看完整回答
反对 回复 2019-03-18
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

这些静态连接是php动态生成的吧,和nginx应该没关系,检查下代码

查看完整回答
反对 回复 2019-03-18
  • 4 回答
  • 0 关注
  • 901 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信