Loading...

How to redirect traffic to https for a Laravel AWS ElasticBeanstalk deployment on Nginx

Last updated: Dec 21st 2024
Laravel | AWS ElasticBeanstalk | Nginx | SSL

If you are working with Laravel or any PHP application and would like to deploy it to AWS EC2 instance through an ElasticBeanstalk, you could do that for sure.  Assuming that you are deploying it on the Nginx webserver, there are a few things that you need to consider.

  1. You would have to tell the webserver to read the .php files
  2. You need to add the SSL certificates onto the server.  This can be done through the .ebextension configration
  3. You need to create a redirect on the NGinx server for all http requests to https 

 

AWS allows you to overwrite the NGINX conf file which can be done simply by placing a file in the following path.

.platform\nginx\nginx.conf 

Put the following code which listens to both ports 80 and 443.  Also the config tells the server to use the location path for php files. 



#Elastic Beanstalk Nginx Configuration File
#Updated by SYED

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    32136;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    include       conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }

    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;
        server_name _;
        return 301 https://$host$request_uri;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }

    server {
        listen              443 ssl;
		ssl_certificate     /etc/pki/tls/certs/server.crt;
		ssl_certificate_key /etc/pki/tls/certs/server.key;
        access_log    /var/log/nginx/access.log main;


        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
           try_files $uri $uri/ /index.php$is_args$args;
        }

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }
}



SYED ABDUL QUADER Published on 08 Nov 2020
Edit
Comments
Guest User