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

来源:互联网 发布:ubuntu软件中心搜索框 编辑:程序博客网 时间:2024/06/04 17:53

每次到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
原创粉丝点击