nginx same location but with different directives - wordpress

Trying to see if this is possible.
We have an app and a wordpress install.
Is it possible to use 2 locations for the same folder but under different circumstances. Example..
http://domain.com/subfolder/ - This shows the APP
http://domain.com/subfolder/anything - This shows WP permalink
Right now, we have it so
http://domain.com/subfolder (without the /) shows the app
http://domain.com/subfolder/ (witht the /) shows WP.
This does work, but would it be possible to have it so, it will only show WP IF the URL contains text after subfolder/*
Current Nginx conf:
location ^~ /knowledge {
root /opt/domain.com/public/;
try_files $uri #backend;
}
location /knowledge/ {
index index.php index.html index.htm;
root /opt;
include /etc/nginx/php-wpsc.conf;
try_files $uri $uri/ /knowledge/index.php?q=$uri&$args;
}

Obviously it makes sense to keep the location /knowledge/ block for WordPress, as that matches the majority of cases, with just one case that needs to be overridden.
A specific URI can be taken away from that location block by using a location block with a higher precedence. See this document for details.
One possibility would be an exact match location block:
location = /knowledge/ {
rewrite ^ /knowledge last;
}
Or possibly, change your existing location ^~ /knowledge block from a prefix location to a regular expression location, making the trailing / optional.
location ~ ^/knowledge/? { ... }
Note that this changes the order of evaluation of this location block, so there may be side-effects that need to be considered.

Related

Nginx remove leading slash and trailing file type suffix and pass to php

I have received to migrate an existing website written in old php hosted on Apache, and I will deploy to an Nginx.
I wish to have URL like this: http://example.com/about.html
To be executed like this http://example.com/content.php?page=about
So I need to remove leading slash and remove html. The config below works if I hardcode a specific page:
location / {
try_files $uri $uri/ /content.php?page=about;
}
But of course it always serve about regardless if I access our-company.html, or our-services.html. I am not sure what I need to replace the "about" string in the config.
You should use a rewrite directive to perform the actual translation. You can invoke it from a named location specified as the last parameter on the try_files statement.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)\.html$ /content.php?page=$1 last;
}
See this document for more.

Using try_files alongside rewrite in nginx location block

I already make extensive use of rewrite in nginx to perform this sort of thing:
/photos/123344 -> /photos/photos.php?id=123344
/photos/london-2016 -> /photos/photo-shoot.php?name=london-2016
Currently I have no rule for other (non-dynamic) pages. E.g
/photos/shoot-register.php -> /photos/shoot-register.php
Which I'd like to become
/photos/shoot-register.php -> /photos/shoot-register
But without specifying an individual rewrite rule for each .php file.
It seems that try_files is the correct directive here:
location ~ ^/photos {
try_files $uri $uri.php?$args;
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}
But this doesn't work, unless I delete the two rewrite lines.
I assume that means that execution doesn't stop after try files? It finds "shoot-register.php" but then carries on executing and ends up with /photos/photo-shoot.php?name=shoot-register.php?
How can I make it stop after try_files succeeds in finding a match?
Thanks
what if you move the rewrites to separate named location, and then change your try_file directive to try the file, then the php file and then directing to the new location?
location ~ ^/photos {
try_files $uri $uri.php?$args #rewrites;
}
location #rewrites {
rewrite ^/photos/([0-9]+)(/?)$ /photos/photo.php?id=$1;
rewrite ^/photos/([^/\.]+)(/?)$ /photos/photo-shoot.php?name=$1;
}

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 try_files ignore path

I have a location setup for images like:
location ~* ^/images/.+\.(jpg|jpeg|gif|png)$ {
try_files /disk/$uri /images?uri=$uri;
}
The $uri includes the images name. When I access /images/one/two/three/item.jpg, I want to exclude the images from the try_files. So accessing this should actually be trying /disk/one/two/three/item.jpg.
Any idea how I can exclude this first segment?
First I would wrap the regex location with a prefix location. This isolates the regex location and keeps it from conflicting with others. Your config will scale more smoothly as a result.
If there will only be images in this directory, then it's simpler to capture everything after /images/ and not worry about the file extension. You're then free to have a case sensitive match which is a little quicker.
location /images/ {
location ~ ^/images/(?<img_path>.+) {
try_files /disk/$img_path /images?uri=$img_path;
}
}

nginx rewrite nice urls with homemade CMS

I have a home-made CMS, serving a site which I inherited. I'm not really familiar with nginx rewrite rules, although I could set up tiny URLs. Here is my relevant part of the configuration:
*location / {
index index.php index.html;
root /var/www/www.valami.hu;
try_files $uri $uri/ #seo;
}
location #seo {
rewrite ^/([a-z]+)$ /index.php?oldal=$1 last;
break;
}*
The problem is that the site has a blog which is located on blogspot.com and the stuff from the blog is taken from there. So what I need help with is a rule for this sort of URL:
http://www.valami.hu/index.php?oldal=blog&options=2012/01/some-title-here.html
So, it would be fine like:
http://www.valami.hu/blog/2012/01/some-title-here
The most important is the first rule should be work also as it is more frequently used.
This is actually trivial. Watch and learn!
location / {
try_files $uri $uri/ #site;
}
location #site {
rewrite ^/blog/(.+)$ /index.php?oldal=blog&options=$1 last;
rewrite ^(.+)$ /index.php?oldal=$1 last;
}
The order makes all the difference. You can also do it by removing the last flag and redirecting to /blog with the options query string parameter explicitely set. No if is needed.
well seems we only have 2 cases, the /blog and the non /blog, I'd write 2 location blocks
location ~ ^/blog/(.*) {
try_files $uri /index.php?oldal=blog&options=$1;
}
location ~ /(.*) {
try_files $uri /index.php?oldal=$1;
}
I would have used just / and $request_uri in the second location but that would put a preceeding / in olda1, if that wouldn't matter with you then i'd prefer that method, cause it doesn't involve regex.
About index index.php index.html; and root /var/www/www.valami.hu;, it's better if you move them to the server block instead of the location block, if possible of course.

Resources