Difference between revisions of "Nginx"
(Created page with "{{Short description|Web server and reverse proxy}} {{Other uses|NGINX (disambiguation)}} {{Infobox software | name = Nginx | logo = 150px | caption = |...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
'''Nginx''' (pronounced "engine-x") is a free, open-source, high-performance HTTP and reverse proxy server, as well as a mail proxy server and generic TCP/UDP proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. | '''Nginx''' (pronounced "engine-x") is a free, open-source, high-performance HTTP and reverse proxy server, as well as a mail proxy server and generic TCP/UDP proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. |
Latest revision as of 16:18, 29 April 2025
Nginx (pronounced "engine-x") is a free, open-source, high-performance HTTP and reverse proxy server, as well as a mail proxy server and generic TCP/UDP proxy server. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
Originally written by Igor Sysoev, Nginx was first publicly released in 2004. It is widely used to serve static content, act as a reverse proxy for dynamic web applications, and for load balancing.
Contents
Installation on Debian-based Systems (CLI)
This section outlines the process for installing Nginx on Debian or Ubuntu distributions using the command-line interface.
- Update the package index:
sudo apt update
This command refreshes the list of available software packages from the configured repositories.
- Install Nginx:
sudo apt install nginx -y
This command downloads and installs the Nginx server and its required dependencies. The `-y` option automatically confirms prompts during the installation.
- Verify the installation:
nginx -v
Executing this command will display the version of Nginx that has been successfully installed on your system.
Managing the Nginx Service
Nginx operates as a system service. Its lifecycle (starting, stopping, restarting, reloading) is managed using the `systemctl` utility on modern Debian-based systems.
- Starting Nginx:
sudo systemctl start nginx
- Stopping Nginx:
sudo systemctl stop nginx
- Restarting Nginx:
sudo systemctl restart nginx
This command effectively stops and then immediately starts the Nginx service.
- Reloading Nginx:
sudo systemctl reload nginx
Reloading applies changes to the Nginx configuration without interrupting active connections, providing a seamless update.
- Checking Nginx status:
sudo systemctl status nginx
This command provides information on the current state of the Nginx service, including whether it is running and recent log entries.
- Checking Nginx configuration syntax:
sudo nginx -t
It is crucial to test your Nginx configuration files for syntax errors before attempting to reload or restart the service to avoid potential downtime.
Changing the Default Port
By default, Nginx is configured to listen for incoming HTTP connections on TCP port 80. To change this default port, you need to modify the `listen` directive within the relevant Nginx server block configuration file. Configuration files are typically located in `/etc/nginx/` with site-specific settings often in `/etc/nginx/sites-available/` and enabled via symbolic links in `/etc/nginx/sites-enabled/`.
- Locate the configuration file: For the default website configuration, this is commonly `/etc/nginx/sites-available/default`.
- Open the configuration file: Use a text editor with administrative privileges:
sudo nano /etc/nginx/sites-available/default
(Replace `nano` with your preferred editor, such as `vim`.)
- Find the `listen` directive(s): Look for lines specifying the port number, which may resemble:
listen 80 default_server; listen [::]:80 default_server;
- Modify the port number: Change the existing port number (e.g., 80) to your desired new port (e.g., 8080):
listen 8080 default_server; listen [::]:8080 default_server;
- Save and close the file.
- Test the updated configuration:
sudo nginx -t
- Reload Nginx to apply the changes:
sudo systemctl reload nginx
Note: If you configure Nginx to listen on a port number below 1024, it requires root privileges to bind to that port due to operating system restrictions.
Port Conflicts
A port conflict arises when another application or service is already using the specific TCP or UDP port that Nginx is configured to listen on. This prevents Nginx from starting correctly or from binding to the specified port, resulting in an error.
Identifying Port Conflicts
Several command-line tools can be used on Linux to determine which process is currently listening on a particular port:
- Using
ss
(recommended for modern systems):
sudo ss -tuln | grep :your_port_number
Replace your_port_number
with the port number Nginx is trying to use.
- Using
netstat
(available on older systems):
sudo netstat -tulnp | grep :your_port_number
- Using
lsof
(List Open Files):
sudo lsof -i :your_port_number
The output of these commands will typically show the Process ID (PID) and the name of the program or service that is currently utilizing the port.
Resolving Port Conflicts
Once the conflicting process has been identified, there are several strategies to resolve the conflict:
- Reconfigure the conflicting service: The most common solution is to change the port number that the other application is using, if its configuration allows for it.
- Stop the conflicting service: If the conflicting service is non-essential or can be temporarily stopped, you can stop it using `systemctl` (e.g., `sudo systemctl stop service_name`) or by terminating the process directly using the `kill` command (e.g.,
, replacing `PID` with the process ID found earlier). Exercise caution when terminating processes, especially system- critical ones.
- Change the Nginx port: As described in the preceding section, modify the Nginx configuration to listen on an alternative, available port.
After implementing one of these solutions to free up the port, attempt to start or reload the Nginx service again.
Hosting Your Website (index.html)
Nginx serves web content from a designated directory on the server's filesystem, referred to as the "document root" or "webroot". To host a simple static website consisting of an `index.html` file, you need to configure Nginx to use the directory containing your website files as the document root.
- Create the directory for your website files: A conventional location for web content on Debian-based systems is within `/var/www/`. Create a directory for your specific website:
sudo mkdir -p /var/www/mywebsite
Replace mywebsite
with a descriptive name for your website's directory.
- Place your
index.html
file in the directory: Create or copy your main HTML file into the newly created directory:
sudo nano /var/www/mywebsite/index.html
Add or paste the content of your `index.html` file into the editor and save it.
- Configure Nginx to serve files from your directory: Edit the Nginx server block configuration file that is handling requests for your domain or IP address. This might be `/etc/nginx/sites-available/default` or a dedicated configuration file for your site within that directory.
- Set the
root
andindex
directives: Within the `server` block, locate or create a `location /` block and define the `root` directory and the `index` file(s) Nginx should look for when a directory is requested.
server {
listen 80; # Or the port you configured listen [::]:80; # Or the port you configured server_name your_domain_or_ip; # Replace with your domain name or server's IP address
root /var/www/mywebsite; # Specifies the directory containing your website files index index.html index.htm index.nginx-debian.html; # Defines the order of default files to try
location / { try_files $uri $uri/ =404; # Attempts to serve the requested file, then the URI as a directory, falling back to a 404 error if not found. }
}
Replace your_domain_or_ip
with your server's domain name or public IP address.
- Enable the site configuration (if using separate files in
sites-available
): If you created a new configuration file in `/etc/nginx/sites-available/`, activate it by creating a symbolic link in `/etc/nginx/sites-enabled/`:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Replace mywebsite
with the exact name of your configuration file.
- Test the configuration syntax:
sudo nginx -t
- Reload Nginx to apply the changes:
After completing these steps, accessing your server's IP address or domain name in a web browser on the configured port should display the content of your `index.html` file located in the specified webroot directory.
See also
References
External links
Template:Web servers Template:Reverse proxies Template:Load balancers Template:Mail servers Template:Free and open-source software Template:Authority control