joseamtalavera / lhm_inmo_app

0 stars 0 forks source link

nginx EC2 #5

Closed joseamtalavera closed 1 week ago

joseamtalavera commented 1 week ago

Step 1: Install Nginx on EC2

SSH into your EC2 instance.

Run the following commands to install Nginx:

sudo apt update sudo apt install nginx -y

Step 2: Configure Nginx for Reverse Proxy

Open the Nginx configuration file:

sudo nano /etc/nginx/sites-available/default sudo cat /etc/nginx/sites-available/default

Replace the default configuration with a reverse proxy configuration for your frontend and backend. Here’s an example setup:

server { listen 80; server_name 13.53.38.238;

# Redirect traffic for frontend
location / {
    proxy_pass http://localhost:3010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

# Redirect traffic for backend API
location /api/ {
    proxy_pass http://localhost:5010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

Step 3: Test and Restart Nginx

Test the Nginx configuration to ensure there are no syntax errors:

If everything is OK, restart Nginx to apply the new configuration:

Check the status

Reload to apply all the changes

If everything is ok, the site is been rendered in http://13.53.38.238

Step 4: Nginx after set up app.lhainmobiliaria.es

When using an AWS Load Balancer (ELB) with SSL termination, the ELB manages incoming traffic on port 443 and forwards it to NGINX on port 80. In this case, NGINX will be unaware of the original HTTPS request, as the load balancer handles SSL termination.

The new Nginx document will be:

server { listen 80;

# Frontend routing
location / {
    proxy_pass http://localhost:3010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

# Backend API routing
location /api/ {
    proxy_pass http://localhost:5010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

Test the Nginx configuration to ensure there are no syntax errors:

- sudo nginx -t

If everything is OK, restart Nginx to apply the new configuration:

- sudo systemctl restart nginx

Check the status

- sudo systemctl status nginx

Reload to apply all the changes

- sudo systemctl reload nginx

The site should be rendering in https://app.lhainmobiliaria.es