amazon-web-servicesnginxamazon-elastic-beanstalk

nginx does not restart after AWS Elastic Beanstalk update environment


My website in AWS Elastic Beanstalk was running ok after installation in php 8.1 using nginx. However a few days later the website is down due to nginx error after AWS updated the environment configuration of Elastic Beanstalk. The website error looks like:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

I have to to ssh into AWS ec2 instance and restart nginx manually to make it work using the following shell command:

sudo service nginx restart

I store my nginx.conf in .platform/nginx/nginx.conf. The nginx.conf is as following:

#Elastic Beanstalk Nginx Configuration File

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

events {
    worker_connections  1024;
}

http {
    server_tokens off;
    
    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 {

        location ^~ /pv/ {
           #  allow 192.168.1.1/24;
           deny all;
           return 404;
        }
     
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        client_max_body_size  50M;
        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_name mywebsitename.com;
    index index.php;
    error_page 404 /index.php;

    location /app/upload_video.php {
         client_max_body_size 2084M;
    }
    
    if ($http_user_agent ~ "Snabcd"){
        rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)/location/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexapp.php?c1=$1&c2=$2&c3=$3&r2=$4&r3=$5&r4=$6&f_urlseo=1&f_idxt=cpn&$7=$8 last;
        rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexapp.php?c1=$1&c2=$2&c3=$3&f_urlseo=1&f_idxt=cpn&$4=$5 permanent ;
    }

    if ($http_user_agent !~ "Snabcd"){
        rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)/location/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexpc.php?c1=$1&c2=$2&c3=$3&r2=$4&r3=$5&r4=$6&f_urlseo=1&f_idxt=cpn&$7=$8 last;
        rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexpc.php?c1=$1&c2=$2&c3=$3&f_urlseo=1&f_idxt=cpn&$4=$5 
    }

    rewrite ^/?ld/([^/]+).*/?$      https://$host/ad_detail.php?ldasid=$1 permanent; 
  
    rewrite ^/?job/sales/location/([^/]+)/([^/]+)/([^/]+).*/?$   https://$host/users/jobs/job_sales.php?r2=$1&r3=$2&r4=$3 permanent; 

    
  https://$host/ect/gamesrc/javascript/tw/index_all_games.php [L,R=301]
    if ($host ~ "192.168.|172.20.|10.0."){ 
        rewrite ^/?games/?$ https://$host/ect/gamesrc/javascript/tw/index_all_games.php permanent;
    } 
    rewrite ^/?games/?$ https://$host/gamesrc/javascript/tw/index_all_games.php permanent;

  }
}

My config file for AWS EBS inside folder .ebextensions is like this:

packages:
  yum:
    sysstat: []
Resources:
  sslSecurityGroupIngress: 
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {Ref : AWSEBSecurityGroup}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443
      CidrIp: 0.0.0.0/0
container_commands:
  01nginxrestart:
    command: "sudo service nginx restart"

What is the issue here? How comes AWS does not restart nginx automatically ?


Solution

  • I think the reason for having to restart the Nginx service manually is because the container_command is run before your application is deployed.

    Going by tthe EB developer guide on Application deployment platform hooks,

    The predeploy files run after running commands found in the container_commands section of any configuration file and before running Procfile commands.

    the Nginx service should be restarted in the postdeploy hook:

    postdeploy – Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server.

    This is the last deployment workflow step.

    Create a postdeploy hook in your project for restarting the Nginx service. mkdir -p .platform/hooks/postdeploy && touch .platform/hooks/postdeploy/01_restart_nginx.sh

    Write in the hook script the instruction to restart the Nginx service.

    cat > .platform/hooks/postdeploy/01_restart_nginx.sh <<EOF
    #!/bin/bash
    
    sudo service nginx restart
    EOF
    

    Then make the file executable

    chmod +x .platform/hooks/postdeploy/01_restart_nginx.sh