Question

How to hide .user.ini in my NGINX server to use Wordfence in Wordpress?

Hello, i just got a fresh install of wordpress and i installed Wordfence and it recommends me to hide user.ini because is public and anybody can have access to it.

Link to the screenshot of wordfence


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @hcmendez:

You can make Nginx drop all requests to the file .user.ini by returning a HTTP 400 status code (or any other error code of your choice). Add the following to the Nginx server block for your WordPress site:

    location = /.user.ini { return 400; }

This location block should be at the same level as other existing location blocks.

Then your server block should look like this:

server {
...
    server_name yoursite.com;
...
    location = /.user.ini { return 400; }
...
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
....
}
KFSys
Site Moderator
Site Moderator badge
March 28, 2024

Hiding or restricting access to the .user.ini file in Nginx (when using WordPress and Wordfence) is a good security practice. The .user.ini file, like .htaccess in Apache, can be used to override PHP settings and is read by PHP-FPM.

Here’s how you can restrict access to the .user.ini file in your Nginx configuration:

  1. Edit Nginx Configuration File

    First, locate and open your Nginx configuration file for your WordPress site. This file is typically found in /etc/nginx/sites-available/. If you’re unsure about which file to edit, it’s usually the one with your domain name.

  2. Add a Location Block to Deny Access

    Inside the server block, add a new location directive to deny access to the .user.ini file. Add this location block:

location ~ /\.user\.ini {
    deny all;
}

Here’s what it does:

-   `location ~ /\.user\.ini`: This line tells Nginx to apply the configuration to any requests that match the `.user.ini` file. The `~` indicates a regular expression match.
-   `deny all;`: This line instructs Nginx to deny all requests to this location.
  1. Reload Nginx

    After you’ve made these changes, you need to reload Nginx to apply the new configuration. You can do this with the following command:

sudo systemctl reload nginx
  1. Test the Configuration

    Make sure that your website is still functioning correctly after making these changes. Try accessing http://yourdomain.com/.user.ini directly in your browser to see if access is denied.

Additional Considerations

  • File Permissions: Ensure that the .user.ini file has the correct file permissions. It should be readable by the user that PHP or PHP-FPM is running as.

  • Regular Backups: Always have regular backups of your website and its database, especially before making configuration changes.

  • Security Plugins: While Wordfence is a good start, consider using additional security measures and plugins to harden your WordPress installation further.

  • Nginx Best Practices: Familiarize yourself with Nginx best practices, especially in regards to securing PHP applications like WordPress.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel