<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Devashish</title>
    <description>The latest articles on DEV Community by Devashish (@devashish_trehan).</description>
    <link>https://dev.to/devashish_trehan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4280296%2F4d414818-aea4-4c4f-8e6f-980dcbe11a32.png</url>
      <title>DEV Community: Devashish</title>
      <link>https://dev.to/devashish_trehan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devashish_trehan"/>
    <language>en</language>
    <item>
      <title>From Localhost to AWS EC2: Deploying My First Full-Stack App</title>
      <dc:creator>Devashish</dc:creator>
      <pubDate>Thu, 03 Dec 2026 10:48:38 +0000</pubDate>
      <link>https://dev.to/devashish_trehan/from-localhost-to-aws-ec2-deploying-my-first-full-stack-app-516p</link>
      <guid>https://dev.to/devashish_trehan/from-localhost-to-aws-ec2-deploying-my-first-full-stack-app-516p</guid>
      <description>&lt;p&gt;I've spent over five years working primarily on frontend development, building applications with React, Next.js, and TypeScript.&lt;/p&gt;

&lt;p&gt;Recently, I decided to take one of my personal projects beyond local development and deploy the complete application on AWS EC2.&lt;/p&gt;

&lt;p&gt;The project has a Next.js frontend, a Node.js and Express backend, and a MongoDB database. I used Docker and Docker Compose for containerization, Nginx as a reverse proxy, and Let's Encrypt for HTTPS.&lt;/p&gt;

&lt;p&gt;This was my first time managing the complete deployment myself, from configuring the server to connecting the frontend, backend, database, domains, and authentication.&lt;/p&gt;

&lt;p&gt;Here's how I approached it and some of the challenges I encountered along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Tech Stack
&lt;/h2&gt;

&lt;p&gt;Here's what the deployment looks like:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Technology&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Frontend&lt;/td&gt;
&lt;td&gt;Next.js (App Router), TypeScript&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend&lt;/td&gt;
&lt;td&gt;Node.js, Express, TypeScript&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;MongoDB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;td&gt;AWS EC2 (Ubuntu)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Containerization&lt;/td&gt;
&lt;td&gt;Docker, Docker Compose&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reverse Proxy&lt;/td&gt;
&lt;td&gt;Nginx&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSL&lt;/td&gt;
&lt;td&gt;Let's Encrypt, Certbot&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I used two subdomains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;blog.example.com&lt;/code&gt; for the frontend.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;blog-api.example.com&lt;/code&gt; for the backend.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal was to keep the application services behind Nginx instead of exposing their ports directly to the internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setting Up the EC2 Instance
&lt;/h2&gt;

&lt;p&gt;I started with an Ubuntu EC2 instance and configured SSH access.&lt;/p&gt;

&lt;p&gt;One of the first things I focused on was the security group. I wanted the application to be publicly accessible without exposing every service running on the machine.&lt;/p&gt;

&lt;p&gt;I kept the public entry points limited:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Port 22 for SSH, restricted to my IP.&lt;/li&gt;
&lt;li&gt;Port 80 for HTTP.&lt;/li&gt;
&lt;li&gt;Port 443 for HTTPS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The frontend, backend, and database didn't need publicly accessible ports.&lt;/p&gt;

&lt;p&gt;I also assigned an Elastic IP so that the instance would retain a stable public IP address.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Containerizing the Application
&lt;/h2&gt;

&lt;p&gt;Both the frontend and backend were already developed, so the next step was preparing them to run in production containers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Backend
&lt;/h3&gt;

&lt;p&gt;The backend uses Node.js, Express, and TypeScript.&lt;/p&gt;

&lt;p&gt;I created a multi-stage Docker build. The first stage installs dependencies and compiles TypeScript, while the final stage contains only the production output and required dependencies.&lt;/p&gt;

&lt;p&gt;This keeps development dependencies and build tools out of the final production image.&lt;/p&gt;

&lt;p&gt;I also kept environment-specific configuration separate from the Dockerfile. I didn't want to modify the Dockerfile every time a new environment variable was introduced.&lt;/p&gt;

&lt;p&gt;Docker Compose made it easier to manage the backend and MongoDB services together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Frontend
&lt;/h3&gt;

&lt;p&gt;For Next.js, I used the standalone output configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// next.config.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"standalone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates a standalone server bundle containing the dependencies needed to run the application.&lt;/p&gt;

&lt;p&gt;The production image only needs the standalone output, static assets, and public directory.&lt;/p&gt;

&lt;p&gt;One detail worth understanding is the difference between build-time and runtime environment variables.&lt;/p&gt;

&lt;p&gt;Variables prefixed with &lt;code&gt;NEXT_PUBLIC_&lt;/code&gt; are embedded into the client bundle during &lt;code&gt;next build&lt;/code&gt;. Changing them after the image has been built won't update the already-generated JavaScript bundle.&lt;/p&gt;

&lt;p&gt;Server-only environment variables have different considerations and can be supplied at runtime, provided the application reads them at runtime rather than baking them into the build.&lt;/p&gt;

&lt;p&gt;Understanding this distinction helped me structure the frontend configuration more carefully.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Dealing With EC2 Memory Limitations
&lt;/h2&gt;

&lt;p&gt;One of the more frustrating issues came during the build and deployment process.&lt;/p&gt;

&lt;p&gt;The EC2 instance started running out of memory, and some processes were being terminated.&lt;/p&gt;

&lt;p&gt;I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;free -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to inspect memory usage.&lt;/p&gt;

&lt;p&gt;To investigate whether Linux had terminated processes through the Out Of Memory (OOM) killer, I checked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo dmesg -T &lt;span class="o"&gt;|&lt;/span&gt; grep -i -E &lt;span class="s1"&gt;'oom|killed process'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helped identify that memory pressure was causing the problem.&lt;/p&gt;

&lt;p&gt;I also learned more about swap space and how it provides additional virtual memory when physical RAM is under pressure.&lt;/p&gt;

&lt;p&gt;I configured swap to give the instance more breathing room.&lt;/p&gt;

&lt;p&gt;Swap isn't a replacement for RAM. It can help prevent processes from being killed immediately under memory pressure, but it's significantly slower than physical memory.&lt;/p&gt;

&lt;p&gt;This was a useful reminder that a Docker build that works comfortably on a development machine may not run as smoothly on a small cloud instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Docker Networking and Nginx
&lt;/h2&gt;

&lt;p&gt;Once both applications were containerized, I needed to make them accessible through the server.&lt;/p&gt;

&lt;p&gt;I wanted Nginx to be the public entry point, so I didn't expose the application ports directly.&lt;/p&gt;

&lt;p&gt;Instead, I bound them to the EC2 host's loopback interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;frontend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"127.0.0.1:3000:3000"&lt;/span&gt;

  &lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"127.0.0.1:3001:3001"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows services on the EC2 host to reach the containers through those ports without exposing them directly to the public internet.&lt;/p&gt;

&lt;p&gt;Another important distinction is that &lt;code&gt;localhost&lt;/code&gt; inside a container refers to that container itself, not the EC2 host or another container.&lt;/p&gt;

&lt;p&gt;For communication between containers on the same Docker network, I can use the Docker Compose service name as the hostname.&lt;/p&gt;

&lt;p&gt;For example, the frontend container can communicate with the backend using its service name and internal port.&lt;/p&gt;

&lt;p&gt;This is particularly useful for server-side requests made by Next.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring Nginx
&lt;/h3&gt;

&lt;p&gt;I configured Nginx to route incoming requests to the appropriate application.&lt;/p&gt;

&lt;p&gt;The frontend domain forwards requests to port 3000, while the API domain forwards requests to port 3001.&lt;/p&gt;

&lt;p&gt;A simplified frontend configuration looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//127.0.0.1:3000;&lt;/span&gt;

        &lt;span class="n"&gt;proxy_http_version&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt; $&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Real&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;IP&lt;/span&gt; $&lt;span class="n"&gt;remote_addr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;For&lt;/span&gt; $&lt;span class="n"&gt;proxy_add_x_forwarded_for&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proto&lt;/span&gt; $&lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The backend has a separate server block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//127.0.0.1:3001;&lt;/span&gt;

        &lt;span class="n"&gt;proxy_http_version&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt; $&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Real&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;IP&lt;/span&gt; $&lt;span class="n"&gt;remote_addr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;For&lt;/span&gt; $&lt;span class="n"&gt;proxy_add_x_forwarded_for&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proto&lt;/span&gt; $&lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also had to understand how Nginx's &lt;code&gt;sites-available&lt;/code&gt; and &lt;code&gt;sites-enabled&lt;/code&gt; directories work.&lt;/p&gt;

&lt;p&gt;The configuration files live in &lt;code&gt;sites-available&lt;/code&gt;, while symbolic links in &lt;code&gt;sites-enabled&lt;/code&gt; determine which configurations Nginx loads.&lt;/p&gt;

&lt;p&gt;After making changes, I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to validate the configuration before reloading Nginx.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Configuring DNS and HTTPS
&lt;/h2&gt;

&lt;p&gt;After configuring Nginx, I added DNS A records for both subdomains, pointing them to the EC2 Elastic IP.&lt;/p&gt;

&lt;p&gt;Once DNS resolution was working, I moved on to HTTPS.&lt;/p&gt;

&lt;p&gt;I installed Certbot using Snap and used its Nginx integration to obtain and install certificates from Let's Encrypt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo certbot --nginx \
  -d blog.example.com \
  -d blog-api.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certbot handled domain validation and updated the Nginx configuration to enable HTTPS.&lt;/p&gt;

&lt;p&gt;I also checked automatic certificate renewal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo certbot renew --dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helped verify that certificate renewal was configured correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Handling Authentication Cookies Across Subdomains
&lt;/h2&gt;

&lt;p&gt;Authentication was one of the more interesting parts of the deployment because it involved both browser requests and server-side requests.&lt;/p&gt;

&lt;p&gt;The frontend and backend were hosted on separate subdomains.&lt;/p&gt;

&lt;p&gt;Although browser requests to the API were working, I ran into a problem when Next.js needed to make a server-side request to refresh an expired access token.&lt;/p&gt;

&lt;p&gt;The key difference is that a browser automatically attaches eligible cookies to requests, whereas a server-side &lt;code&gt;fetch()&lt;/code&gt; doesn't automatically inherit the browser's cookie storage.&lt;/p&gt;

&lt;p&gt;I had to explicitly forward the refresh token in the request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sb"&gt;`${COOKIE_KEYS.REFRESH_TOKEN}=${refreshToken}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"no-store"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also needed to read the cookies returned by the backend and forward the &lt;code&gt;Set-Cookie&lt;/code&gt; headers to the browser.&lt;/p&gt;

&lt;p&gt;Another important detail was the cookie domain.&lt;/p&gt;

&lt;p&gt;If authentication needs to work across subdomains, the cookie configuration must account for the domain scope.&lt;/p&gt;

&lt;p&gt;For example, a cookie configured for &lt;code&gt;blog-api.example.com&lt;/code&gt; won't automatically be available to &lt;code&gt;blog.example.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this setup, the parent domain &lt;code&gt;.example.com&lt;/code&gt; was the relevant scope.&lt;/p&gt;

&lt;p&gt;I also paid closer attention to &lt;code&gt;HttpOnly&lt;/code&gt;, &lt;code&gt;Secure&lt;/code&gt;, &lt;code&gt;SameSite&lt;/code&gt;, cookie paths, and CORS configuration.&lt;/p&gt;

&lt;p&gt;These settings solve different problems, and getting them right requires understanding which request is made by the browser and which is made by the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Debugging Production Issues
&lt;/h2&gt;

&lt;p&gt;One thing this deployment reinforced is that a running container doesn't necessarily mean a working application.&lt;/p&gt;

&lt;p&gt;For example, when I encountered a 504 Gateway Timeout, I didn't immediately assume Nginx was the problem.&lt;/p&gt;

&lt;p&gt;I tested the upstream services directly from the EC2 instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -I --max-time 10 http://127.0.0.1:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And separately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -I --max-time 10 http://127.0.0.1:3001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helped isolate whether the issue was with the application itself or the reverse proxy.&lt;/p&gt;

&lt;p&gt;The same approach applied throughout the deployment: check each layer independently before changing multiple things at once.&lt;/p&gt;

&lt;p&gt;Docker logs, Nginx logs, system memory information, DNS resolution, and direct HTTP requests all became part of the debugging process.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Took Away From This Deployment
&lt;/h2&gt;

&lt;p&gt;Some of the most useful things I worked through were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building production-ready Docker images for Next.js and Node.js.&lt;/li&gt;
&lt;li&gt;Understanding build-time versus runtime environment configuration.&lt;/li&gt;
&lt;li&gt;Managing containers and services through Docker Compose.&lt;/li&gt;
&lt;li&gt;Understanding Docker networking and port binding.&lt;/li&gt;
&lt;li&gt;Configuring Nginx as a reverse proxy.&lt;/li&gt;
&lt;li&gt;Managing DNS records and SSL certificates.&lt;/li&gt;
&lt;li&gt;Diagnosing memory pressure and configuring swap.&lt;/li&gt;
&lt;li&gt;Understanding cookie behaviour across subdomains.&lt;/li&gt;
&lt;li&gt;Handling authentication refresh requests between the browser, Next.js server, and backend.&lt;/li&gt;
&lt;li&gt;Debugging issues across application, container, and infrastructure layers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;The deployment is working, but there are still improvements I want to make.&lt;/p&gt;

&lt;p&gt;My next step is setting up CI/CD so that changes can be built and deployed without manually connecting to the EC2 instance and running each command.&lt;/p&gt;

&lt;p&gt;I also want to improve production monitoring and make the deployment process more reliable.&lt;/p&gt;

&lt;p&gt;This experience helped me look beyond application code and understand more of the systems involved in running an application in production.&lt;/p&gt;

&lt;p&gt;Building an application is one part of the job. Making sure it can run reliably, communicate with other services, handle authentication, and remain accessible introduces an entirely different set of considerations.&lt;/p&gt;

&lt;p&gt;If you've self-hosted a Next.js and Node.js application on EC2, I'd be interested to know how you handle CI/CD, monitoring, and deployments.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>docker</category>
      <category>nextjs</category>
      <category>node</category>
    </item>
  </channel>
</rss>
