Introduction
Nginx is a popular open-source web server software that is commonly used to serve static content, dynamic content and reverse proxy content. Nginx is known for its high performance, scalability, and flexibility. When you install Nginx, it installs a default server block. In this article, we will discuss the default server block and how you can use it to serve your content.
Configuring Nginx's Default Server
Nginx's default server block is defined in the file /etc/nginx/sites-available/default. This file is used by Nginx to serve content when no other server block is defined. The default server block is used to serve default 'welcome to Nginx' web page.
By default, the Nginx default server is configured to listen on port 80, which is the default HTTP port. You can modify the port by editing the listen directive in the default server block.
For example:
server {
listen 8080 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
In the above example, we have changed the listening port to 8080 in the listen directive. We have also defined the root directory and index files that Nginx should use to serve the content. Additionally, we have defined the server name as an underscore character, which tells Nginx to use this server block as the default server block.
A default server block can be used to serve content that does not require any additional configuration. For example, to serve static HTML files, you can simply place the files in the root directory defined in the server block.
You can also use a default server block to serve dynamic content. For example, you can use a default server block to serve content from a PHP application. To do so, you will need to install PHP and enable the PHP-FPM service.
Enabling PHP on the Default Server
To enable PHP on the default server block, you must install the PHP-FPM service and modify the server block to use the PHP-FPM service.
First, install the PHP-FPM service using the following command:
$ sudo apt-get install php-fpm
Next, modify the Nginx default server block to use the PHP-FPM service:
server {
listen 8080 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
In the above configuration, the location block defines how Nginx should handle requests to PHP files. The include statement includes the fastcgi configuration file that defines how Nginx should communicate with the PHP-FPM service. The fastcgi_pass directive specifies the location of the PHP-FPM socket.
With these modifications, Nginx is now configured to serve PHP content on the default server.
Conclusion
The default server block is used to serve content when no other server block is defined. It is useful for serving default static content and can also be used to serve dynamic content with PHP-FPM. By understanding how the default server block works, you can better configure your Nginx server to suit your needs.
In the previous topic, we discussed the default server block in Nginx and how it can be used to serve static and dynamic content. In this article, we will dive deeper into the configuration of the default server block with additional code examples.
Configuring a Custom 404 Page
When Nginx encounters a URL that does not exist, it will return a standard 404 error page. However, you can customize the 404 page by modifying the default server block.
To do so, add the following code to the default server block:
error_page 404 /404.html;
location = /404.html {
internal;
}
In this code, we define an error page for the 404 error code and specify the location of the custom 404 page. The second line defines a location block for the 404 page that is set as internal. This ensures that the 404 page is only accessible to Nginx and not directly from the browser.
Using SSL/TLS with the Default Server
If you want to use SSL/TLS (Secure Sockets Layer/Transport Layer Security) with the default server block, you need to install an SSL/TLS certificate and configure Nginx to use it.
To install an SSL/TLS certificate, you can use a service like Let's Encrypt or purchase one from a trusted certificate authority (CA).
After installing the certificate, modify the default server block to use it:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/cert.key;
location / {
root /var/www/html;
}
}
In this code, we define the default server block to listen on the HTTPS port 443 and use an SSL/TLS certificate. We also define the server name, which should match the domain name of the certificate. Finally, we set the root directory for the default server block.
Redirecting HTTP to HTTPS
To redirect HTTP traffic to HTTPS, add the following code to your default server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
return 301 https://$server_name$request_uri;
}
In this code, we define the default server block to listen on the HTTP port 80 and specify the server name. We then use the return statement to redirect all HTTP traffic to HTTPS.
Conclusion
In conclusion, the default server block in Nginx is the baseline configuration used for serving content. By understanding how to modify the default server block, you can customize it to meet your specific needs. In this article, we covered several common modifications, including customizing the 404 page, using SSL/TLS, and redirecting HTTP traffic to HTTPS. By applying these modifications, you can make your Nginx server more secure and tailored to your specific needs.
Popular questions
-
What is the default server block in Nginx?
Answer: Nginx's default server block is the baseline configuration used for serving content. It is used to serve content when no other server block is defined. -
How can you modify the listening port for the default server block?
Answer: You can modify the listening port by editing the listen directive in the default server block. For example: "listen 8080 default_server;" -
How can you enable PHP on the default server block?
Answer: To enable PHP on the default server block, you need to install the PHP-FPM service and modify the server block to use the PHP-FPM service. You also need to define a location block to handle requests to PHP files. -
How can you customize the 404 error page for the default server block?
Answer: You can customize the 404 page by adding an error_page directive to the default server block and specifying the location of the custom 404 page. You also need to define a location block for the 404 page that is set as internal. -
How can you redirect HTTP traffic to HTTPS for the default server block?
Answer: To redirect HTTP traffic to HTTPS, you can add a server block that listens on port 80 and uses a return statement to redirect all traffic to HTTPS. For example: "return 301 https://$server_name$request_uri;".
Tag
"nginx-samples"