I have Wordpress website which is live. In this website are a lot of posts with uploads over 200GB. I want to create in localhost development environment which I have done, but I have problems with posts images, because I don't want to download all of them and put in my localhost. So I am wondering if there is any Nginx rewrite rule which will rewrite only domain name in my image urls.
I need to rewrite this url:
https://example.local/wp-content/uploads/2020/10/10/very-nice-picture.png
To this one:
https://example.com/wp-content/uploads/2020/10/10/very-nice-picture.png
This is possible with Nginx or there is other better solution?
Here are your options:
Use a redirection:
location /wp-content/uploads/ {
return 301 http://example.com$request_uri; # or use 302 to prevent redirection caching
}
Use transparent proxying:
location /wp-content/uploads/ {
proxy_pass http://example.com;
}
You can also use remote files only in case they are missing on the local development server:
location /wp-content/uploads/ {
try_files $uri #remote;
}
location #remote {
# redirect or proxy the request as shown above
}
Related
I'm using nginx with the deployment of my website on my local server.
The website is a single page create react app running on the server. I have a domain, www.test.com for example, and i want the single page app to be found on www.test.com/first-website. From reading online I'm supposed to use the rewrite directive.
This is my current config:
http {
upstream sensory-showcase {
server 127.0.0.1:5000;
}
server {
listen 80;
location /sensory-solution-for-firefighters {
rewrite ^/sensory-solution-for-firefighters $1 break;
proxy_pass http://sensory-showcase/;
}
}
}
events { }
From this the url resolves without an nginx 500 error however it just shows a blank white page.
And i have to have a trailing /, eg www.test.com/first-website/ . Without the trailing / it errors.
I just note, when I didnt have the rewrite directive in, and left the location at just / the site loaded fine.
Try this, it will fallback to the index.html of your single page app, which will do the routing:
location /first-website {
try_files $uri $uri.html $uri/ /first-website/index.html;
}
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;
}
I am having a problem with trying to serve two different sites using the same server.
I have a landing page at the root of timothylim.is and a separate blog at timothylim.is/writing.
I have the following nginx configuration with two different locations:
location / {
rewrite ^/pserver(/.*)$ $1 break;
proxy_pass https://timothyylim.github.io/landing-page/;
}
location /writing {
rewrite ^/pserver(/.*)$ $1 break;
proxy_pass https://timothyylim.github.io/writing-online/;
}
However, when any of the links are clicked on the '/writing' location the browser navigates to timothylim.is/poetry/your-pillar/ instead of timothylim.is/writing/poetry/your-pillar/.
Is there a way to redirect to a subfolder on the main domain?
You could use the base meta tag instead of trying to change server behaviour.
<base href="timothylim.is/writing/">
I have file sitemap.xml in site public directory. When I use subdomain 'm.', it uses same public directory as general site. I need to return file '/mobile-sitemap.xml' from url '/sitemap.xml', when I'm on subdomain 'm.'.
What nginx modules and rules could help me to do this?
My first step was like this:
if ($host ~* m\.(.*)) {
#if url = /sitemap.xml, give back file /mobile-sitemap.xml as /sitemap.xml
}
Or maybe it's ok to google, if i just send 301 redirect to another sitemap file?
You could create a separate server block for your mobile subdomain, in which case the if statement would not be necessary. See this caution on the use of if.
However, to implement this in a single server block, use a location and a rewrite:
location = /sitemap.xml {
if ($host ~* m\.) {
rewrite ^ /mobile-sitemap.xml last;
}
}
See this and this for more.
I want to do a redirect in order to give a cleaner url for my users.
I want to change:
http://mydomain.com/main/username/profile
To:
http://mydomain.com/username/profile
Would I do this with a rewrite or an alian and how?
First you have to understand what the difference between a redirect and an alias is.
Redirect
A redirect will send a user who requests /main/username/profile to /username/profile. The URL will change in the browser. This is particularly important if the URL is accessible by search engines, because they would otherwise index the same page twice (duplicated content).
If you decide to use a redirect you should be sure, that your URLs stay that way. The reason for this is Cool URIs don't change.
Example for a redirect:
server {
# ...
location / {
# ...
location ~ /main/([a-zA-Z0-9]+)/profile$ {
# SEO effective redirect
return 301 /$1/profile;
}
# ...
}
}
nginx documentation: return
nginx wiki: return
Alias
An alias is used to tell nginx that that a requested file is not mapped by the URL on the filesystem and that it should have a look elsewhere. The following example is from the nginx wiki:
root /var/www;
location /i/ {
alias /spool/w3/images/;
}
A request for /i/empty.gif will not map to /var/www/i/empty.gif. Instead it will be matched to /spool/w3/images/empty.gif.
nginx documentation: alias
nginx wiki: alias