nginx代理httpd(mod_php)时yii2的url美化问题

每次到cdsn写博客时,肯定是全网都搜不到解决方案的,哎。


比如我要把 /index.php?r=site/about 改为 /site/about.html


yii2的url美化问题解决方案一搜一大把,nginx转php-fpm的也有,单用apache的也有(.htaccess),但偏偏没有怎样设置nginx代理httpd(mod_php)的情况。我试了N久都是404错误,后来通过对比enablePrettyUrl为true和false的两种情况,发现要想让yii2能正常识别url,关键就是SERVER信息中的REQUEST_URI必须要正确,比方说要为 /site/about.html 这样。因此nginx中不能用try_files来改写url到index.php,否则转发到后端的httpd时,识别出来的REQUEST_URI就是index.php,而不是 /site/about.html


不废话了,上配置:

Yii2:

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '.html',
            'rules' => [
            ],
        ],

Ngnix:

注意!这里对location /不像普遍的那个答案,用try_files,而是直接转发给后端的apache,让它处理

        location / {
           proxy_redirect   off;
           proxy_set_header  Host $host;
           proxy_set_header  X-Real-IP $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header  X-Forwarded-Protocol  $scheme;
           proxy_set_header  PATH-INFO $request_uri;
           proxy_pass  http://$ups;
        }
        
        location ~ \.php$ {
           try_files $uri =404;
           proxy_redirect   off;
           proxy_set_header  Host $host;
           proxy_set_header  X-Real-IP $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header  X-Forwarded-Protocol  $scheme;
           proxy_set_header  PATH-INFO $request_uri;
           proxy_pass  http://$ups;
        }

Apache:

放在.htaccess中也可以,但放httpd.conf中效率更高些

<VirtualHost *:99>
    ServerName www1.tq.com
    DocumentRoot "/www/tq/www/web"
    <Directory "/www/tq/www/web">
      Options FollowSymLinks MultiViews
      DirectoryIndex index.php index.html

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . index.php

      AllowOverride None
      Order allow,deny
      Allow from all
      Require all granted
    </Directory>
</VirtualHost>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值