GianFJ

GianFJ

Devops, Linux and IT in general

30 Aug 2020

Enabling CORS on Elastic Beanstalk/Nginx with Amazon Linux 2

That’s a quick tip, altough I think a lot of people are stuck with this.

On Elastic Beanstalk running Amazon Linux 2, the way to configure the frontend proxy (nginx/Apache) changed a lot from Amazon Linux 1.

The default recommendation is to create a .platform directory on the root of the source bundle, however when making any changes on a directory of a location, the deployments will fail.

This happens because the .platform/nginx/conf.d/ directory include is on the top level scope of Nginx config. A quick solution is to include the configuration on the .platform/nginx/conf.d/elasticbeanstalk/ directory which has the beanstalk settings and uses the server scope.

I’ve used the following configuration file(cors.conf):

location ~* \.(eot|otf|ttf|woff|woff2|css|js)$ {
    add_header Access-Control-Allow-Origin *;
}

And I’ve created the following directory structure in my source bundle:

~/workspace/my-app/
|-- .platform
    `-- nginx
        `-- conf.d
            `-- elasticbeanstalk
                `-- cors.conf

After deploying it to my environment, CORS was enabled and I was able to run my scripts.