Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
What is Nginx (Web Server) and how to install it ?
Next article icon

What is Nginx (Web Server) and how to install it ?

Last Updated : 09 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

What is a Web Server?

Whenever you open your browser, type a URL and then click enter. Basically, you are requesting the contents of that URL. Ever wondered where the content are? Yes, you're right those are contents placed on remote computers which, after accepting your request, send the contents of that URL back as a response.

Web Servers are computers that deliver the requested web pages. Every web server has an IP address and domain name. 

Let us understand web servers as an example.

You open your favorite browser and type www.geeksforgeeks.org/c-plus-plus and click enter to see resources on C++. In the above URL, www.geeksforgeeks.org is the domain name, and /c-plus-plus is the page you want to see.  

So www.geeksforgeeks.org will route your request to the webserver and that will, in turn, see the file system that you're requesting. In our case, it is /c-plus-plus. 

Image for illustration

What is Nginx?

It is open-source software designed for maximum performance and stability. Let's see basically why we need it basically see how we can benefit from this.

Why do we need a dedicated webserver?

Suppose we develop our application on Django or Node. All such frameworks have built-in development servers to host your project. But whenever someone tries to host that application on the production level with built-in development servers, you'll get a tough time handling all the requests while experiencing downtimes while just handling 30-40 concurrent requests.

Nginx is a dedicated web server that has solved efficiency issues and provided us with an optimum way to handle 1000s of requests concurrently.

Web server for reverse proxy, caching, and load balancing. 

The reverse proxy accepts a request from a client, forwards the request to a server that can fulfill it, and returns the response from the server to the client.

Caching is a technique that stores a copy of a given resource and serves it back when requested. When a web cache has a requested resource in its store, it intercepts the request and returns its copy instead of re-downloading from the originating server.

A load balancer distributes the incoming client requests to a group of servers, in which it can handle concurrent requests without experiencing load on a particular server.

Other features of Nginx are as follows:

  • It provides HTTP server capabilities.
  • Designed to provide stability and maximum performance.
  • Functions as a proxy server for email (IMAP, POP3, and SMTP).
  • It uses an event-driven and non-threaded architecture to provide less CPU computation per request served.
  • It provides scalability.
  • Reduces wait time for the client.
  • Upgrades can be done while Nginx is hosting a website without any downtime.

I hope you're convinced now why we should use a dedicated web server in our production application. 

 

Nginx Architecture 

For understanding the further configuration settings of Nginx, we need to get a glimpse of Nginx understands before that.

Nginx uses the Master-Slave architecture, where we have a master who reroutes our request to any of the workers under it by distributing the load on the server, then the Proxy cache is looked for faster response, else after failing to do So the webpage is loaded from the memory itself. An image demonstration will help to understand this structure more clearly. 

Nginx Architecture 

Some Configuration Settings for Nginx 

The main settings required for Nginx are stored in a file named nginx.conf. This file is mainly structured into many contexts.

worker_processes: A worker process is basically the number of workers that a master will control. To be more precise, it is a single-threaded process. So if you have a multi-core processor like 8-cores, you can set 8 worker_processes to fully utilize disk bandwidth and help it in doing CPU-intensive work.

worker_connections: It is the number of simultaneous connections with different clients. The default is 512 or 1024. So, suppose you have 8 worker_processes and 512 worker_connections, the maximum clients you can serve is worker_processes * worker_connections, i.e. 8*512 = 4096 connections.

access_log & error_log: By default, the Nginx server writes its events in 2 types of logs as mentioned. The activities of all visitors, requesting data from the server are logged in access_log, you can find which files were accessed and how Nginx responded to them, the IP of the client, and a lot more. But if Nginx faces any issues and glitches, then it will log all this information in the error log for further debugging.

gzip: If you want to juice out as much performance as available, it compresses all the response data. but it uses extensive CPU resources, which can slow down your Nginx if you use this on all items, so keep this option up and running only for large items.

How to install Nginx?

Steps to follow are:

  • Install Nginx
  • Adjust Firewall
  • Check your server

Linux (Ubuntu like distros)

First of all, open the terminal in your Linux distro and then type in the following commands to install Nginx on your system.

# Update your system   sudo apt-get update  # After updating your system     # Install nginx using CLI, press Y to allow it to install  sudo apt-get install nginx    # Enabling Firewall  sudo ufw enable

These are some steps to installing Nginx and enabling the firewall in Linux. Let's check the version and proceed ahead to starting the server.

# checking Nginx version  nginx -v  # if output is -> nginx version: nginx/1.xx.x (ubuntu)  # you have successfully installed it

Now we need to add the rules to the firewall so that your server can get requests on Http and Https ports.

# This commands tells you all the configuration   # that your firewall know which can be added  sudo ufw app list    # Here you'll see the output and from available options,  # you shall see (Nginx Full, Nginx HTTP, Nginx HTTPS)   # Let's add these rules to your firewall  sudo ufw allow 'Nginx Full'  sudo ufw allow 'Nginx HTTP'  sudo ufw allow 'Nginx HTTPS'

Now we need to see the status of the rules that we added to the firewall.

# To check status   sudo ufw status

Now we will check if our server is running. 

# To check the status of the server  sudo systemctl status nginx

Congratulations! If you can see active (running) under the Active heading, means your server is running.

Windows (7/8/10 or maybe 11)

Click here to go to the download page of Nginx, and download a mainline version (in my case it is nginx-1.21.1.zip). 

Unzip it anywhere you want, open the folder and open the Nginx application, and allow it to change the settings and rules in the firewall.

Voila! Your server is running and you can check that by going to this link in the browser: http://localhost/

Now you might be confused about where this webpage (Welcome to Nginx) is coming from. For that, let's proceed to the Conf folder in Nginx, unzip the folder, and use any editor (or notepad) to open the nginx.conf file. 

Here you can see worker_processes, worker_connections, and other discussed configuration settings. You can go under HTTP -> server -> location/  to get the location of the webpage or which routes will open a webpage from. Here it says under the root directory (here it is an unzipped folder) the HTML folder, index.html page is being served as a response. 

Hence, whenever you have started the Nginx server and while going on localhost URL in a browser, if you see Welcome to Nginx. Our server is up and running!!

Do read next article on setting up nginx on the remote server: link


Next Article
What is Nginx (Web Server) and how to install it ?

T

tewatia5355
Improve
Article Tags :
  • Operating Systems

Similar Reads

    How to Install and Configure Nginx from Source on Linux
    Nginx is written in C language by Igor Sysoev to overcome the C10K problem (i.e. Concurrently handling 10k(ten thousand) connections). The problem was how to optimize the network socket to handle numerous clients at the same time. Nginx is a solution to that problem. It is a free and open-source sof
    4 min read
    How To Install Nginx On Amazon linux ?
    Nginx, at first conveyed in 2004 by Igor Sysoev, is a decent open-source web server, switch go-between, load balancer, and HTTP store. With its occasion-driven planning, Nginx productively handles endless simultaneous affiliations, making it ideal for high-traffic districts and applications.It keeps
    6 min read
    How to run NGINX for root & non-root
    In Linux, there are two types of users namely root and non-root users. System privileges are given to the root user to perform more severe configurations as compared to non-root users. Nginx Web Server is one of the most important utilities to host the static contents on the server, running this con
    6 min read
    How to Install Nginx in AWS Linux ?
    Nginx is a simple web server that can be used for multiple purposes such as load balancer, reversal proxy & HTTP cache as well, the software was created by Igor Sysoev, a Russian developer and it was released in the year 2004. the reason why nginx is popularly used is because it is an open-sourc
    8 min read
    How to install and configure Nginx Web Server on Godaddy VPS (Ubuntu)?
    GoDaddy Server is a cloud-based hosting platform that consists of virtual and dedicated servers. The premium service includes weekly backups, 99% uptime, 24x7 Customer Support, a free encrypted SSL certificate, unlimited bandwidth, and SSD storage. For regular users, the latest generation is VPS Gen
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences