NGINX user and group: Explained with examples

Updated: January 19, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful, open-source HTTP server and reverse proxy server. In addition to its HTTP server capabilities, NGINX can also function as a load balancer and an HTTP cache. A critical component of securing and properly managing an NGINX server involves understanding how the server interacts with system user accounts and groups.

This guide will explain the importance of the NGINX user and group and show how to configure them with various examples. This information will not only bolster server security but also ensure the right permissions are in place for NGINX to operate efficiently.

Basic Concepts

Before diving into configuration examples, it is essential to grasp a few fundamental concepts:

  • User: In the context of a UNIX-like operating system, a user is an entity that has permissions to interact with the system. Users can be real people, or system users created for running services and applications.
  • Group: A group is a collection of users. Groups allow for easier management of permissions for multiple users at once.
  • NGINX user and group: The NGINX user and group refer to the system user account and group that the NGINX process runs under. This is important for defining access controls on system resources that NGINX needs to manipulate.

Why Set a Specific NGINX User and Group?

Rather than running NGINX as the root user (which is potentially insecure), it is best practice to run NGINX with a less-privileged system user. This minimizes potential damage in case of a system compromise.

Setting the NGINX User and Group

When you install NGINX, it typically runs as the nginx or www-data user by default, depending on the Linux distribution. Let’s start by setting the NGINX user and group explicitly.

Checking the Current User and Group

ps aux | grep nginx

This command outputs information indicating which user the NGINX worker processes are running as. Look for the first column in the output for the username.

Configuring the User Directive

Edit the NGINX configuration file, typically found at /etc/nginx/nginx.conf. You can use vi, nano, or your preferred text editor.

sudo nano /etc/nginx/nginx.conf

Add the following directive at the top of the file:

user YOUR_USER_NAME GROUP_NAME;

Replace YOUR_USER_NAME and GROUP_NAME with the desired user and group.

Restarting NGINX

After making changes to the configuration, restart NGINX to apply them:

sudo systemctl restart nginx

Use the ps aux | grep nginx command again to check if the user and group have changed successfully.

Advanced User and Group Handling

Here we will cover setting specific permissions for the NGINX user and using access controls for enhanced security.

Creating a Dedicated NGINX User and Group

sudo adduser --system --no-create-home --shell /bin/false --group --disabled-login nginx

This command creates a dedicated user and group nginx with limited login capabilities, ideal for running a secure web server.

Setting Folder Permissions for NGINX

sudo chown -R nginx:nginx /var/www/html

This recursively changes ownership of the /var/www/html directory to the nginx user and group, ensuring that NGINX can read and write to the web root directory.

Working with Supplementary Groups

If you want the NGINX user to be part of additional groups to access specific resources, you can add the user to supplementary groups using:

sudo usermod -a -G additional_group nginx

Replace additional_group with the group name you wish to add the user to.

Directory Permissions with Access Control Lists (ACLs)

Using ACLs, you can define more granular permissions:

sudo setfacl -m u:nginx:rwx /some/special/directory

Here, the NGINX user is granted read, write, and execute permissions on /some/special/directory without changing the ownership.

Security Considerations

While configuring the NGINX user and group, there are certain security best practices to follow:

  • Always use a non-privileged user for running NGINX.
  • Grant only necessary permissions to the NGINX user and group.
  • Regularly check for directory permissions and make sure sensitive directories are not world-readable.
  • Monitor and audit user permissions regularly to prevent unauthorized access.

Conclusion

In this tutorial, we explored the NGINX user and group’s roles in server security and operation. Appropriate configuration and management of user permissions are essential for a robust and secure server. By following the examples given, you should have a functioning and well-secured NGINX environment.