WP migration from apache to nginx results -- 404 -- on https - wordpress

That was kind of a loaded title.
I moved my site over from Apache to Nginx and I'm learning the hard way that Nginx does things its own ways. It does not like htaccess files, ignores them. This causes problems when running Wordpress, because wordpress supposedly loves to use htaccess files and nginx does not. Why? I have no idea.
Anyways,
I managed to figure out how to bring back the site from the abyss of 404, by placing this code in nginx.conf file
location / {
index index.php index.html;
if (!-e $request_filename)
{
rewrite ^/(.+)$ /index.php last;
}
}
But.
while pages load fine on HTTP, HTTPS still shows dreaded 404. Why? I don't know. So, anybody know what to do next?

Well, it turns out I also have to add the same code on nginx.ssl.conf file.
Why? I don't know, but it works.

Related

How do I configure nginx for WordPress REST API in sub-folder?

I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:
https://example.com/site1/?rest_route=/wp/v2/posts
But this endpoint gives a 404:
https://example.com/site1/wp-json/wp/v2/posts
I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:
location /site1/wp-json {
rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}
location /site1/ {
try_files $uri $uri/ /site1/index.php$is_args$args;
}
I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.
Update
Gist of the redacted full config file and the config syntax lints okay:
nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:
/index.php?rest_route=/wp/v2/users/&who=authors...
And these all work fine.
However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:
/wp-json/wp/v2/users/?who=authors...
And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.
But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:
# Resolves WP Gutenberg 404 issue
location /wp-json {
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
I believe that the rewrite directive should be written as shown below:
server {
location /site1/wp-json
{
rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
}
}
I was able to resolve it like this:
location /wordpress/ {
rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
An easy way if your website pages in the subfolder is already working, just add index.php to the url:
https://site1.com/site2/index.php/wp-json/
If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:
location /site2 {
rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}

Only homepage loads following migration (WordPress, nginx)

following a migration from a shared platform to a private cloud server only the home page is loading.
After looking around the most common fix appears to be flushing the perma links and modifying the .htaccess with the following:
AllowOverride All
However the server uses nginx, after trying a .htaccess to nginx converter with the following code added to nginx.conf:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
However I had no luck with this.
Any assistance is appreciated - I hope this is sufficient information but let me know if not.
Thank you.
Rename .htaccess to some other name, lets say .htaccess1, then go to Settings -> Permalinks, Select Post Name and hit save. It will generate new .htaccess and see if it fix the issue.

How do you run multiple instances of expressionengine in subdirectories using nginx?

I'm trying to setup a dev site for a site built in expressionengine. I would have preferred to use a subdomain for my dev site, but I don't control the DNS... So to avoid the hassle I just made a copy in a subdirectory called /~dev/. Like this:
http://example.com/ <-- main site
http://example.com/~dev/ <-- dev site
I copied the database and updated the config files accordingly, and everything works if I pull up either the main site or dev site home pages.... but as soon as I click any link on the dev site, I get a 'Status: 404 Page Not Found' page. I'm thinking that nginx just isn't routing requests correctly. This is my nginx config (in part):
server {
listen 80;
server_name ~^(?<www>.+)\.example\.com$;
root /data/website;
index index.php;
include /etc/nginx/restrictions.conf;
#dev site
location /~dev {
index index.php;
}
I'm not too familiar with nginx (I'm mostly an Apache guy)... so what am I missing here?
Sheesh, I can answer my own question now. Before posting I had spent nearly an hour googling, searching Stack Overflow, and reading nginx docs to no avail... but as soon as I posted my question, I saw this related question pop up in the side bar, which I was able to adapt to my case. facepalm My solution:
location /~dev {
if (!-e $request_filename) {
rewrite ^/~dev/(.*)$ /~dev/index.php/$1 last;
}
}

Redirecting from subdomain to folder on Dotcloud

Is it possible to redirect from a subdomain to a folder using the application nginx.conf on Dotcloud?
I want to redirect http://blog.domain.com to http://www.domain.com/blog
I may be wrong but as I understand the content of the application level nginx.conf is inserted into a server block of the main nginx.conf.
I've tried the following directive in the nginx.conf but it doesn't work:
rewrite http://blog.domain.com http://www.domain.com/blog/ permanent;
Has anyone else managed to do this?
Ok, so I figured this out. Whilst Ifs are considered evil in nginx this was the best I could do:
if ($http_host = "blog.domain.com") {
rewrite ^/(.*)$ http://www.domain.com/blog/$1?$args;
}

vBulletin archive rewrite rules on Nginx

I got a problem for vBulletin archive running on Nginx 0.8.54. The Nginx gives me 404 error when I try visiting the following URLs
http://mydomain.com/forums/archive/index.php/t-19.html
http://mydomain.com/forums/archive/index.php/f-19.html
All other things of vBulletin works fine. Can anybody may share its rewrite rule to fix my problem ?Thanks.
Try something like:
location /archive {
rewrite ^/archive/index.php/t-([0-9]+)\.html /archive/index.php?t-$1.html last;
rewrite ^/archive/index.php/f-([0-9]+)\.html /archive/index.php?f-$1.html last;
}

Resources