Static files getting 404 with nginx proxy - nginx

I have configured nginx to serve static page as below.
location /home {
alias /home/username/files/home;
try_files $uri /home/index.html;
}
Only index.html loads on http://example.com/home. All other css, js and image files are getting 404 as request is still going for http://example.com/css/style.css instead of http://example.com/home/css/style.css. Is there any way I can configure /home prefix to all the static file without manually adding to all static files?

You can try to use variable root depending on HTTP Referer header (although it is a dirty hack):
map $http_referer $root {
~example\.com/home/ /home/username/files/home;
default <your default root here>;
}
server {
...
root $root;
location /home {
root /home/username/files;
try_files $uri /home/index.html;
}
}
Note that if any of your web pages under /home/ have a links to the main site, those links won't work correctly because of example.com/home/... HTTP Referer header value.
Why root /home/username/files; instead of alias /home/username/files/home;, you ask? Because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}

Related

redirect from one folder into another using nginx

I am running any Nginx file server. I have 2 main folders i.e. folder-a and folder-b. If a user tries to land on /folder-a/abc and it's a 404, I should auto-redirect to another folder like /folder-b/abc. How do I set up such a rule in Nginx? My top folder names will always be hard-coded names like folder-a and folder-b.
As being said by #RichardSmith, if you want to look for a file in one of two locations, you can use try_files directive:
location ~ ^/folder-a(?<suffix>/.*) {
try_files $uri $uri/ /folder-b$suffix /folder-b$suffix/ =404;
}
If you want to generate an HTTP redirect, you can use error_page directive with the additional named location:
location ~ ^/folder-a(?<suffix>/.*) {
error_page 404 #redirect;
}
location #redirect {
return 301 /folder-b$suffix;
}
If you have some additional configuration directives in your root location (location / { ... }), you should either duplicate them inside the location ~ ^/folder-a(?<suffix>/.*) { ... } or move them to the server context if there are no other locations where those directives should not be applied.

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

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

How can I serve a particular file for root url in Nginx?

I need to serve file from my filesystem for GET / request. I tried the following:
location = / {
index page2257160.html;
root /var/www/site/;
}
The rest of requests are proxied to backend:
location / {
proxy_pass http://localhost:1234;
}
But when I do the request, instead of serving the file from filesystem, nginx asks backend about /page2257160.html, backend returns 404, nginx sends this 404 back to client.
How can I fix this?
The index directive performs an internal redirect, so you will need a second location to match the rewritten URI. For example:
root /var/www/site/;
location = / {
index page2257160.html;
}
location = /page2257160.html {
}
See this document for details.
You can achieve the same thing with one location block and a try_files directive. For example:
location = / {
root /var/www/site/;
try_files /page2257160.html =404;
}
See this document for more.

Resources