How to Fix Nginx WordPress Permalinks 404 Error After Switching from Apache (Easy Fix)

🚫 What Causes the Nginx WordPress Permalinks 404 Error?

When switching from Apache to Nginx, the main issue is that:

πŸ”Ή Apache uses .htaccess for URL rewriting
πŸ”Ή Nginx does NOT support .htaccess files
πŸ”Ή WordPress permalinks rely on rewrite rules

Since Nginx handles URL rewriting differently, WordPress permalinks break unless the correct Nginx configuration is added manually.

Also, note:

mod_security rules and Apache rewrite formats do not apply to Nginx.
You must use native Nginx rules instead.


βš™οΈ How to Fix Nginx WordPress Permalinks 404 Error

You need to manually edit the Nginx configuration files for your domain.

πŸ“ File locations on VestaCP:

/home/admin/conf/web/domain-name.nginx.conf
/home/admin/conf/web/domain-name.nginx.ssl.conf

Both files must be updated to support HTTP and HTTPS.


πŸ“ Step 1: Open the Nginx Configuration File

cd /home/admin/conf/web/
vim domain-name.nginx.conf
# or for SSL:
vim domain-name.nginx.ssl.conf

Press i in vim to enter insert mode.


πŸ› οΈ Step 2: Add the Correct Location Block

βœ” Recommended (Try Files Method – Most Reliable)

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

This rule checks if the request is a file or directory, and if not, it passes it to index.php, which handles WordPress permalinks.


πŸ’‘ Alternative Solution (Rewrite Method)

If the first solution doesn’t work, try this method:

location / {
    rewrite ^/(.*)$ /index.php?$1;
    index  index.html index.htm index.php;
}

This rule forcefully rewrites all URLs to index.php, allowing WordPress to process them.


πŸ’Ύ Step 3: Save and Exit in Vim

Press:

ESC
:wq

πŸ”„ Step 4: Reload Nginx to Apply Changes

nginx -s reload

✨ Your WordPress Permalinks Should Now Work Perfectly!

Once done, visit your WordPress dashboard and go to:

Settings β†’ Permalinks β†’ Save Changes (no need to change anything β€” just resave).

This refreshes your permalink structure.


πŸ“ Bonus: Basic Vim Commands (For Beginners)

CommandAction
iEnter insert mode
ESCExit insert mode
:wSave changes
:qQuit
:wqSave and quit
:q!Quit without saving

πŸ“Œ Post Tags:

#404 #Linode #PHP #PHP-FPM #Ubuntu18.04 #VestaCP #Nginx #Permalinks #WordPress

Posted in Web TutorialsTags:
Write a comment