Question

[Nginx + Wordpress] Any endpoints not ending in .php return 404

For example, trying to call /wp-admin results in 404, whereas /wp-admin/index.php works flawlessly. I reckon this won’t play nice in the future.

I imagine this has something to do with a rewrite issue, but I don’t know nginx well enough to formulate a custom block to fix this. When I visit /home, it accurately shows me the homepage.

Here’s the nginx config for this vhost:

server {
	root /var/www/[ip];

	index index.php index.html index.htm index.nginx-debian.html;
	server_name [ip] www.[ip];

	client_max_body_size 50M;

	location / {
		proxy_set_header X_Forwarded_For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded_Proto $scheme;
		proxy_pass http://localhost:3000;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection 'upgrade';
		proxy_set_header Host $host;
		proxy_cache_bypass $http_upgrade;
	}

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

	location ~ \.php$ {
		#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
		include fastcgi_params;
		fastcgi_intercept_errors on;

		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

		#The following parameter can be also included in fastcgi_params file
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}

	location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
		expires max;
		log_not_found off;
	}

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

Thanks in advance for any help with this :)


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I remedied this behaviour by moving the contents of my wordpress folder down into the wordpress/home directory.

My project structure used to be:

.
├── ecosystem.config.js/
├── index.js/
├── lib/
│   [...]
├── node_modules/
│   [...]
├── package-lock.json/
├── package.json/
└── wordpress/
    [wordpress content]

It is now:

.
├── ecosystem.config.js/
├── index.js/
├── lib/
│   [...]
├── node_modules/
│   [...]
├── package-lock.json/
├── package.json/
└── wordpress/
    └── home/
        [wordpress content]

Thanks to all who pointed me in the right direction / acted as my rubberduck.

KFSys
Site Moderator
Site Moderator badge
January 11, 2021

Hi @Medallyon,

You need to add which files your Nginx should try to open first in your config. In the #wordpress block add the following:

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

Save the file, restart nginx and everything should work properly.

Regards, KFSys

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel