Nginx, what is it?

A quick explanation of Nginx for beginners. Why use it? How does it work? Where to start?

Find more tutorials explaining the basics of a subject in ten minutes with the tag what is it.

Nginx is an HTTP server, like Apache.

Nginx is designed to be very efficient: low memory usage, high scalability. Its performance makes it the most used web server for the top 1000 websites.

If it's so efficient, why do so many websites continue to use Apache?

This performance comes with a cost: Nginx is less flexible than Apache.

  • The modules are harder to install, involving a compilation step.
  • There is no way for the projects to override the server configuration: there is no such file as Apache's htaccess. This fact is the main reason making Nginx hard to use for smaller websites on shared-hosting.

Nginx natively comes with a lot of advanced features, its default configuration file remains however very easy to understand.

Default Nginx configuration

server { listen 80; listen [::]:80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }

As you can see, the default configuration contains a lot of commented lines, useful for the most common cases. Some explanations of this default configuration:

  • The first listen instruction relates to the port 80 of the "INADDR_ANY" address in IPv4 0.0.0.0. On the second line, [::] it is the way to declare an IPv6 address: "::" is the IPv6 translation of "0.0.0.0". Note that you can merge these 2 instructions by adding the option ipv6only=off at the end of the IPv6 line. It might however cause some issues in case of several servers.
  • server_name is basically the IP address or the domain name used for accessing the server. The same Nginx instance can manage several domains with, for each of them, a specific configuration. The default configuration allows an access via http://localhost.
  • PHP, is not enabled by default. A call to a PHP script via a browser will actually download the script instead of interpreting it.
Wait! I can see that the proxy_pass is used to proxy a request to an Apache server. Why would anybody ever want to use both an Nginx and an Apache server?

One of the main use-case of Nginx is to deploy it as a reverse-proxy in front of an applicative server like Apache. Nginx and its great performances will directly serve the static files like the images, the stylesheets and the HTML. The applicative code, like PHP for instance, will be executed by the applicative server, thus benefiting from the own strengths of Apache. The reverse-proxy protects the applicative servers from the external world and decreases their workload.

Learn more about Nginx on the official site.

Have a nice day :)