How does one map many URLs to a single file using nginx? - nginx

I have a static file, index.html. How would I configure nginx to serve it from every path on the domain?
URL | file
-----------------
/ | index.html
/foo | index.html
/bar | index.html
/baz | index.html
Essentially, I want a wild card match.
(I realize this will be an unusual setup.)

I faced the same problem a while back and seem to remember doing something along the lines of:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
try_files index.html =404;
}
}
If you don't mind returning an error code (e.g. you're down for maintenance) you could also do something like:
server {
server_name example.com www.example.com;
root /var/www/vhosts/example.com;
location / {
error 503 index.html;
return 503;
}
}

Is this what you're looking for?
rewrite ^(.*)$ index.html

Related

Nginx alias still points to and loads from root directory

I have a django backend and react frontend.
I want to serve the react on / and use /admin, /api and /auth for Django. Here's what I have in my Nginx.
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/frontend;
index index.html index.htm;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
With the above, the expected behavior is
/ uses the default root folder, /home/user/folder/frontend and loads the built index files from react accordingly
/(admin|api|auth) points to django
/static loads static files saved in the /home/user/folder/backend/staticfiles folder.
So not sure why when I hit example.com/static/myfile.css, Nginx is going to /home/user/folder/frontend/static/myfile.css
I'd expect none of the above configuration says that's what it should do, so what magic is going on?
I thought this answer was self explanatory enough, yet Nginx keeps doing whatever it likes.
I'm using nginx/1.18.0 (if that matters)
Try adding root inside the location / directive too.
Like this:
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/backend/staticfiles;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
root /home/user/folder/frontend;
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
Also have a look at those QAs:
serve react frontend and php backend on same domain with nginx
Nginx -- static file serving confusion with root & alias
Deploy both django and react on cloud using nginx
from ngix documentation here, it seems you are missing a / at the end of your paths. this trailing / can cause a lot of pain in many languages to be the root cause of many errors.
please give it a try like this:
# for serving static
location /static/ {
alias /home/user/folder/backend/staticfiles/;
}

Nginx location not resolved

I am trying to have my nginx instance host from two separate static files.
The config for root path works but using any other prefix pattern gives 404 not found.
The config is as follows:
server {
listen 80;
server_name *.saurabhharwande.com;
root /var/www/certbot;
location /abc/ {
index vindex.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
www.saurabhharwande.com Works
www.saurabhharwande.com/abc Gives 404
Am I missing something?
Edit: My basic understanding of how nginx configuration works was flawed. I thought that index file is searched in the root itself /<domain>/<root>/index.html and location is just used to point to different roots. It is rather searched at <domain>/<root>/<location>/index.html

Nginx - homepage redirects to /index

I am trying to configure nginx to serve pure static html pages. I am using Ubuntu 18.04.
The issue is as follows:
When trying to enter my website by url: http://www.mywebsite.com/ it changes URL automatically to http://www.mywebsite.com/index .
When using http://www.mywebsite.com/index.html it also changes URL to http://www.mywebsite.com/index - but I assume it's because of the rewrite rule which removes the .html extension from uri.
I would like to remove the silly "/index" ending when using both "/" and "index.html". I have found a solution, but not sure though if it's a "proper" one:
# If URI equals '/' then find index.html static page
location = / {
try_files /index.html $uri =404;
}
# After rewrite homepage URI equals '/index', it rewrites it to '/'
location = /index {
rewrite /index / permanent;
}
I have attempted to configure it by using simple try_files without any rewrites, returns etc. But still it always changed URI to "/index" no matter what, for the homepage.
My configs:
sites-available/mywebsite.com content
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# remove the .html extension from request URI
rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
root /var/www/mywebsite.com;
index index.html;
# redirect rules for homepage '/', removes 'index' from URI
include /etc/nginx/sites-available/conf/redirects-homepage-template.conf;
# rule for any URI content. Searches for static file with .html extension or exact URI content file name. If not found, returns 404.
location / {
try_files $uri.html $uri =404;
}
}
redirects-homepage-template.conf content
# Rules for handling homepage redirects
# If URI equals '/' then find index.html static page
location = / {
try_files /index.html $uri =404;
}
# After rewrite homepage URI equals '/index', it rewrites it to '/'
location = /index {
rewrite /index / permanent;
}
The website is meant to serve only static content and it's suppose to work as fast as possible. I wonder about the consequences of my solution and if there's any better way of removing the silly "/index" from homepage.
Haven't configured domain yet, using bare ip address for testing purpose.

nginx config with spa and subdirectory root

I always seem to have problems with nginx configurations. My SPA is located at /mnt/q/app (pushstate is enabled) and the frontend root is located at client/public. Everything should be mapped to index.html, where the app picks up the route and decides what to do.
Full path to the index is /mnt/q/app/client/public/index.html.
I think I ran out of options by now. No matter what I do, I just get a 404 back from nginx, I think the configuration is simple enought and have no clue what's wrong.
server {
listen 80;
server_name app.dev;
root /mnt/q/app;
location / {
root /client/public;
try_files $uri #rewrites =404;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
}
Any help is appreciated.
If nginx views the file system from the root, then the root should be set to /mnt/q/app/client/public, and not either of the two values you are using.
The last element of the try_files directive can be a default action (e.g. /index.html), a named location or a response code. You have a named location in the penultimate element - which will be ignored.
Your named location should work, but is unnecessary, as try_files is capable of implementing it more simply. See this document for more.
For example:
root /mnt/q/app;
location / {
root /mnt/q/app/client/public;
try_files $uri $uri/ /index.html;
}
location /api {
}
location /auth {
}
The $uri/ element will add a trailing / to directories, so that the index directive can work - you do not have to add it if you do not need it.

Why nginx not able to serve sites in different locations?

I want to use nginx to host some static html which is located in different path on my pc, below is my configuration:
server {
listen 8080;
server_name localhost;
location /chatserver {
root /Users/xxxx/gitrepo/chatserver/public;
index index.html;
}
location /test {
root /Users/xxxx/test/test_site;
index index.html;
}
The file structure is:
Users
|-xxxx
|-gitrepo
| |-chatserver
| |-public
| |- index.html
|-test
|-test_site
|- index.html
But when I access: http://localhost:8080/chatserver or http://localhost:8080/test, nginx always responds 404 Not Found.
If I access: http://localhost:8080/, nginx will return the default nginx welcome page.
Why is my configuration not working?
I think you should use alias instead of root.
In your example the URL /chatserver/index.html will search the /Users/xxxx/gitrepo/chatserver/public/chatserver/index.html (note the "undesired" chatserver after public!). Check your nginx's logfiles!
See documentation of root and documentation of alias.

Resources