Nginx rewrite location path - nginx

Is there a way I can add a rewrite which will dynamically set the location/path of the file?
The following is from the nginx config:
server {
root /media;
server_name media.domain.com;
location / {
autoindex off;
}
I have images with names like "e9m7L4_1.jpg" that are stored in a directory according to the first 6 letters/numbers of the filename, for example:
e9m7L4_1.jpg (stored in)-> e/9/m/7/L/4/e9m7L4_1.jpg
km40lj_1.jpg (stored in)-> k/m/4/0/l/j/km40lj_1.jpg
Currently I can access it like this:
http://media.domain.com/e/9/m/7/L/4/e9m7L4_1.jpg
Is there a way to rewrite the location using filename passed to nginx so it could be accessed like this, without the long directory path/prefix:
http://media.domain.com/e9m7L4_1.jpg
Thanks

You could try this:
server {
server_name media.domain.com;
root /media;
location / {
rewrite ^/((.)(.)(.)(.)(.)(.).+)$ /$2/$3/$4/$5/$6/$7/$1 break;
}
}

Related

NginX redirect /service to /service/

I have two servicesr accessible via NginX. The web server configuration looks like this:
location /service1/ {
# process php files
}
location /service2/ {
proxy_pass http://127.0.0.1:9999/;
}
However, if one clicks on https://example.com/service1 (or 2) he gets a 404 error. There is no folder called "service1" (or 2) in the website root.
I would like links to "https://example.com/service1" to point to "https://example.com/service1/" (with trailing slash), possibly without specyfing a redirect for every service I have, i.e
location = /service1 {
return 301 https://$host/service1/;
}
location /service1/ {
# process php files
}
location = /service2 {
return 301 https://$host/service2/;
}
location /service2/ {
proxy_pass http://127.0.0.1:9999/;
}
I have already tried try_files $uri $uri/ =404;, but it seems to only work for real files and folders in the website root, no "virtual" subdirectories.
I am avoiding configurations like location /service { ... } because they seem more vulnerable.
Inside your location = blocks you need to generate an internal redirect to $uri/. You can achieve this using try_files or rewrite...last.
For example:
location = /service1 {
try_files nonexistent $uri/$is_args$args;
}
Notice that the internal redirection must be the last parameter. See this document for details.
Or:
location = /service1 {
rewrite ^(.*)$ $1/ last;
}
See this document for details.

Nginx multiple domains shared resources

I have the following folders:
/web/domain1/
/web/domain2/
/web/shared/
I want domain1 and domain2 to share static files from /web/shared/ but I am having trouble creating the mapping in nginx.
domain1: /assets/ mapped to /web/shared/
domain2: /admin/assets/ mapped to /web/shared/
server{
server_name domain1;
root /web/domain1/;
location / {
rewrite /assets/(.*) /web/shared/$1;
}
}
This gives me 404 error.
Define a location for URIs that begin with /assets/ (see this document for details). Use the alias directive, as the root directive cannot be used in this case (see this document for details).
For example:
location /assets/ {
alias /web/shared/;
}
This works
location /assets/(.*) {
alias /web/shared/$1;
}

nginx serve multi index files from location

I want to archive that serving variable html file through different uri, below is my config.
server {
listen 8888;
server_name localhost;
location / {
root html/test
index foo.html
}
location /another {
root html/test
index bar.html
}
}
I want request for localhost:8888/another then response bar.html which present in my test directory, but I'm failed :(
how could I fix above config, thanks for your time.
The filename is constructed from the value of the root directive and the URI. So in this case:
location /another {
root html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/another/bar.html.
If you want the value of the location directive to be deleted from the URI first, use the alias directive.
location /another {
alias html/test;
index bar.html;
}
The URI /another/bar.html will be located at html/test/bar.html.
See this document for details.

How to set location with extension in NGINX server

I'm new with NGINX server and I was wondering how to set something like this.
location phpmyadmin \.(gif|jpg|png)$ {
root /usr/share/phpmyadmin;
}
example above fails at server restart.
Thanks for all answers.
UPDATE:
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
root /home/safeftp/www/public_html;
}
location ~ "phpmyadmin \.(gif|jpg|png)$" {
root /usr/share/phpmyadmin;
}
If you want to use regex, use ~ (for case-sensitive matching) or ~* (for case-insensitive matching). Your location block should look like this:
location ~ "phpmyadmin \.(gif|jpg|png)$" {
root /usr/share/phpmyadmin;
}
You can read more here: http://nginx.org/en/docs/http/ngx_http_core_module.html#location

nginx how to include rewrite outside of root

How do you call get a file that outside of the root to the be processed. i was reading about the alias and couldnt get it working. so ive tried adding a new root within the location no luck.
this is a cutdown of my config file
server {
listen 443;
server_name domain.com;
---
root ../var/www/domain/public_html;
# works as being called form within the root
location /login {
rewrite ^/login /account/login permanent;
}
#need to
location /validation/code.png {
root /var/www/domain/include;
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
}
You are rewriting the png file to a php file. This will create a sub request for /captcha/display_captcha.php. Is there a location for php in your config? Assuming the php location uses the general root, when the sub request hits this, /captcha/display_captcha.php will not be found an you will get a 404 error.
Your best bet is to copy the php location and create a php location specifically for the php file.
server {
listen 443;
server_name domain.com;
root /var/www/domain/public_html;
...
location = /validation/code.png {
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
location ~ ^/captcha/display_captcha.php {
root /var/www/domain/include
...
# copy php processing code from normal php location.
}
}
Better still, just use '/captcha/display_captcha.php' directly in your html and drop '/validation/code.png' altogether.

Resources