【问题标题】:Elastic-beanstalk gzip Python & Django弹性豆茎 gzip Python & Django
【发布时间】:2019-11-06 06:04:37
【问题描述】:

我正在尝试使用Apache 2.4.39 (Amazon) 为Python 2.7Django 1.11 启用gzip 压缩。

我在创建Elastic Beanstalk php 7.2 应用程序时成功启用了gzip 压缩。

我将.htaccess文件放在/etc/httpd/conf.d中并命名为compression.conf

我重启了Apache

$ sudo service httpd restart

我的 test.html 页面使用了本地 jscss 文件,这些文件现在使用 gzip 压缩。

该文件来自html5 样板的压缩部分。

但是,Elastic BeanstalkPython 2.7Django 1.11 失败了。

我把文件放在这里/etc/httpd/conf.d/compression.conf 重启阿帕奇

本地 cssjs 文件未压缩。

/var/log/httpd/error_log没有相关错误

欢迎提出任何建议。

==================================

.ebextensions 中的两个文件

来自 html5boilerplate .htaccess 文件

<----------------- start enable_mod_deflate.conf ------------------------->

# ----------------------------------------------------------------------
# | Compression                                                        |
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>

    # Force compression for mangled `Accept-Encoding` request headers
    #
    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
    # https://calendar.perfplanet.com/2010/pushing-beyond-gzipping/

    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Compress all output labeled with one of the following media types.
    #
    # https://httpd.apache.org/docs/current/mod/mod_filter.html#addoutputfilterbytype

    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE "application/atom+xml" \
                                      "application/javascript" \
                                      "application/json" \
                                      "application/ld+json" \
                                      "application/manifest+json" \
                                      "application/rdf+xml" \
                                      "application/rss+xml" \
                                      "application/schema+json" \
                                      "application/geo+json" \
                                      "application/vnd.ms-fontobject" \
                                      "application/wasm" \
                                      "application/x-font-ttf" \
                                      "application/x-javascript" \
                                      "application/x-web-app-manifest+json" \
                                      "application/xhtml+xml" \
                                      "application/xml" \
                                      "font/eot" \
                                      "font/opentype" \
                                      "font/otf" \
                                      "image/bmp" \
                                      "image/svg+xml" \
                                      "image/vnd.microsoft.icon" \
                                      "text/cache-manifest" \
                                      "text/calendar" \
                                      "text/css" \
                                      "text/html" \
                                      "text/javascript" \
                                      "text/plain" \
                                      "text/markdown" \
                                      "text/vcard" \
                                      "text/vnd.rim.location.xloc" \
                                      "text/vtt" \
                                      "text/x-component" \
                                      "text/x-cross-domain-policy" \
                                      "text/xml"

    </IfModule>

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Map the following filename extensions to the specified
    # encoding type in order to make Apache serve the file types
    # with the appropriate `Content-Encoding` response header
    # (do note that this will NOT make Apache compress them!).
    #
    # If these files types would be served without an appropriate
    # `Content-Enable` response header, client applications (e.g.:
    # browsers) wouldn't know that they first need to uncompress
    # the response, and thus, wouldn't be able to understand the
    # content.
    #
    # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
    # https://httpd.apache.org/docs/current/mod/mod_mime.html#addencoding

    <IfModule mod_mime.c>
        AddEncoding gzip              svgz
    </IfModule>

</IfModule>
 <--------------------------enable_mod_deflate.conf  ------------------------------>

<---------------------- myapp.config ------------------------------------->
 container_commands:
  01_setup_apache:
    command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf"

<---------------------- end myapp.config ------------------------------------->    

基于: http://www.tonmoygoswami.com/2013/05/how-to-enable-gzip-on-amazon-elastic.html

【问题讨论】:

    标签: python django apache gzip amazon-elastic-beanstalk


    【解决方案1】:

    最好的方法是在您的.ebextensions 目录中创建一个文件。
    使用此文件夹配置每次机器启动时将运行的所有文件。
    因此,您无需访问机器并手动更改配置。

    像这样创建一个文件.ebextensions/gzip.config

    files:
      "/etc/httpd/conf.d/enable_mod_deflate.conf":
        mode: "000644"
        owner: root
        group: root
        content: |
          <IfModule mod_deflate.c>
            AddOutputFilterByType DEFLATE text/plain
            AddOutputFilterByType DEFLATE text/html
            AddOutputFilterByType DEFLATE application/xhtml+xml
            AddOutputFilterByType DEFLATE text/xml
            AddOutputFilterByType DEFLATE application/xml
            AddOutputFilterByType DEFLATE application/xml+rss
            AddOutputFilterByType DEFLATE application/x-javascript
            AddOutputFilterByType DEFLATE text/javascript
            AddOutputFilterByType DEFLATE text/css
            AddOutputFilterByType DEFLATE image/png
            AddOutputFilterByType DEFLATE image/gif
            AddOutputFilterByType DEFLATE image/jpeg
    
            DeflateCompressionLevel 9 # Highest level
            BrowserMatch ^Mozilla/4 gzip-only-text/html
            BrowserMatch ^Mozilla/4\.0[678] no-gzip
            BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
            <IfModule mod_headers.c>
              Header append Vary User-Agent env=!dont-vary
            </IfModule>
          </IfModule>
    

    【讨论】:

    • 感谢您的建议。你在 Django 1.11 和 Python 2.7 上测试过吗?错误:Helloapp-env-2 [实例:i-03905e92f051a489c] 命令在实例上失败。返回代码:1 输出:(TRUNCATED)...apache_is_running() 文件“/opt/elasticbeanstalk/hooks/config.py”,第 280 行,在 ensure_apache_is_running 中引发 PythonHooksError(“Apache 没有运行,但它应该是。” ) config.PythonHooksError: Apache 没有运行,但它应该运行。挂钩 /opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.py 失败。有关更多详细信息,请使用控制台检查 /var/log/eb-activity.log
    • @johnc 有人说必须重新启动 Apache 才能工作。我建议您尝试将您的 .conf 完全部署为空,以检查服务器是否启动正常,并且该文件将在/etc/httpd/conf.d/ 上正确创建或复制。否则,您的 .conf 内容可能会出错。
    • 我再次尝试并消除了任何错误。但是,Apache 没有压缩文件。我现在认为这是因为它们托管在 Amazon S3 standtall-design.s3.amazonaws.com/static/css/main.css" rel="stylesheet" /> 上并且不共享域名。我通过使用 7zip 压缩文件解决了这个问题。一旦我将元数据添加到 S3(内容编码 gzip)中的 5 个文件中的每一个,它就可以工作了,并且浏览器会在网络选项卡中显示压缩和未压缩的大小。谢谢您的帮助
    • 请记住,如果您需要此 json API 休息,您必须为该 AddOutputFilterByType DEFLATE application/json 添加一行
    猜你喜欢
    • 2018-03-23
    • 2015-03-20
    • 2017-06-08
    • 2021-05-15
    • 2016-01-04
    • 2020-06-15
    • 2018-02-25
    • 2016-01-13
    • 2015-04-02
    相关资源
    最近更新 更多