# Nginx: Stat() Failed (13: Permission Denied)

The `stat() failed (13: Permission Denied)` error in Nginx typically indicates that Nginx does not have the necessary permissions to access a file or directory. This can occur due to improper file or directory permissions, incorrect ownership settings, or security context issues. Here’s a detailed guide to troubleshoot and resolve this error:

### **1. Check File and Directory Permissions**

Ensure that the files and directories Nginx needs to access have the correct permissions.

**Verify Permissions:**

- **Files:** Typically, files should have permissions `644` (read and write for owner, read-only for group and others).
- **Directories:** Typically, directories should have permissions `755` (read, write, and execute for owner, read and execute for group and others).

**Commands:**

```bash
# Check permissions
ls -l /path/to/your/file_or_directory
```

**Set Permissions (if needed):**

```bash
# Set directory permissions
sudo chmod 755 /path/to/your/directory

# Set file permissions
sudo chmod 644 /path/to/your/file
```

### **2. Check File and Directory Ownership**

Ensure that the files and directories are owned by the user under which Nginx is running (typically `www-data` on Debian/Ubuntu or `nginx` on CentOS/RHEL).

**Verify Ownership:**

```bash
# Check ownership
ls -l /path/to/your/file_or_directory
```

**Change Ownership (if needed):**

```bash
# Change ownership to the Nginx user (e.g., www-data or nginx)
sudo chown www-data:www-data /path/to/your/file_or_directory
```

### **3. Check Nginx Configuration**

Ensure that the `root` and `location` directives in your Nginx configuration correctly point to directories that Nginx can access.

**Example Nginx Configuration:**

```
server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    location / {
        try_files $uri $uri/ =404;
    }
}

```

**Verify the `root` directive:**

Make sure that `/var/www/html` is the correct path and accessible.

### **4. Check SELinux Context (for CentOS/RHEL)**

If SELinux is enabled on your system, it might restrict access to files even if file permissions and ownership are correct. Check and adjust SELinux policies if necessary.

**Check SELinux Status:**

```bash
sestatus
```

**View SELinux Contexts:**

```bash
ls -Z /path/to/your/file_or_directory
```

**Modify SELinux Context (if needed):**

```bash
# Set correct SELinux context for web files
sudo chcon -R -t httpd_sys_content_t /path/to/your/directory
```

### **5. Review Nginx Error Logs**

Nginx’s error logs provide detailed information about permission issues. Check these logs to gain more insight into the problem.

**Error Log Location:**

```bash
# Common log locations
cat /var/log/nginx/error.log
```

### **6. Restart Nginx**

After making changes to permissions, ownership, or configurations, restart Nginx to apply the changes.

**Restart Nginx:**

```bash
sudo systemctl restart nginx
```

### **7. Verify Directory Traversal**

Ensure that Nginx can traverse all directories leading up to the target file. If Nginx cannot access any parent directory, it will result in a permission denied error.

**Verify Parent Directories:**

```bash
# Check parent directory permissions
ls -ld /path/to/your
```

### **Summary**

To resolve the `stat() failed (13: Permission Denied)` error in Nginx:

1. **Check File and Directory Permissions:** Ensure correct permissions for files and directories.
2. **Check File and Directory Ownership:** Verify that Nginx has ownership or proper access to files.
3. **Check Nginx Configuration:** Confirm that Nginx configuration paths are correct and accessible.
4. **Check SELinux Context:** For CentOS/RHEL, ensure SELinux contexts allow access.
5. **Review Nginx Error Logs:** Inspect logs for detailed error messages.
6. **Restart Nginx:** Apply changes by restarting Nginx.
7. **Verify Directory Traversal:** Ensure Nginx can access all parent directories leading to the file.

By following these steps, you should be able to resolve the permission denied error and ensure Nginx can access the necessary files and directories.