How to set location with extension in NGINX server - nginx

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

Related

How can I manually set static html files for each location block

I am trying to play with nginx. I am trying to serve a particualar index.html files for each location declared on my configuration file like
location / {
root /usr/src/seo/homepage;
}
location ~ /mypage {
root /usr/src/seo/mypage;
}
location ~ /mypage2 {
root /usr/src/seo/mypage2;
}
Where each of the folder location specified has it's own index.html file. But when I try to access mypage and mypage2, nginx returns 404. I am expecting it to render it's respective index.html
UPDATE!!!
Solved it using alias like:
location / {
alias /usr/src/seo/homepage;
}
location ~ /mypage {
alias /usr/src/seo/mypage;
}
location ~ /mypage2 {
alias /usr/src/seo/mypage2;
}
From the docs:
To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive
I.e. when /mypage is requested, nginx tries to find /usr/src/seo/mypage/mypage.
To address that, location blocks for mypage and mypage2 should look something like
location ~ /(mypage|mypage2) {
root /usr/src/seo;
}
That however requires the request to end with a slash / for index directive to work. So it might be a good idea to include try_files:
location ~ /(mypage|mypage2) {
root /usr/src/seo;
try_files $uri $uri/ =404;
}

Nginx one alias for multiple location directives

I have installed a project in a separate directory to make this project available on multiple domains
location ^~ /phpRedisAdmin/css/ {
alias /home/phpRedisAdmin/css/;
}
location ^~ /phpRedisAdmin/js/ {
alias /home/phpRedisAdmin/js/;
}
location ^~ /phpRedisAdmin/images/ {
alias /home/phpRedisAdmin/images/;
}
To prevent declaring a location directive for each one of the project subdirectory, is it possible to declare one location directive that would handle all the possible aliases instead?
I am looking for something like this (I don't know how to retrieve in the alias directive what is in the parenthesis):
location ^~ /phpRedisAdmin/(css|images|js)/ {
alias /home/phpRedisAdmin/>> what should I insert here? <</;
}
Problem solved, I ended up using this solution:
location ^~ /phpRedisAdmin/(css|images|js)$ {
alias /home/phpRedisAdmin/$1;
}
You should not use an alias directive, where a root directive will suffice. One solution would be to define a location block for all URIs which live below /phpRedisAdmin. For example:
location ^~ /phpRedisAdmin/ {
root /home;
location ~ \.php$ {
...
}
}
If you need to execute PHP scripts within this location, add a nested location (as shown above).
If you must use a regular expression location with an alias, you will need to capture the entire remainder of the URI. For example:
location ^~ /phpRedisAdmin/((?:css|images|js)/.*)$ {
alias /home/phpRedisAdmin/$1;
}
See this document for details.
You can try below
location ~* /phpRedisAdmin/(?P<folder>css|images|js)/ {
alias /home/phpRedisAdmin/$folder;
}

Nginx rewrite location path

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;
}
}

nginx redirect loop, index.html

This seems ridiculous but I've not found a working answer in over an hour of searching.
When I access "http://oa.wechat.com/screen/index.html", it will cause a 301 redirect loop, like this:
"GET /screen/ HTTP/1.1" 301
"GET /screen/index.html/ HTTP/1.1" 301
"GET /screen/index.html/index.html/ HTTP/1.1" 301
...
nginx verson: 1.5.6
nginx.conf
server {
listen 80;
server_name oa.wechat.com;
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
}
Could anyone tell me the reason? Thanks very much.
i have checked nginx document. the right usage of 'alias':
# use normal match like this
location /i/ {
alias /spool/w3/images/;
}
# use regex match like this
location ~ ^/download/(.*)$ {
alias /home/website/files/$1;
}
the wrong way to use 'alias' is:
location ~ ^/screen/ {
alias /data/screen/static/;
index index.html;
}
In this case, the request would be considered as a directory request, not file request, which will lead a redirect loop.
Anyway, Thanks Flesh very much!
It's already trying to access index.html in that directory because it's the default of nginx's index directive. The problem is that you're using the index directive within a location block where it has a special meaning and executes an internal redirect (as documented).
Unless you know what you're doing, set the index directive within the server block. We end up with the following server block (be sure to read the comments).
server {
# Both default values and not needed at all!
#index index.html;
#listen 80;
server_name oa.wechat.com;
# Do not use regular expressions to match the beginning of a
# requested URI without protecting it by a regular location!
location ^~ /screen/ {
alias /data/screen/static/;
}
}
location examples
server {
# Won't work because the /data is considered the new document root and
# the new location matches the regular expression again.
location ~ ^/screen/ {
alias /data/screen/static/;
}
# Should work because the outer location limits the inner location
# to start with the real document root (untested)
location / {
location ~ ^/screen/ {
alias /data/screen/static/;
}
}
# Should work as well above reason (untested)
location / {
location ~ ^(/screen/) {
alias /data$1static/;
}
}
# Might work as well because we are using the matching group
# VERY BAD because we have a regular expression outside any regular location!
location ~ ^(/screen/) {
alias /data$1static/;
}
# Always works and allows nesting of more directives and is totally save
location ^~ /screen/ {
alias /data/screen/static/;
}
}
Weblinks
alias documentation
index documentation
location documentation
you should move ^ location modifier from ^/screen/, then add ^ before ~, like this:
`location ^~ /screen/ {
alias /data/screen/static/;
index index.html;
}`

Nginx locations - case insensitive with spaces

My question is about nginx "location" configuration blocks:
If I want to make a location with a space character (well, %20) in the URL I can do it like so:
location ^~ "/Style Library/" {
}
If I want to make a case-insensitive location, I can do it like so:
location ~* ^/StyleLibrary/ {
}
However, I can't find a way of getting case-insensitive locations with space characters working. None of these appear to work:
location ~* "^/Style Library/" {
}
location ~* ^/Style[^_]Library/ {
}
location ~* ^/Style\sLibrary/ {
}
location ~* ^/Style.Library/ {
}
Can anyone help?
Do you have other regex locations that may be handling the request earlier in the server block? I just ran a test locally and was able to make the following location work:
location ~* "^/Style Library/" {
rewrite ^ /dump.php;
}
where /dump.php is just a simple script that does a var_export($_SERVER);
I tested this with
curl -i "dev/StYlE LiBrArY/"
I'd guess that some other location is handling the request instead of that regex location.

Resources