Nginx (pronounced "engine-x") is a high-performance web server and reverse proxy server. It is known for its stability, low resource usage, and ability to handle a large number of simultaneous connections.

Here's a basic tutorial on how to install, configure and use Nginx:

  1. Install Nginx on your system. On CentOS/RHEL 7 you can use the command:
sudo yum install nginx
  1. Start the Nginx service:
sudo systemctl start nginx
  1. Verify that Nginx is running by visiting http://localhost/ in your web browser. You should see the default Nginx welcome page.

  2. To configure Nginx, you will need to edit the main configuration file located at /etc/nginx/nginx.conf. This file contains the global settings for Nginx, such as the user and group the server runs as, the number of worker processes, and the location of the error and access logs.

  3. To create a new server block for your website, you can create a new configuration file in the /etc/nginx/conf.d/ directory. You can name this file as your domain name or any name you wish to give.

  4. For example, if your domain name is example.com, you can create a new file called example.com.conf in the /etc/nginx/conf.d/ directory, and add the following contents to the file:

server { listen 80; server_name example.com; root /var/www/example.com; index index.html; }
  1. This configuration will tell Nginx to listen on port 80 for requests to example.com, and to serve files from the /var/www/example.com directory.

  2. Once you have made the necessary changes, you can test the configuration by running:

sudo nginx -t
  1. If the configuration test is successful, you can then reload Nginx to apply the changes:
sudo systemctl reload nginx
  1. To verify that your website is working correctly, you can visit http://example.com/ in your web browser.

Please note that this is just a basic tutorial and there are many other configuration options that can be used to customize Nginx. Also make sure that you have the necessary permissions to make changes to the Nginx configuration files.

 

Here are a few examples of how you can customize Nginx:

  1. Virtual Hosts: You can create a new server block for a domain example2.com and configure it to serve files from a different directory than example.com
server { listen 80; server_name example2.com; root /var/www/example2; index index.html; }
  1. Reverse Proxy: You can configure Nginx to forward all incoming requests for a specific location to a backend server, for example, all requests for /api/ to be forwarded to a backend server running on IP address 10.0.0.2
location /api/ { proxy_pass http://10.0.0.2/; }
  1. Load Balancing: You can configure Nginx to distribute incoming requests to multiple backend servers based on different algorithms, for example, round robin algorithm:
upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } }
  1. Caching: You can configure Nginx to cache certain types of files in memory, for example, cache all files with the .jpg extension for one hour:
location ~* \.(jpg)$ { expires 1h; }
  1. SSL/TLS: You can configure Nginx to handle SSL/TLS encryption for your website, for example:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/ssl/example.crt; ssl_certificate_key /etc/ssl/example.key; root /var/www/example; index index.html; }
  1. Security: You can configure Nginx to limit the number of request a client can make to your server, for example:
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { listen 80; server_name example.com; location / { limit_req zone=one; proxy_pass http://backend; } }
  1. Logging: You can configure Nginx to log the request details, for example:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main;

Please note that these are just examples and the specific steps to customize Nginx may vary depending on your particular use case and setup. It's always a good idea to consult the Nginx documentation for more detailed information on the available options and directives.

 

أحدث أقدم