Wordpress blog on Nginx - CSS and JS files not found - wordpress

I built a custom PHP/MySQL website (www.mywebsite.com) to which I attached a wordpress blog (www.mywebsite.com/blog). I've been trying so many different nginx config things and read so many blog articles that I'm lost now :-)
The main website part is working fine. All my url rewrites work fine.
But on the blog part, I don't know what to do anymore for Wordpress to work properly.
Here's my last conf for the blog part. Permalinks work but the css and js files cannot be found.
location /blog {
root /var/www/mywebsite.com/blog/;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}

You can add a location for them, and a expires as well.
location ~* \.(js|css|xml|txt)$ {
expires 14d;
root /to/the/folder/they/live/in;
access_log off;
}

Discovered an awesome blog that I definitely recommend => http://blog.martinfjordvald.com/
After a read through, I updated the root in my location block like this:
location /blog {
root /var/www/mywebsite.com;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
}
Works fine now !

Related

How to deploy Next.js static export with Nginx? (deep links not working)

I made a next.js export into the out folder.
Folder structure is:
out
index.html
terms.html
privacy.html
I set up nginx to serve files from this folder:
server {
root /var/www/myproject/out;
index index.html index.htm index.nginx-debian.html;
server_name myproject.com;
location / {
try_files $uri $uri/ /index.html;
}
}
The main page (index) opens fine. Navigation from within the app to urls like myproject.com/privacy works fine. The problem is if I try to open these links directly, it will serve the main page (index) instead of the actual pages, since those urls don't exist in the folder. The only way to open the privacy page directly is adding the html extension to the url: myproject.com/privacy.html.
How to configure nginx to serve the actual page myproject.com/privacy.html when someone enters the myproject.com/privacy url?
Issue is in try_files.
As current configuration includes:
/ (which default route to index.html at root path)
index.html
/index.html
test/*.html
To access pages route path name without extension i.e., /privacy that format should be included in try_files insdie location /
Try this:
try_files $uri $uri.html /$uri /index.html
I needed a 2nd location block because of the way NextJS does ids in the url. NextJS will have files like [id].html or whatever.
location / {
try_files $uri $uri.html /$uri /index.html;
}
location ~* /(.*)(\d+)$ {
try_files $1/[id].html /$1/[id].html /index.html;
}
So I needed the 2nd block to catch urls of the form /whatever/etc/5 and redirect nginx to /whatever/etc/[id].html

Nginx location trouble

So I am trying to create location direction where only / would be moved to subfolder /fr but I am failing and can't find the reason. This is nginx docker container exposed on k8s. It's bi lingual app, /fr is default language. Locations /fr and /en exist, rest don't so idea is that all non existing locations are moved to /fr , root doesn't contain index.html so it should be moved to /fr too and /en stays there. Here is the code:
location / {
try_files $uri $uri/ fr/index.html;
}
location = / {
return 301 $scheme://$server_name/fr/index.html;
}
location /de {
try_files $uri $uri/ fr/index.html;
}
location /en {
try_files $uri $uri/ en/index.html;
Everything works except = / . Thanks for the help!
Well everything is working, it's stupid Chrome that caches old responses even though I was testing from incognito mode...

Regular Expression to fix broken URL's in Nginx

On my previous server that ran apache I had some htaccess rules that helped forward a certain pattern of URL's which were giving 404's to the fixed pattern.
Long time ago my URLS for my site were http://domainname/articlename and then I changed it to be http://domainname/category/articlename
Now the problem is the older links that google has are returning 404's and I want to intercept any URL that doesn't have a category and insert a fake category and then my wordpress installation can resolve the URL.
So I'm looking for a nginx solution to this problem which I presume will be in the config file somewhere that will take this URL
http://www.criticalhit.net/prey/ (which gives a 404)
and change it to
http://www.criticalhit.net/fixed/prey/
which then resolves properly.
Use a named location to perform the rewrite, although this simple rewrite can be accomplished efficiently using a return 301.
Place a regular expression location (after the PHP location block) to bypass excluded URLs. This does not need to include the static files which are served by the try_files statement.
For example:
root /path/to/root;
index index.php;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 301 /category$request_uri;
}
location ~ \.php$ {
try_files $uri =404;
...
}
location ~ ^/(category|tags|feeds) {
try_files $uri $uri/ /index.php;
}
See this document for more.

nginx match specific file from different folder on certain routes

I have an angularjs app, it has a blog as well. This url shows all blog posts under
http://example.com/blog/
And specific blog posts under
http://example.com/blog/example-blog-post-title
Now i'm precompiling HTML of blog posts for SEO purposes and i want to serve them completely separately from my main app like this:
...
root "/home/ubuntu/client/public";
location / { ## Handle default requests ##
try_files $uri $uri/ /index.html;
}
location /blog { ## serve precompiled blog HTML
alias /home/ubuntu/bloghtml;
try_files $uri.html $uri/ index.html;
}
...
And this works, by going to http://example.com/blog/example-blog-post-title nginx successfully serves file /home/ubuntu/bloghtml/example-blog-post-title.html
However the issue is that in this case nginx doesn't correctly route blog post list under http://example.com/blog/ to my main angular app, i get error 403 on that URL.
I tried changing location /blog to location /blog/ in conf file, this makes the http://example.com/blog/ work, howewever i get 404 errors on http://example.com/blog/example-blog-post-title
How can i make this work for both cases?
If you change the location from /blog to /blog/ you need to remember to change alias from /home/ubuntu/bloghtml to /home/ubuntu/bloghtml/. The alias and location need to have the same ending, otherwise the calculated pathnames are wrong.
I try to avoid using alias and try_files in the same block because of some known issues. You might consider making the last directory in the path blog so that you can use root instead.
I presume that your angular app is /index.html, in which case your try_files statement is incorrect. The $url/ will cause it to try /blog/index.html (assuming you have an index directive in force) and index.html is missing a leading /.
I would suggest you try:
location /blog {
alias /home/ubuntu/bloghtml;
try_files $uri.html /index.html;
}
but consider designing out the alias directive too.

Nginx - serving assets from user directories

I have 2 issues.
I don't want to require .html file extension for html files
/index => /index.html
I want to serve from user directories
/~username serves from /home/username/www/
I previously used try_files to achieve (1), and I am user the nginx UserDir suggestion:
location ~ /^/~(.+?)(/.*)?$ {
alias /home/$1/www$2;
index index.html index.htm;
autoindex on;
}
The above works for user directories but still requires the .html ext to be used.
I know there is a known bug preventing alias and try_files from working well together.
Thoughts? Sorry if this has been answered before couldn't find a working solution.
You can always replace alias with root
location ~ /^/~([^/]+)(/.*)?$ {
root /home/$1/www;
autoindex on;
try_files $2 $2/ $2.html;
}
PS: move the index to the server scope instead of location
It's a bit old, but as I've been hit by the same problem recently, here is my answer. Thanks to http://marc.info/?l=nginx&m=124533515814122&w=2 I've found that a better answer would be:
location ~ /^/~(.+?)(/.*)?$ {
alias /home/$1/www$2;
index index.html index.htm;
autoindex on;
try_files "" .html / =404;
}
You could add the .html extention to the location regex and the alias:
location ~ /^/~(.+?)(/.*)?.html$ {
alias /home/$1/www$2.html
Note that in this configuration, ONLY html files can be served. You can add another location to support other file extentions.

Resources