2

我有一个运行Elastic Beanstalk的EC2实例。我想启用GZIP压缩,并且我知道我需要修改我的.htaccess文件。AWS EC2上的.htaccess在哪里EB

我读过.htaccess文件位于应用程序安装的根文件夹中。但是,我似乎无法找到它。 (我是新手,不知道根文件夹在哪里)。

我能够通过Java 8应用程序ssh登录运行Apache Tomcat 8的服务器。

问题

凡位于.htaccess

这是我在我的根:

$ ls 
bin cgroup etc lib local  media opt root sbin  srv tmp var 
boot dev  home lib64 lost+found mnt proc run selinux sys usr 

UPDATE

我有一个.ebextensions/tomcat-settings.config现在的作品。它启用GZip压缩。

option_settings: 
    aws:elasticbeanstalk:environment:proxy: 
    GzipCompression: 'true' 
    ProxyServer: nginx 
    aws:elasticbeanstalk:environment:proxy:staticfiles: 
    /pub: public 

虽然这似乎并不压缩.svg文件。所以,如果有可能,我想有以下,但不知道在哪里添加:

## EXPIRES CACHING ## 
<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresByType image/jpg "access 1 year" 
ExpiresByType image/jpeg "access 1 year" 
ExpiresByType image/gif "access 1 year" 
ExpiresByType image/png "access 1 year" 
ExpiresByType image/svg "access 1 year" 
ExpiresByType text/css "access 1 month" 
ExpiresByType text/html "access 1 month" 
ExpiresByType application/pdf "access 1 month" 
ExpiresByType text/x-javascript "access 1 month" 
ExpiresByType application/x-shockwave-flash "access 1 month" 
ExpiresByType image/x-icon "access 1 year" 
ExpiresDefault "access 1 month" 
</IfModule> 
## EXPIRES CACHING ## 
+0

我对豆茎不熟悉,但尝试添加-a开关到ls,因为.htaccess是一个隐藏文件。 ls -a允许您查看所有文件,包括隐藏的文件。 –

回答

0

你需要上传.htaccess文件或让它在你的EC2实例在你的根文件夹上你上传应用程序,如果您使用的是elasticick beanstalk,则需要将.htaccess文件添加到根文件夹中的.zip文件。

+0

嗨Brayan,谢谢你的建议。我在'src/main/webapp'的根目录中添加了'.htaccess'。这意味着当我执行'mvn install'时,它会建立到我的'war'文件的根目录。然后部署到Tomcat服务器。但是当我测试页面时,它没有被压缩。当我在服务器上ssh时,我在'./tmp/deployment/application/ROOT/.htaccess'上找到它。 (我将以下内容添加到我的'.htaccess'文件中:https://www.clayharmon.com/words/posts/enabling-gzip-compression-on-ec2)。你知道为什么它不压缩页面吗? – Richard

+0

我遵循以下建议,它适用于我。 (在tomcat-settings.config文件中添加了“webapp”根目录)http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-platform.html – Richard

0

如何通过ssh或.ebextensions文件配置ec2实例? 如果您通过ssh配置实例并且您使用的是弹性环境,那么当实例降级或更改时,配置将被删除,如果ec2只使用一个实例,配置将继续工作。

如果您需要通过.ebextensions配置它,您需要在.zip文件中创建一个名为.ebextensions的文件夹,在该文件夹内需要创建两个名为enable_mod_deflate.conf和myapp.config的文件。

enable_mod_deflate.conf的内容:

# mod_deflate configuration 
    <IfModule mod_deflate.c> 
    # Restrict compression to these MIME types 
    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 

    # Level of compression (Highest 9 - Lowest 1) 
    DeflateCompressionLevel 9 

    # Netscape 4.x has some problems. 
    BrowserMatch ^Mozilla/4 gzip-only-text/html 

    # Netscape 4.06-4.08 have some more problems 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 

    # MSIE masquerades as Netscape, but it is fine 
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html 

    <IfModule mod_headers.c> 
    # Make sure proxies don't deliver the wrong content 
    Header append Vary User-Agent env=!dont-vary 
    </IfModule> 

    </IfModule> 

myapp.config的内容:

container_commands: 
     01_setup_apache: 
      command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf" 

然后,你需要重新启动服务器。

+0

Hi Brayan,感谢全面的答案。我已经有了一个'.ebextensions/tomcat-settings.config'文件,可以成功进行GZip压缩(请参阅上面的UPDATE)。所以,我按照上面的建议添加'enable_mod_deflate.conf'和'myapp.config'。但是,当我在我的网站(www.thewhozoo.com)上运行Google PageSpeed Insights时,我没有得到任何改进。 – Richard