nginx rewrite virtual directory to file - nginx

This should be really easy to do but I'm hitting my head on the wall. If I get a request for www.mysite.com/mypath I want to serve the content of www.mysite.com/myotherpath/thisfile.html. How can I do this with an nginx config.

location = /mypath {
try_files /myotherpath/thisfile.html =404;
}
http://nginx.org/r/try_files
http://nginx.org/r/location

Use rewrite directive within proper location block. So for example you have basic location which will handle all requests
location / {
/*your rules here*/
}
You will need to add another block, which will do for you handling of specific path
location /mypath {
rewrite ^/mypath$ /real/path/to/file/thisfile.html;
}
Also for your server to think in that block that thisfile.html is default you can use try thisfile.html directive
It is all well explained on official page Official Nginx RewriteModule page

Related

NGINX try_files works for all but one url

I have a simple dashboard for my site. Here is the directive:
location /dashboard {
try_files $uri /dashboard/index.php;
}
It works for all items after /dashboard. For example, /users or /pages - all CRUD operations work as expected.
The index.php file at /dashboard is my "controller". It parses the url and includes and runs scripts from there.
For example: /dashboard/group/edit/123456 works as expected and I get the edit page for group number 123456.
But when I post from that page to /dashboard/group/update, it serves /dashboard/group/index.php
So, in the first example, The edit page is loaded and the url at the top of the screen does not change.
In the second example, NGINX is CHANGING the url so my script cannot get the url parts to do the job.
I thought it may have something to do with POST, but I have other forms that use POST without issue.
In addition, or possibly a clue, try_files is returning /dashboard/group/index.php while the directive should return /dashboard/index.php.
Is there another NGINX file that could have so old code in it that is overwriting this domain's config?
I've been at this a few hours and have run out of ideas. Any thoughts?
* One More Clue *
When I BROWSE to /dashboard/group/update, NGINX shows the page as expected. It is only when I POST to that page that NGNIX sends me to /dashboard/group/index.php.
Again, at the very least, it should be sending me to /dashboard/index.php and NOT /dashboard/group/index.php.
You not send all after /dashboard try this:
location /dashboard {
try_files $uri /dashboard/index.php?$uri&$args;
}
OR
location /dashboard {
try_files $uri /dashboard/index.php?$query_string;
}
Nginx docs: https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Instead of
location /dashboard {
try_files $uri /dashboard/index.php;
}
Try
location /dashboard {
index index.php; #adding this may work alone
try_files $uri /dashboard/index.php?$uri;
}
I have concluded that I have a cache problem. The location directive works on all items that I have not yet accessed.
So, my configuration - as described - works as it should.
I just have to figure out how to clear my cache ( which in NOT set up in NGINX that I can see!)
Thank you all who helped!

Nginx - Forwarding a request to the Server-App if 'alias' fails

For a project I implemented automatic serverside thumbnail-generation. If you request an Image with '_thumb' in its filename the server checks if there already is a thumbnail for the original image, if not it generates it and sends it back. Works fine.
Now I'd like to bring Nginx into the mix to serve the images directly from the harddrive without involving the server-app (the url itself would work fine and return the image if requested from the server-app itself).
The following Block works fine, if the thumbnail already exists. If there is no thumbnail already generated it just responds 404 - unsurprisingly.
How would I modify the following Nginx-Block to make another request to the server in case the thumbnail cant be found to create it?
Request
http://www.proj.com/files/images/234782348234/bunny_thumb.jpg
Nginx-Block
location ~* ^/files/images/(\w+)/.+_thumb\.(jpg|png|gif)$ {
alias /srv/proj/data/uploads/images/temp/$1_thumb.$2;
// No image found? Request it from the server directly on "files/images/234782348234/bunny_thumb.jpg"
}
Actual File-Path
/srv/proj/data/uploads/images/temp/234782348234_thumb.jpg
Sometimes it helps to read the excellent blog of the nginx company.
What you search for is called named location with try_files.
https://www.nginx.com/resources/admin-guide/nginx-web-server/
https://www.nginx.com/resources/admin-guide/serving-static-content/
location / {
try_files $uri $uri/ #backend;
}
location #backend {
proxy_pass http://backend.example.com;
}
For your case I would try the following
location ~* ^/files/images/(\w+)/.+_thumb\.(jpg|png|gif)$ {
try_files /srv/proj/data/uploads/images/temp/$1_thumb.$2 #callapp;
// No image found? Request it from the server directly
// on "files/images/234782348234/bunny_thumb.jpg"
}
location #callapp {
...
}
So, finally had to tackle it again and found a pretty easy solution. RegexMatches stillt won't work in try_files. So I just used "alias" to direct to the actual file-path and defined a 'error_page 404' pointing to the expressapp:
location ~* ^/files/images/(\w+)/.+_thumb\.(jpg|png|gif)$ {
error_page 404 #makethumb
alias /srv/proj/data/uploads/images/temp/$1_thumb.$2;
}
location #makethumb {
proxy_pass http://localhost:3000$uri;
}

Nginx URL rewrite

I have a website that has laravel setup to run under http://www.example.com/lara/
So, most Laravel pages have URLs of type http://www.example.com/lara/page/23 OR http://www.example.com/lara/category/23 etc.
Nginx is the underlying server and it has the following configuration to handle these requests:
location /lara/ {
try_files $uri $uri/ /lara/index.php?$query_string;
}
Everything works ok.
Now, I need to setup a special page with the URL http://www.example.com/mystuff/ which actually is handled by
http://www.example.com/lara/category/29
To get this working I added the following rewrite right below location /lara/, that is:
rewrite ^/mystuff/(.*)$ /lara/category/29/$1 last;
Unfortunately, I get a page not found error. Any insights?
Further investigation & research:
1)
location /mystuff/ {
return 301 /lara/index.php/category/29;
}
worked although that's not (browser address bar changes to) what I actually want.
2) Looks like Laravel is not seeing the updated REQUEST_URI.
Try this
server {
...
rewrite ^/lara/(.*)\..*$ $1/mystuff last;
return 403;
...
}

Nginx rewrite requests to subdomain.tld/files/* to external domain

I'd like to rewrite all requests to Nginx matching http://*.examle.tld/files/* to http://$1.otherdomain.tld/files/?file=$2. I'd also like to rewrite the same request without the subdomain i.e. http://example.tld/files/* to http://otherdomain.tld/files/?file=$1
The reason for this is to use production files from local development without having to sync folders.
This is what I've got so far, however without success:
location / {
...
rewrite ^http://(\w+)\.(.*)/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$3;
rewrite ^.*/files/(.*) http://$1.otherdomain.tld/inc/reader.php?file=$1;
...
}
Thank you for any assistance.
You cannot use the server name as part of the rewrite directive's regex. If you have a server block with a wild card server_name as described here, you can use a named capture for use later within the block.
For example:
server {
server_name ~^(?<sub>\w+\.)?example\.tld$;
location /files/ {
rewrite ^/files(.*)$ http://${sub}otherdomain.tld/files/?file=$1 permanent;
}
}
See this document for details.

Nginx load another file at home page

I have an Nginx configuration that's working, but I have a weird situation. The /index.html file isn't what I want to load at / or /index.html. At those locations I want to load /home/index.html. I'm not sure what the best strategy is to configure this. This is the pseudo-config I have so far.
location [/ or /index.html] {
load /home/index.html instead, respond 200 not with a redirect;
}
You might use try_files for this. Something like:
location / {
try_files $uri /home/index.html;
}
Note: This will give you /home/index.html for every bad address too, maybe not what you want.
Edit: Or just symlink /index.html to /home/index.html.

Resources