当前位置:首页 > 科技新闻 > Web编程 > 正文

PhpStudy2018开启gzip压缩功能
2022-05-11 11:09:48

PhpStudy是一款非常方便的搭建PHP动态网站或者服务器的国产集成软件包,包含apache、mysql、php等。
注意这里的PhpStudy是Apache+php解决方案,由于Apache的使用较多些,以下的所有示例均是针对Apache,当然,只要是Apache,不管是自己独立安装,还是选择象我这样的懒人方式,集成式一键安装。配置均相同。
要让apache支持gzip功能,要用到deflate_Module和headers_Module。打开apache的配置文件httpd.conf,大约在105行左右,找到以下两行内容:(这两行不是连续在一起的)
1 #LoadModule deflate_module modules/mod_deflate.so
 
2 #LoadModule headers_module modules/mod_headers.so
然后将其前面的“#”注释删掉,表示开启gzip压缩功能。开启以后还需要进行相关配置。
如果只需配置http的话只需在httpd-vhosts.conf内相对应的域名下配置如下:

<VirtualHost *:80>
DocumentRoot "E:phpStudyPHPTutorialWWWweb_allaqxc"
ServerName www.xxxx.com
RewriteEngine on
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).css $1.css.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).js $1.js.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule .css.gz$ - [T=text/css,E=no-gzip:1,E=is_gzip:1]
RewriteRule .js.gz$ - [T=text/javascript,E=no-gzip:1,E=is_gzip:1]
Header set Content-Encoding "gzip" env=is_gzip
</VirtualHost>

如果配置https的话只需在httpd-ssl.conf内相对应的域名下配置如下:

<VirtualHost *:443>
DocumentRoot "E:phpStudyPHPTutorialWWWweb_allaqxc"
ServerName www.xxxx.com
SSLEngine on
SSLProtocol TLSv1 TLSv1.1 TLSv1.2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile "E:phpStudyPHPTutorialApachecertwww.xxxx.com_public.crt"
SSLCertificateKeyFile "E:phpStudyPHPTutorialApachecertwww.xxxx.com.key"
SSLCertificateChainFile "E:phpStudyPHPTutorialApachecertwww.xxxx.com_chain.crt"
RewriteEngine on
#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).css $1.css.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).js $1.js.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule .css.gz$ - [T=text/css,E=no-gzip:1,E=is_gzip:1]
RewriteRule .js.gz$ - [T=text/javascript,E=no-gzip:1,E=is_gzip:1]
Header set Content-Encoding "gzip" env=is_gzip
<Directory "E:phpStudyPHPTutorialWWWweb_allaqxc">
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>

最后一步尤为重要不要忘记!

在你域名指向的项目根目录下创建一个.htaccess文件里面内容如下:注意我这里只配置了css文件和js文件配置,如果还需配置别的后缀类型文件直接加上即可

# AddEncoding allows you to have certain browsers uncompress information on the fly.
AddEncoding gzip .gz

#Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).css $1.css.gz [QSA]

# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.*).js $1.js.gz [QSA]

# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule .css.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule .js.gz$ - [T=text/javascript,E=no-gzip:1]

本文摘自 :https://www.cnblogs.com/