Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to set up Nginx for WordPress permalinks and statistics

This guide shows you how to edit Nginx so WordPress pretty links work, and so a statistics tool such as Webalizer or AWStats can be reached. It needs root access.

WordPress2 min read13 steps

This guide needs root access. Root is the all-powerful admin account on a VPS or dedicated server. If you are on shared hosting, you cannot do this. Ask Hostvento support instead.

Nginx (say "engine-x") is a web server program. It receives requests from visitors and sends back your pages. A permalink is the permanent address of a post. A pretty permalink looks like example.com/my-first-post/. An ugly one looks like example.com/?p=123.

Apache reads a file called .htaccess for pretty links. Nginx does not read that file. So you must tell Nginx yourself. If you do not, posts show "404 Not Found".

Warning: a mistake in an Nginx file can take every site on the server offline. Copy the file before you edit it.

  1. Connect to your server with SSH. SSH is a safe way to type commands on a server.
  2. Find your site's config file. It is often in /etc/nginx/conf.d/ or /etc/nginx/sites-available/.
  3. Make a copy:
    cp /etc/nginx/conf.d/example.conf /etc/nginx/conf.d/example.conf.bak
    This saves the original under a new name.
  4. Open the file in an editor:
    nano /etc/nginx/conf.d/example.conf
  5. Inside the server block, find or add a location / block, and make it say:
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    This tells Nginx: try the real file first, then a folder, then hand the request to WordPress.
  6. Save the file. In nano, press Ctrl + O, Enter, then Ctrl + X.
  7. Test the config:
    nginx -t
    This checks your file for mistakes without applying it. It must say "syntax is ok".
  8. Reload Nginx:
    systemctl reload nginx
    This applies the change without cutting off visitors.
  9. In WordPress, go to Settings, then Permalinks. Choose Post name and click Save Changes.

Steps: reach your statistics

Statistics tools such as AWStats make reports from your server logs. If your report folder gives a 404 under WordPress, add a separate location so WordPress does not take over that path. Use your real folder path.

  1. Open the same config file.
  2. Above the main location / block, add:
    location /stats/ {
        alias /var/www/stats/;
        index index.html;
    }
    This serves the folder /var/www/stats/ at the address /stats/. Use the folder where your tool saves its reports.
  3. Run nginx -t again. Then run systemctl reload nginx.
  4. Visit yourdomain.com/stats/.
Tip: Protect the stats folder with a password. Statistics can show private information. Nginx supports a basic login through the auth_basic setting.

Quick recap

  • Nginx ignores .htaccess, so you add try_files yourself.
  • Back up the config, edit, run nginx -t, then reload.
  • Save permalinks again in WordPress.
  • Give your stats folder its own location block.