How to set up a default server in nginx?

Published on Aug. 22, 2023, 12:19 p.m.

To set up a default server in NGINX, you can create a server block with a default_server parameter in the configuration file. This block will handle all requests that do not match any other server block. Here is an example configuration file:

server {
    listen 80 default_server;
    server_name _;
    return 404;
}

In this example, we are creating a server block that listens on port 80 and specifies default_server as the parameter. The server_name directive is set to _, which is a wildcard character that matches all request domains. The return 404 directs NGINX to return an HTTP 404 Not Found error for all requests that match this block.

You can customize the behavior of the default server as per your need. It’s important to keep in mind that default_server should only be specified once for a given listener, typically on the server block that has the most lenient matching criteria.

I hope that answers your question. Let me know if you have any other question or if there’s anything else I can help you with.

Tags: