Skip to content

MrD9877/Nginx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Start Nginx

Step 1: Launch a instance

Step 2: SSH to instance

Step 3: update yum/atp -->

sudo yum update

Step 4: Download nginx

sudo apt install nginx -y

Step 5: Check if nginx working

 sudo systemctl status nginx

if not working--->

sudo systemctl start nginx
sudo systemctl enable nginx

Reverse Proxy

location / of conf.d

  location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

Load Balancing

Round Robin

upstream backend_app {
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://backend_app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Least Connections

upstream backend_app {
    least_conn;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://backend_app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

IP Hash

upstream backend_app {
    ip_hash;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://backend_app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

SSL certification

Run Command to create SSL Certificate

  1. Expires in 1 year
  2. Certificate stored in /etc/ssl/private/nginx-selfsigned.key
  3. Key stores in /etc/ssl/certs/nginx-selfsigned.crt
sudo openssl req -x509 -nodes -days 365 \
 -newkey rsa:2048 \
 -keyout /etc/ssl/private/nginx-selfsigned.key \
 -out /etc/ssl/certs/nginx-selfsigned.crt

When prompted:

  • Common Name (CN): use localhost or your server’s IP

Replace the following:

server {
    listen 443 ssl;
    server_name localhost; //or domain name

    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    location / {
        proxy_pass http://localhost:3000; //or loadbalancing
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

# Optional: Redirect HTTP to HTTPS
server {
    listen 80;
    server_name localhost;
    return 301 https://$host$request_uri;
}

Reload NGINX

sudo nginx -t
sudo systemctl reload nginx

Test

curl -k https://localhost

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published