Configuring nginx config files in AWS elasticbeanstalk with .ebextensions not found
The nginx
setting you are trying to use (/etc/nginx/conf.d/myconf.conf
) is for Amazon Linux 1.
But it seems that you are using Amazon Linux 2 (AL2). Thus you should be using different files for setting nginx. For AL2, the nginx settings should be in .platform/nginx/conf.d/
, not in .ebextentions
as shown in the docs.
Therefore, you could have the following .platform/nginx/conf.d/myconfig.conf
with content:
server { listen 443; server_name localhost; ssl on; ssl_certificate /etc/pki/tls/certs/server.crt; ssl_certificate_key /etc/pki/tls/certs/server.key; ssl_prefer_server_ciphers on; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
The above is an example only of the config file. I can't verify if the setting will actually work, but you are definitely using wrong folders to set nginx options in AL2.
My recommendation would be to try to make it work manually through ssh first. You may find that you need to overwrite entire nginx setting if nothing works by providing your own .platform/nginx/nginx.conf
file.
Amazon Linux 2:you have to create file in folder the same hierarchic where you actually want to place it on Elasticbeanstalk. For example for me in EBS I need to create custom conf file under /etc/nginx/conf.d/elasticbeanstalk/
For that my folder structure will be:
and in file I can add my required configurations:
location / { try_files $uri $uri/ /index.php?$args; }
Located the type of Amazon Linux 2 (AL2) on your Elastic Beanstalk Dashboard:
Create the following directory structure from the root directory of your App:
.platform/nginx/conf.d/
Create the following file:
.platform/nginx/conf.d/01_custom_ngix.conf
add the following to your 01_custom_ngix.conf file you created:
add_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
Git commit your changes (make sure to do this before deploying because your code will not be deployed when using eb deploy unless you commit your changes)
Deploy your code:
> eb deploy
options: eb deploy --verbose --debug
Verify your http response using http cli:
> http https://name-of-nodejs-server.domain.com
output:
HTTP/1.1 200 OKAccess-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,RangeAccess-Control-Allow-Methods: GET, POST, OPTIONSAccess-Control-Allow-Origin: *Access-Control-Expose-Headers: Content-Length,Content-RangeConnection: keep-aliveContent-Length: 22Content-Type: text/html; charset=utf-8Date: Thu, 26 Aug 2021 02:33:30 GMTETag: W/"16-dDAEIDLSKDKGJGJGKDLASE"Server: nginx/1.20.0X-Powered-By: Express
DONE
Optional, you can also increase your client_max_body_size by adding the following to your 01_custom_ngix.conf:
client_max_body_size 1000M;client_body_buffer_size 100M;....add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;