Nginx try_files with regex - nginx

I'm trying to configure my Nginx conf file but it doesnt want to work ...
I want to serve some pdf files.
When you write the url "127.0.0.1/abcFile.pdf" Nginx should serve you the file which is located here : "/a/b/c/abcFile.pdf"
I can have multiple folders. Each time first 3 letters of the file name are the folders names.
So i'm trying this :
location ~* "\.(\w{1})(\w{1})(\w{1})(\w*.pdf)$" {
try_files $uri $uri/ /$1/$2/$3/$1$2$3$4 ;
}
But I only got an error 500... I can't figure out what i'm missing here.
This kind of code work with rewrite:
rewrite "(\w{1})(\w{1})(\w{1})(\w*.pdf)" $1/$2/$3/$1$2$3$4 permanent;
But here i'v an issue too : Too many redirect. If I remove the "permanent" it doesn't work anymore.
If you have any suggestion :) Thanks a lot !

Related

Add location station in Nginx config file in Synology NAS

I need to modify the nginx config file (/etc/nginx/app.d/server.webstation-vhost.conf) to add one line, which is for Laravel routing work correctly.
location / { try_files $uri $uri/ /index.php?$query_string; }
The problem is /etc/nginx/app.d/server.webstation-vhost.conf will ALWAYS OVERWITTEN once reboot the NAS,
Does anybody having experience how to hand this problem.
Many Thanks !
Not sure if you figured this out, but if you haven't, under that vhost conf file (/etc/nginx/app.d/server.webstation-vhost.conf), look for something like:
include /usr/local/etc/nginx/conf.d/f2f0a62b-74d6-4c34-a745-d0156f13c9d6/user.conf*;
Instead of f2f0a62b-74d6-4c34-a745-d0156f13c9d6 you should see another unique id for your nginx app, create/edit the mentioned user.conf file (without asterisk) with the contents you need, in my case I created a file with the contents below:
location / {
try_files $uri $uri/ /index.html;
}
Then I had to restart nginx with the command sudo synoservice --restart nginx.
And it worked.
PS.: I believe it should work for any DSM v6.1 or later (maybe 6.0.x as well).
For research I used:
https://community.synology.com/enu/forum/1/post/122043
https://community.synology.com/enu/forum/1/post/120538

Nginx URL routing

I think this is kind of basic stuff, but I'm struggling to find proper guide that would explain these things:
I have a index.php file and nginx config so that https://dev.something.com works ok.
But I need to change nginx config so that that address produces blank page, and index.php only works from https://dev.something.com/lists. I could put index.php inside lists directory, but isn't there more subtle solution?
And here's the hard part:
Users should be able to access
https://dev.something.com/lists/userName
https://dev.something.com/lists/userName/listName
userName and listName should be used as GET-parameters.
Can anyone help how I could achieve this kind of config with nginx?
You're asking a few (relatively basic) questions, and I would suggest you start with their free e-book https://www.nginx.com/blog/announcing-oreillys-new-book-nginx-a-practical-guide-to-high-performance/
You can define where nginx looks for index files with the root clause, and though they normally use the URL context relative to the server's root, it can be override in each location.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
You can use portions of URLs as variables, which can be passed as paramters too.
location = /lists { # '=' will match exactly, no trailing url
root /path/where/index.php/lives;
try_files $uri /index.php;
}
location /lists { # this will match anything under that url
rewrite ^/lists/(\d+)/?$ /lists?user=$1; # matches username
rewrite ^/lists/(\d+)/(\d+)/?$ /lists?user=$1&list=$2; # matches username/list
}
location /{ #everything else
root /path/where/index.html/lives; #index.html is empty file.
try_files $uri /index.html;
}

NGINX Wordpress install in subdirectory wrong script URL

Switching from Apache to NGINX here :)
So I have Site A which sits on the root directory http://test.fake.com/ and works fine. I also have another Wordpress Site (B) where the root is http://test.fake.com/b/
All the front end pages for B load scripts and css like http://test.fake.com/test.fake.com/b/ which of course is incorrect. When I try to goto the Admin like http://test.fake.com/wp-admin/ I receive No input file specified. as the only output, also the URL changes to http://test.fake.com/test.fake.com/b/wp-login.php.
Here is a snippet of my NGINX config where I think the problem lies, please let me know if you need more info and be gentle on a NGINX n00b :P
location /b {
try_files $uri $uri/ /b/index.php$args;
}
location / {
# Set try_files according WP architecture
try_files $uri $uri/ /index.php$args;
}
Here is a snippet of B's wp-config file where I am setting the URLs
define('WP_HOME','http://test.fake.com/b');
define('WP_SITEURL','http:/test.fake.com/b');
I have no idea where the error lies :(
Thank you for viewing!

Nginx, render a file with dynamic name

I have a cachebreaker that produces css filenames as this one: /css/vendor.min.333311133.css.
I want nginx to answer request with a previous version with the latest.
Note that I will only have one version of that file in the directory... so I'm thinking of a rule like the following, but it's not working:
location ~* /css\/vendor\.min\.(.*)\.css {
try_files $uri ~* /css\/vendor\.(.*)\.css =404;
}
Does anybody knows if nginx support dynamic names in try_files? Or should I use another directive? Any ideas?
If your build process is able to do this, just put the file there without the cachebreaker part and do this:
location ~* /css\/vendor\.min\.(.*)\.css {
try_files /css/vendor.min.css =404;
}
In theory you can use a regex capture inside the location, but since the old version number would be in there, it would not help.

Nginx: serving static files from multiple locations (probably very easy to solve)

I have a very simple case of serving static files via nginx yet I can't figure it out.
I want all URLs beginning with /static/ to serve static files from directory /foo/bar/dir1 and if the file isn't there, serve from /foo/bar/dir2, if not there, return 404.
So for example when handling URL /static/some/file.png I want nginx to first try
/foo/bar/dir1/some/file.png
and then
/foo/bar/dir2/some/file.png
I know I should probably use something like this
location /static/ {
try_files .... something .....
}
but the documentation on try_files is very unclear to me. I tried a lot of combinations but nothing seems to work. Multiple alias directives would do the job but it won't work. I think the solution must be very simple but I cant get it right. It's kind of hard to debug how nginx resolves all these locations and files...
You can customize the root (make sure to update the try_files after). And also make sure there is no root directive in location /
location ~* ^/static/(.+)$ {
root /;
try_files /foo/bar/dir1/some/$1 /foo/bar/dir2/some/$1 =404;
}
Edit: Removed the need of the static folder.

Resources