Nginx Proxy_pass try_files drop to location handler - nginx

I'm running into small hick up with try_files in combination with proxy_pass (or alias for that matter).
Current config:
location /staticsrv {
alias /var/www/static/trunk/;
#proxy_pass http://static.localtest.nl/;
}
location ~ ^/staticsrv/images/gallery/(.*)$ {
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^(.*)$ /index.php/?c=media&m=index&imag=$uri;
}
However for every file it gets dropped to the rewrite rule as it doesn't exist.
Is there a "trick" (read correct config) to fix my misfortune? Or is it just not possible? Both domains will eventually be on the same server so we can work with alias and proxy_pass.
Thanks in advance

Your location ~ ^/staticsrv/images/gallery/(.*)$ needs a root or an alias to construct a local path for try_files to try. Also, you do not necessarily need a regular expression here:
location /staticsrv/images/gallery/ {
alias /var/www/static/trunk/images/gallery/;
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
}
proxy_pass will not work with try_files as one deals with remote content and the other with local content.
I try to avoid using alias and try_files in the same location block because of this open bug.
A possible work around would be to use another intermediate URI that closely matches the document root:
location /staticsrv/images/gallery/ {
rewrite ^/staticsrv(.+)$ /trunk$1 last;
}
location /trunk {
internal;
root /var/www/static;
try_files $uri #img_proxy;
}
location #img_proxy {
rewrite ^ /index.php/?c=media&m=index&imag=$uri last;
}

Related

nginx rewrite specific folder to php and arg

Am trying to redirect
domain.tld/blog/read.php?article=first-article to domain.tld/blog/first-article
What I tried and didn't work resulting in redirect to domain.tld/first-article
location "^blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
location /blog {
rewrite ^/blog/?$ /blog/read.php?article=? last;
rewrite ^/blog/([-a-zA-Z0-9_]+)/$ /blog/read.php?article=$1? last;
}
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
Thinking the issue comes from my other parts in the config and mainly second location from below
server {
...
...
location ~ "^/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1;
}
location ~ "^/([^/]+)/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1&crversion=$2;
}
...
Any pointers would help a lot
Cheers
So the fast answer is actually the fact that my config was fine first time, yet nginx config is read top bottom with first match being the one that is used.
So in the end the "fix" was adding the blog part upper in the site config
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}

Nginx not matching the location

Can anyone tell me why is this ngnix config doesn't match all URL that starts with /admin :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
It always fall back to the default content of location / . However, I hardcoded all the possible URL in the Nginx config, it works and only matches the hard coded URL, something like :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/news/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/another-url/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
Thanks for your help.
The final term of the try_files statement is a URI. The URI of the index.html file at /path/to/admin/build/index.html is /admin/index.html.
Using alias and try_files in the same location block can be problematic.
You may want to use a more reliable solution:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
The location and alias values should both end with / or neither end with /. The ^~ operator will prevent other regular expression location blocks from matching any URI that begins with /admin. See this caution on the use of if.

nginx:how use alias route different paths

i want point /m/v2/,And then the random path,for example apc, tcb and so on. To /var/www/app.But my configuration shows 404
location ~* ^/m/v2/([a-z]+)/(.*)$ {
try_files $uri $uri.html $uri/ #htmlext;
alias /opt/www/app/;
index myInterest.html;
access_log logs/app_v2_access.log main;
}
location #htmlext {
rewrite ^(.*)$ $1.html last;
}
Using alias with a regular expression location you should construct the full path to the file in the alias expression. See this document for details.
Using alias with try_files is challenging. See this bug report for details.
Your $uri.html and #htmlext terms seem to be attempting the same thing. In my example, an if block is used instead of the try_files statement. See this caution on the use of if.
For example:
location ~* ^/m/v2/([a-z]+)/(.*)$ {
alias /opt/www/app/$2;
index myInterest.html;
access_log logs/app_v2_access.log main;
if (-e $request_filename.html) {
rewrite ^(.*)$ $1.html last;
}
}

Nginx: serving static files by URL path

Is there any way of serving static files by only some URL path? For example, next URL pattern http://host/static/*.png has /static/ substring (path), and Nginx will serve any statics from there.
In the web server documentation I found an example:
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js)$ { ...
and defined my Nginx config like that:
location / {
try_files $uri $uri/ /index.html;
}
location /apib {
#some proxy_pass
}
location /apim {
#some proxy_pass
}
location /api {
#some proxy_pass
}
I try to add additional location for */static/*.* with root dir /var/www/some_statics.
location ~* ^/static/.+\.(png|whatever-else)$ {
alias /var/www/some_static;
expires 24h;
}
location / {
# regular rules
}
Hand written, may contain mistakes.
If you want to extend the rules to match anything/something/static/*.png just remove the ^ in the patten.
location ^~ /static/ {
alias /var/www/some_static;
}

Nginx rewrites static files to index.html

I'm trying to work on a single page app - I need to rewrite all urls to index.html but allow existing static files (.css and .js) to be served as they normally would be in a browser.
This is the code that I'm trying to use to re-write but it serves my static files to the re-write as well
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
you don't actually need a rewrite for that in nginx, just use try_files like so:
location / {
try_files $uri /index.html;
}
what this does is for all url's:
try the exact static filename match, and serve it if present
if 1 didn't serve anything, then server /index.html instead
see http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
This should work:
server {
listen 1.2.3.4:80;
server_name domain.eu;
root /usr/local/www/domain.eu/public;
try_files $uri #rewrites;
location #rewrites {
rewrite ^/favicon.ico$ /pictures/favicon.ico last;
rewrite ^ /index.html last;
}
}

Resources