How to configure Nginx for WordPress permalinks
Here's how to configure Nginx to support WordPress permalinks:
- 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; } } - Add the following rewrite rules to the
serverblock, just below thelocation /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; } } - Save the changes to the Nginx configuration file:
sudo nano /etc/nginx/sites-available/example.com - Test the Nginx configuration:
sudo nginx -t - If the test is successful, reload Nginx:
sudo systemctl reload nginx
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.
