nginx下配置laravel+rewrite重写

 server {
    listen 80;
    server_name ha.d51v.cn;
    #access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/demo/wechat_center/public;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ /\.ht {
      deny all;
    }
#################以下是Nginx自定义配置的#######################
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # 去除index action
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # 根据laravel规则进行url重写
      if (!-e $request_filename)
      {
          rewrite ^/(.*)$ /index.php?/$1 last;
          break;
      }
      #error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
  }

 

转载于:https://www.cnblogs.com/IT--Loding/p/6853083.html

### Laravel伪静态配置PHPStudy+Nginx环境下的URL重写规则 在PHPStudy和Nginx环境下实现Laravel项目的伪静态功能,主要涉及以下几个方面的配置: #### 1. Nginx 配置文件调整 为了支持Laravel框架的路由机制,需要对Nginx的`server`块进行适当修改。以下是基于引用中的内容以及标准实践推荐的配置方式[^3]。 ```nginx server { listen 80; server_name your-domain.com; # 替换为你的域名或IP地址 root /path/to/laravel/public; # 替换为你项目public目录的实际路径 index index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; # 关键部分:将未匹配的请求转发至index.php } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; # 确保FastCGI服务正常运行 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; # 禁止访问隐藏文件 } } ``` 以上配置的关键在于 `try_files $uri $uri/ /index.php?$query_string;` 这一行,它确保当用户请求的资源不存在时,能够正确地将请求转交给Laravel的核心入口文件`index.php`来处理。 --- #### 2. PHPStudy 中 vhosts.ini 的编辑 由于PHPStudy自带了一套虚拟主机管理工具,默认情况下可能无法完全满足自定义需求。因此建议按照以下方式进行手动配置[^3]: - 手动打开并编辑位于PHPStudy安装目录下的`vhosts/vhosts.conf` 或者 `vhosts-ini` 文件。 - 将上述完整的Nginx配置粘贴进去,并替换掉对应的根目录(`root`)和服务端名称(`server_name`)字段。 - **注意**: 不要通过PHPStudy内置的图形界面去设置这些选项,因为其默认模板可能会覆盖掉手工更改的内容。 此外,在某些特殊场景下还需要额外关注mod_rewrite模块的状态是否开启。如果遇到类似错误提示,则可以尝试参照如下修正后的`.htaccess`样式[^2]: ```apacheconf <IfModule mod_rewrite.c> Options +FollowSymLinks -MultiViews RewriteEngine On # 如果是一个已存在的目录或者文件就直接返回原路劲 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f # 否则跳转到index.php并将后续参数附加过去 RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L] </IfModule> ``` 尽管这是针对Apache服务器设计的一段代码片段,但在调试过程中也可以作为一种辅助手段验证问题所在。 --- #### 3. 测试与优化 完成上述两步操作后,请重启Nginx服务以使改动生效: ```bash # 使用命令行执行下面语句之一即可 sudo service nginx restart # 或者如果是Windows平台上的PHPStudy组件的话可以直接点击面板里的相应按钮 ``` 随后可以通过浏览器访问几个典型的测试链接确认效果如何。例如对于一个简单的控制器方法定义如下所示: ```php Route::get('/hello/{name}', function ($name) { return 'Hello '.$name.'!'; }); ``` 此时输入形如`http://your-domain.com/hello/world`这样的网址应该可以看到预期的结果页面而不是404错误信息。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值