• 當前位置:首頁 > IT技術 > 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/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码