I'm trying to serve my AppLink for google association services. The following works:
location /.well-known/assetlinks.json {
root /var/www/static/google-association-service/;
types { } default_type "content-type: application/json";
}
Provided I have the correct file placed at
/var/www/static/google-association-service/.well-known/assetlinks.json
The URI is what it it is, Google gets to decide that, but I'd rather not have it resolve to a hidden file at the bottom of a directory where the next guy wonders why the directory is even there because he forgot to ls with a '-a'. Is there a way to make it so I can map this URI to something like:
/var/www/static/google-association-service/assetlinks.json # omit the hidden sub directory
?
(I've tried understanding the difference between root and alias, but I'm not seeing how alias would help me here)
alias basically gives you the possibility to serve a file with another name (e.g. serve a file named foo.json at location /.well-known/assetlinks.json). Even if this is not required in your case I would favor this config, as it is easily understandable:
location = /.well-known/assetlinks.json {
alias /var/www/static/google-association-service/assetlinks.json;
}
Note that the = is required to not match locations like /.well-known/assetlinks.json1111.
You can use try_files to find the specific file you want.
For example:
location = /.well-known/assetlinks.json {
root /var/www/static/google-association-service;
try_files /assetlinks.json =404;
}
Or:
location = /.well-known/assetlinks.json {
root /var/www/static;
try_files /google-association-service/assetlinks.json =404;
}
The concatenation of the root value and the try_files parameter form the path to the file.
Related
I'm trying to serve a directory of user-provided files with nginx, but with an index file that is outside of the document root. The reason I don't want to locate the index file inside the directory is because the directory is meant for users to drop their stuff in there.
Nginx's index directive seems to only work for files inside the document root; the documentation says that the index can be an "absolute path", but my experiments tell that this is only relative to the document root.
So, I tried to serve another index location with location =/index.html { alias /path/to/index.html }, and this manages to work when /index.html is directly requested, but it doesn't work if / is requested (403 with log "directory index of "/srv/docroot/" is forbidden"). It starts to work, though, if I create an empty file to /srv/docroot/index.html; then /path/to/index.html is served at /. So it seems that Nginx
Checks if /srv/docroot/index.html file exists in the filesystem at the /'s location.
If it exists, it does an internal redirect, and serves the /path/to/index.html at /index.html location.
What is the correct way to serve an index file outside of the document root?
index and try_files directives, provided with absolute paths, seem only to be able to point to the files inside the current document root. (With relative ../ paths you can point outside of the document root, but that's not ideal if you want to point to an absolute path in the filesystem.)
It seems that only the alias directive can point outside the document root. I was able to get my setup to work with:
...
location / {
try_files $uri /index.html;
}
location =/index.html {
alias /path/to/index.html;
}
...
This doesn't strictly answer to the question in the sense, that in this case, index.html isn't shown only when / is requested, but always when a matching file isn't found. I'm happy with this solution, but it might make sense in some cases to separate the 404 error.
How can I serve a PDF file at the below address:
127.0.0.1/getMeThatFile/willYou?name=jane
which is stored at a location:
/usr/share/nginx/thatFile.pdf
I tried to follow Serve pdf file by location in nginx, but couldn't get it to work:
server {
location /getMeThatFile/willYou/ {
alias /usr/share/nginx/;
return 302 thatFile.pdf;
}
}
/getMeThatFile/willYou and /getMeThatFile/willYou/ are different URIs. Your question suggests that the request will use the first, but your solution matches the second.
Use location /getMeThatFile/willYou to match both, or location = /getMeThatFile/willYou to match only the first. See this document for details.
To return a single file, use root and try_files. For example:
location = /getMeThatFile/willYou {
root /usr/share/nginx/;
try_files /thatFile.pdf =404;
}
Assuming thatFile.pdf always exists, the =404 is never reached, but is necessary as try_files requires at least two parameters. See this document for details.
I think I have a problem understanding how I can check if a file exists in a specific folder with nginx.
for example, I use this url:
www.domain.tld/folder/filename.pdf
now, I think I have to check it like that:
location /folder/.(pdf)$ {
}
is that correct?
and then, if it is the right way, how can I redirect if the file doesn't exists?
I'm new and from apache os it is a bit hard for men to switch in my mind
The location directive matches the requested URI, but does not decide if the file exists. The contents of the location block determines the action if the file exists or not, and the simplest way to accomplish that is using try_files.
The try_files directive will test if the file exists and internally redirect to another URI if it does not.
For example:
location ~ ^/folder/.*\.pdf$ {
try_files $uri /another/uri;
}
In the interests of keeping my codebase modular, I have two static websites - site_a, which is the parent site, and site_b, which may be considered a child site. I am trying to find the right way to append site_b to a path of site_a, such that site_a/path is equivalent to the site_b's / directory.
Note that each site instance has its own set of static resources (img, css, js), and should be referenced from the corresponding web directory on the server, and there may be some overlap in the names of some of the resources (eg. style.css) and folders (eg. /img/..).
Any pointers of help would be very much appreciated!
What you are searching is a proxy_pass (Or I think it would work in your case).
site_a configuration:
location = /path {
return 301 /path/;
}
location = /path/ {
proxy_pass http://site_b/;
}
This should work if you only want it to be exactly like this, be careful, because the /path part is changed by / , but if you need the rest of the url, you could do:
location = /path {
return 301 /path/;
}
location /path/ {
proxy_pass http://site_b/;
}
Like this, site_a/path/pathtoglory/ would show site_b/pathtoglory/.
Choose whichever you like the most (or fits your actual situation).
Any more info on proxy_pass for special configurations can be found here:
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
I would like to remove all GET parameters from the index file in the root folder, while leaving all GET parameters everywhere else.
Example:
http://support.oursite.com/?ref=inline
I would like that to get ported to
http://support.oursite.com/
While
http://support.oursite.com/tickets/?id=1934
Would still contain the ID parameter.
I have been able to wipe the parameters, what I'm looking for is help on limiting that wipe to just the root index.
I've found the solution to this, for anyone who comes across this issue.
I have two separate location parameters:
location ~* ^/(.+)$ {
proxy_pass http://192.168.1.1/$1$is_args$args;
}
location / {
proxy_pass http://192.168.1.1/;
}
The use of the (.+) tells Nginx only run this location if there is something after the slash. Because GET parameters aren't processed in that spot, it's safe to do this.