8
votes

My app is built on RubyOnRails and its deployed as an elastic beanstalk app using passenger, I am trying to add headers to nginx server and restart it, here is my config file, a script from .ebextensions folder in aws elastic beanstalk:

packages: 
    yum:
        nginx: [] 

files:
    "/etc/nginx/conf.d/webapp.conf" :
        mode: "000644"
        owner: root
        group: root
        content: |
            server {

                location /assets {
                  alias /var/app/current/public/assets;
                  gzip_static on;
                  gzip on;
                  expires max;
                  add_header Cache-Control public;
                }

                location /public {
                  alias /var/app/current/public;
                  gzip_static on;
                  gzip on;
                  expires max;
                  add_header Cache-Control public;
                }

            }

# This reloads the server, which will both make the changes take affect and makes sure the config is valid when you deploy
container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

However I got this error:

[2017-12-13T06:23:48.635Z] ERROR [17344] : Command CMD-AppDeploy failed!
[2017-12-13T06:23:48.635Z] INFO  [17344] : Command processor returning results: 
{"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"container_command 01_reload_nginx in .ebextensions/01_elastic_beanstalk_webapp.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI","returncode":7,"events":[]}]}

/var/log/eb-activity.log:

[2017-12-13T06:23:48.584Z] INFO  [17344] - [Application update fix-command-nginx-reload-hope@2/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_myapp_website/Command 01_reload_nginx] : Starting activity...
[2017-12-13T06:23:48.619Z] INFO  [17344] - [Application update fix-command-nginx-reload-hope@2/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_myapp_website/Command 01_reload_nginx] : Activity execution failed, because:  (ElasticBeanstalk::ExternalInvocationError)


[2017-12-13T06:23:48.619Z] INFO  [17344] - [Application update fix-command-nginx-reload-hope@2/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_myapp_website/Command 01_reload_nginx] : Activity failed.

although if I ssh into the instance and execute sudo service nginx reload it will be executed normally.. Any idea?

EDIT

$ cat /proc/version
Linux version 4.9.43-17.39.amzn1.x86_64 (mockbuild@gobi-build-64011) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) ) #1 SMP Fri Sep 15 23:39:41 UTC 2017

deploy command:

eb deploy my-app -v

headers of requested assets:

Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/x-javascript
Date: Fri, 24 Aug 2018 11:03:50 GMT
ETag: W/"12cd8ea0-20db3"
Last-Modified: Mon, 31 Dec 1979 04:08:00 GMT
Server: nginx/1.12.1
Transfer-Encoding: chunked
Via: 1.1 8cc9957dff77c27e9931ab0aaf344ec9.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 0NlE-FiGgzczadHYeK7HMMsDsGRmaB8Sefvo89phHWw3LSx01t5rgQ==
X-Cache: Miss from cloudfront

missing headers:

   access-control-max-age: 3000
   age: 48214

the update conf file at server

$ cat /etc/nginx/conf.d/webapp.conf
server {

    location /assets {
      alias /var/app/current/public/assets;
      gzip_static on;
      gzip on;
      expires max;
      add_header Cache-Control public;
      add_header 'Access-Control-Allow-Origin' '*';
    }

    location /public {
      alias /var/app/current/public;
      gzip_static on;
      gzip on;
      expires max;
      add_header Cache-Control public;
      add_header 'Access-Control-Allow-Origin' '*';
    }

}

EDIT

service nginx configtest result:

$ sudo service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2
@simo: are you deploying via eb cli? if so, can you add which command you are using to deploy the changes? also can you reproduce the error and then run eb logs and post some of the output to your question? Thanks. - Taterhead
yes I deploy using eb cli, the command is eb deploy my-app -v I have added headers of requested assets and missing headers above, kindly see them. I think that before diving into the error of container_commands , at least to check why the missing headers are actually missed? as I have already restarted the nginx via command line successfully and still age header is missing. kindly check webapp.conf above too, please let me know if you need more info - simo
one more question: can you explain what is the intended goal of the nginx config change? - Taterhead
I need to pass access-control-max-age and age headers with the served assets to leverage browser caching - simo

2 Answers

1
votes

command: "sudo service nginx reload" is not necessary as NGINX service restarts automatically after every successful deployment. You can remove it from your config file.

You maybe experiencing a delay in the expiration of your CDN service, try flushing it's cache or testing against the EB url directly.

0
votes

I had similar issues and errors. Previously, I did need the container_commands for settings to take, but then during a big set of upgrades I started getting similar errors during deployment. Ultimately, just needed to remove the container_commands and everything worked perfectly.

Remove this section from your .ebextensions scripts:

container_commands:
  01_reload_nginx:
    command: "sudo service nginx reload"

Note: you probably want to delete the comment line above it too.