Skip to content

Nginx by kuldeep

August 3, 2024 | 12:00 AM

Nginx

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Russian developer Igor Sysoev and publicly released in 2004. Nginx is free and open-source software, released under the terms of the 2-clause BSD license.

Installation

sudo apt install nginx

Configuration and setup

  1. Nginx configuration:

    The main configuration file is usually located at /etc/nginx/nginx.conf, but it’s common practice to define server blocks in separate files within /etc/nginx/sites-available/ and create symbolic links to them in /etc/nginx/sites-enabled/.

    If you don’t have a file for your server yet, you can create one. For example:

    sudo nano /etc/nginx/sites-available/my_site
  2. Add or modify the server block to forward requests from port 80 to port 9000:

    Add the following configuration in your Nginx file:

    server {
        listen 80;
        server_name mysite.com; # Change this to your domain name or use _ for default
    
        location / {
            proxy_pass http://localhost:9000; # Assuming the service is running on port 9000
            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;
        }
    }

    Replace mysite.com with your actual domain name. If you want this configuration to be the default for any request to your server on port 80, you can use _ as the server name.

  3. Enable the configuration:

    If you created the configuration file in /etc/nginx/sites-available/, create a symbolic link to it in /etc/nginx/sites-enabled/:

    sudo ln -s /etc/nginx/sites-available/my_site /etc/nginx/sites-enabled/
  4. Test the Nginx configuration:

    Before restarting Nginx, test the configuration to ensure there are no syntax errors:

    sudo nginx -t
  5. Restart Nginx:

    If the configuration test is successful, restart Nginx to apply the changes:

    sudo systemctl restart nginx

Note:- Remove default config file from sites-enabled for reverse proxy to work properly

SSL Config

Certbox installation

sudo apt update
sudo apt install certbot python3-certbot-nginx

Use

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

Config for www and domain name

# Redirect www.hiodisha.com to hiodisha.com
server {
    listen 80;
    server_name www.hiodisha.com;

    return 301 http://hiodisha.com$request_uri;
}

# Main server block for hiodisha.com
server {
    listen 80;
    server_name hiodisha.com;

    location / {
        proxy_pass http://localhost:3006;
        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;
    }
}

Note: get certificate for both www and original domain name