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