Nginx Configuration: Complete Step-by-Step Guide

To set up Nginx, you can follow the steps below:

  1. Install Nginx.
  2. On Ubuntu, you can install Nginx using the following commands: sudo apt update sudo apt install nginx
  3. On CentOS, you can install Nginx using the following commands: sudo yum update
    sudo yum install nginx
  4. Basic settings:
  5. The configuration file for Nginx can be found at /etc/nginx/nginx.conf. Use any text editor to open the file.
  6. The http block can be found in the file, which is used for global configuration. Within the http block, you can set some basic configuration options such as listening ports, log paths, etc.
  7. You can add a server block within an http block to configure virtual hosts. Each server block corresponds to a website. Within the server block, you can set domain names, root directories, access logs, etc.
  8. Static website configuration:
  9. In the server block, the root directive can be used to set the root directory of the website. For example:
    server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    }
  10. You can use the location block to configure specific URL path rules. For example, to set up a rule for URLs that include /images/, you can use:
    location /images/ {
    alias /var/www/images/;
    }
  11. Reverse proxy configuration:
  12. In the server block, you can use the proxy_pass directive to configure reverse proxy. For example:
    server {
    listen 80;
    server_name example.com;
    location / {
    proxy_pass http://backend_server;
    }
    }
  13. The above configuration will forward all requests to the http://backend_server.
  14. SSL/TLS settings:
  15. To enable SSL/TLS, you need to create a certificate file. You can use the free Let’s Encrypt service to generate the certificate.
  16. In the server block, you can use the ssl_certificate and ssl_certificate_key directives to specify the paths to the certificate files. For example: server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    }
  17. It is important to note that HTTPS defaults to using port 443.
  18. Restart Nginx.
  19. After setting up, you can use the following command to restart Nginx and apply the configuration: sudo systemctl restart nginx

This is a basic Nginx configuration tutorial. You can explore more advanced configuration options and features of Nginx as needed.

bannerAds