How to setup Nginx

Photo by imgix on Unsplash

How to setup Nginx

·

3 min read

To configure Nginx to proxy requests to your server running on port 3000 and attach the domain name api.example.com, follow these steps:

Step 1:

Installation

On Ubuntu/Debian

  1. Update your package lists:

     sudo apt update
    
  2. Install Nginx:

     sudo apt install nginx
    
  3. Start and enable Nginx:

     sudo systemctl start nginx
     sudo systemctl enable nginx
    

On CentOS/RHEL

  1. Install the EPEL repository (if not already installed):

     sudo yum install epel-release
    
  2. Install Nginx:

     sudo yum install nginx
    
  3. Start and enable Nginx:

     sudo systemctl start nginx
     sudo systemctl enable nginx
    

Step 2: Configure DNS

Ensure that the DNS A record for api.example.com points to the IP address of your server. This configuration is typically done through your domain registrar's control panel.

Step 3: Create an Nginx Configuration for Your Domain

  1. Create a configuration file for your domain:

     sudo vim /etc/nginx/conf.d/api.example.com.conf
    
  2. Add the following configuration to the file:

     server {
         listen 80;
         server_name api.example.com;
    
         location / {
             proxy_pass http://localhost:3000;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto $scheme;
         }
     }
    

    This configuration tells Nginx to listen for requests on port 80 for api.example.com and proxy those requests to your server running on http://localhost:3000.

Step 4: Test and Reload Nginx Configuration

  1. Test the Nginx configuration for syntax errors:

     sudo nginx -t
    
  2. If the test is successful, reload Nginx to apply the new configuration:

     sudo systemctl reload nginx
    

To secure your domain with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate.

  1. Install Certbot and the Nginx plugin:

    For Ubuntu/Debian:

     sudo apt install certbot python3-certbot-nginx
    

    For CentOS/RHEL:

     sudo yum install certbot python3-certbot-nginx
    
  2. Obtain and install the SSL certificate:

     sudo certbot --nginx -d api.example.com
    

    Follow the prompts to complete the installation. Certbot will automatically configure Nginx to use the SSL certificate.

  3. Verify that your SSL certificate is working by visitinghttps://api.example.comin your web browser.

Example Full Nginx Configuration with HTTPS

If Certbot successfully installed your SSL certificate, your Nginx configuration file (/etc/nginx/conf.d/api.example.com.conf) will look similar to this:

server {
    listen 80;
    server_name api.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name api.example.com;

    ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This configuration includes both HTTP to HTTPS redirection and the SSL setup for secure connections.

Conclusion

By following these steps, you can configure Nginx to serve your application running on port 3000 and attach the domain name api.example.com. Additionally, securing your domain with HTTPS ensures encrypted communication between clients and your server.