0
0
NginxHow-ToBeginner · 4 min read

How to Use Nginx with WordPress: Simple Setup Guide

To use nginx with WordPress, configure nginx to serve PHP files using php-fpm and set URL rewriting rules for WordPress permalinks. This involves creating a server block with root pointing to WordPress files, handling index.php, and enabling pretty URLs with try_files.
📐

Syntax

This is the basic syntax for an nginx server block to serve a WordPress site:

  • server { ... }: Defines the server configuration.
  • listen 80;: Listens on port 80 for HTTP requests.
  • server_name: Your domain name.
  • root: Path to WordPress files.
  • index: Default index files.
  • location / { try_files $uri $uri/ /index.php?$args; }: Handles pretty permalinks.
  • location ~ \.php$ { ... }: Passes PHP files to php-fpm.
nginx
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/wordpress;
    index index.php index.html index.htm;

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

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

    location ~ /\.ht {
        deny all;
    }
}
💻

Example

This example shows a complete nginx server block configured to serve a WordPress site located at /var/www/wordpress. It listens on port 80, handles PHP files with php-fpm, and supports WordPress permalinks.

nginx
server {
    listen 80;
    server_name myblog.com www.myblog.com;
    root /var/www/wordpress;
    index index.php index.html index.htm;

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

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

    location ~ /\.ht {
        deny all;
    }
}
Output
nginx starts successfully and serves WordPress pages with working permalinks and PHP processing.
⚠️

Common Pitfalls

Common mistakes when using nginx with WordPress include:

  • Not setting try_files correctly, causing permalinks to break.
  • Incorrect fastcgi_pass socket or address, so PHP files don't execute.
  • Missing index.php in the index directive, causing homepage load issues.
  • Not denying access to hidden files like .htaccess, which can expose sensitive data.

Example of a wrong and right try_files usage:

nginx
# Wrong - permalinks break because fallback is missing
location / {
    try_files $uri $uri/ =404;
}

# Right - fallback to index.php for WordPress permalinks
location / {
    try_files $uri $uri/ /index.php?$args;
}
📊

Quick Reference

Remember these key points when configuring nginx for WordPress:

  • Use try_files $uri $uri/ /index.php?$args; for permalinks.
  • Pass PHP files to php-fpm with correct socket or IP:port.
  • Set root to your WordPress installation directory.
  • Deny access to hidden files like .htaccess.
  • Reload nginx after changes with sudo systemctl reload nginx.

Key Takeaways

Configure try_files to enable WordPress permalinks correctly.
Use php-fpm to process PHP files in nginx.
Set the root directive to your WordPress folder.
Deny access to hidden files like .htaccess for security.
Reload nginx after configuration changes to apply them.