WordPress Pretty Permalinks with Nginx/Ubuntu, WordPress in Subfolder (no Apache)

July 25, 2019 192 views
WordPress Nginx Ubuntu 18.04

Hi,

I’ve been struggling for over a day now to get pretty permalinks to work on WordPress. I’ve tried quite a few solutions from around the web and nothing has been working. This is my conf file:

server {
        listen 80;
        listen [::]:80;

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name website.com www.website.com;

        if (!-e $request_filename) {
            rewrite ^.*$ /index.php last;
        }

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

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO $fastcgi_path_info;
            fastcgi_index  index.php;
            include        fastcgi_params;
        }

        location /blog {
           root /var/www/html/blog;
           index index.php index.html index.htm index.nginx-debian.html;
           try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ /blog/.+\.php$ {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                fastcgi_pass   unix:/run/php/php7.2-fpm.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO $fastcgi_path_info;
                fastcgi_index  index.php;
                include        fastcgi_params;
        }

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

Can anyone tell me what I’m doing incorrectly, please? I did not install this with a one-click-install (otherwise it would be using Apache). Wordpress is installed under /blog. Everything works fine except I am unable to publish posts when pretty permalinks are enabled. Thank you!

2 Answers
bobbyiliev MOD July 26, 2019
Accepted Answer

Hello,

What you’ve got there looks correct, you just need to change one slight thing:

        location /blog {
           root /var/www/html/blog;
           index index.php index.html index.htm index.nginx-debian.html;
           try_files $uri $uri/ /blog/index.php$is_args$args;
        }

Note the blog part before the /index.php, then do a config test and restart.

Let me know how it goes!
Bobby

It works now, thank you very much!

Have another answer? Share your knowledge.