How to configure Nginx for WordPress permalinks

Here’s how to configure Nginx to support WordPress permalinks:

  1. Create an Nginx server block for your WordPress site:
    server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;

    location / {
    try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    }

  2. Add the following rewrite rules to the server block, just below the location / block:
    location / {
    try_files $uri $uri/ /index.php?$args;

    if (!-e $request_filename) {
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^(/[^/]+)?(/wp-.*) $2 last;
    rewrite ^(/[^/]+)?(/.*\.php)$ $2 last;
    }
    }

  3. Save the changes to the Nginx configuration file:
    sudo nano /etc/nginx/sites-available/example.com
  4. Test the Nginx configuration:
    sudo nginx -t
  5. If the test is successful, reload Nginx:
    sudo systemctl reload nginx

After these steps, Nginx should be configured to support WordPress permalinks.

Note: Make sure to replace example.com with your own domain name, and to replace /var/www/example.com with the path to your WordPress site. Also, replace php7.4-fpm.sock with the appropriate path to your PHP-FPM socket file.

Leave a Comment