Question

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

Posted January 9, 2021 258 views
NginxWordPressUbuntu 18.04

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 :)

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.

×
Submit an Answer
2 answers

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.

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

  • This doesn’t seem to change anything. I still get 404s when calling any endpoint not ending in .php.

    I should note that a Node.js server is hosted on the domain root, which is why there’s a proxy_pass in the location / block. I followed in the footsteps of this question, where this particular issue didn’t seem to crop up.