Setup nginx to serve static html web pages based on url - nginx

Id like to serve to different websites based on url user enter.
For example if user will go to
my-domain.pl/ the content will be served from desktop folder
my-domain.pl/desktop the content will be served from desktop folder
my-domain.pl/mobile the content will be served from mobile folder
root
|
|---mobile
| |--- assets
| |---js,css,img
|
|---desktop
|--- assets
|---js,css,img
I tried that in nginx setup file:
server {
root /desktop
location /mobile {
root /mobile
}
location /desktop{
root /desktop
}
}
but it works only for / path, remaining paths return 404
I tried to add try_files $uri index.html but it seems that it returns index.html files for all request for this location e.g. it returns index.html file instead javascript too.
I am absolutely new in setting up nginx so any help will be appreciated.

You need to make use of the alias directive instead of root: see documentation.
server {
root /;
location /desktop {
}
location /mobile {
alias /mobile;
}
}
(don't forget trailing semicolons)

You should avoid specifying root inside location blocks (cf. nginx pitfalls).
Have you tried the following:
have only one root
use rewrite to serve /desktop by default
The config would look like:
server {
root /;
## this should take care of the redirect to /desktop by default:
location = / {
rewrite ^ /desktop/ redirect;
}
## this one below probably doesn't work:
# rewrite ^/$ /desktop/;
}
P.S. I have no access to a machine with nginx right now so I'm not able to check the rewrite syntax. See also this answer.

Related

nginx serve index file from subfolders

I would like to serve an index file from a root folder and one from a subfolder from root.
My nginx server conf looks as follows:
server {
listen 80;
server_name localhost;
root /data/;
index index.html index.htm index.txt;
location / {
}
location /a {
alias /data/a/;
}
}
and my directory structure looks as follows:
/data/:
a index.txt
/data/a:
index.txt
If I then do curl localhost, I get the contents of the file /data/index.txt.
But curl /localhost/a gives me a 301.
curl localhost/a/index.txt works.
Why can't I access my index.txt with curl /localhost/a ?
I tried using a root instead of alias in the location /a block and also tried to specify the index.txt for location /a, but no success.
I see similar posts, e.g.
Nginx location configuration (subfolders)
but couldn't yet find the answer.
The index directive works with URIs that end with a /:
So the URI / gives you the contents of /index.txt and /a/ gives you the contents of `/a/index.txt.
If you provide Nginx with a URI of a directory (but without a trailing /), the default behaviour is to redirect to the same URI, but with a trailing /.
This is just how the index directive works. See this document for details.
If you want something other than default behaviour you will have to do it manually using try_files. See this document for details.
For example, to return the contents of an index.txt file by providing the URI of the directory without a trailing /, use:
root /data;
location / {
try_files $uri $uri/index.txt =404;
}
Note that the location /a { ... } block is not necessary in either this example, or the example in your question.

nginx how to serve pictures or media files?

I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.
So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html;
server_name 159.89.141.121;
location / {
try_files $uri $uri/ =404;
}
location /media/ {
root /var/www/example.com/media;
}
}
Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?
Well I decided to read the documentation and realized that the location block adds to the root directory specified..
So the pathing of
`location /media/ {
root /var/www/example.com/media;
}`
would end up routing example.com/media/testpic.jpg to /var/www/example.com/media/media/testpic.jpg
I've changed the location block to look like this
location /images/ {
root /var/www/example.com/media;
}
and it will now route example.com/images/testpic.jpg to /var/www/example.com/media/images/testpic.jpg
I'm not sure why it didn't work when I tried the alias directive, though...

nginx serving static files from root and uploaded files from alias

I run nginx as a reverse proxy server in front of apache.
I need to access uploaded files from frontend in backend so the way to go is to use an alias in nginx site config but static files in backend should be handled directly by nginx. I'm new to nginx so here is my partial config that handles static files. I also specified an alias (/images) but it will not work because it is overwritten by second condition.
How can the two conditions be combined so that nginx handles static files from root (backend app) and uploaded files from frontend app.
In apache config I included an alias for this problem and it works but without nginx in front.
Here is my partial nginx config:
server {
listen 80;
root /var/www/website/backend/www;
# Add index.php to the list if you are using PHP
index index.html index.php index.htm;
server_name admin.website.com www.admin.website.com;
location / {
proxy_pass http://localhost:8080;
include /etc/nginx/proxy_params;
}
#The alias to handle uploaded files(.jpeg, .pdf) from frontend
location /images {
alias /var/www/website/frontend/www/images;
}
#let nginx handle static files from root
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
expires 30d;
}
.
.
.
}
The regular expression location block takes precedence over a prefix location block (unless the ^~ modifier is used). See this document for details.
Try:
location ^~ /images {
root /var/www/website/frontend/www;
}
Note that the root directive is preferred in this case (see this document for details)

Nginx root location

On our server we have the /var/www/root/html/web/ directory that contains all the code, and a /var/www/root/html/web/front/ that contains our static frontend code.
Our frontend communicates with the code only via REST API calls, which have the /api/ prefix, so all the calls will be accessible via ourdomain.com/api/products/ , ourdomain.com/api/products/45 and so on. We also have an admin running there, on ourdomain.com/admin
When we want to see the actual frontend, we have to go to ourdomain.com/front in the browser, which is of course not what we want.
We have, among other stuff, this in our config:
root /var/www/html/web;
index index.php index.html;
location /front {
# some magic to make sure the /front folder will not be parsed
index nothing_will_match;
autoindex on;
}
However, what we wish is that if you go to ourdomain.com it will load /var/www/html/web/front/ folder as root, and if you go to ourdomain.com/api/* or ourdomain.com/admin/* it will load the /var/www/html/web/ as root. Is that possible?
NOTE: the /var/www/html/web/front/ folder can be moved somewhere else if needed, to /var/www/html/front/ for example
What you need is the alias directive (instead of root) to rewrite the URI:
root /var/www/html/web/front;
index index.php index.html;
location / {
}
location /api {
alias /var/www/html/web/;
}
location /admin {
alias /var/www/html/web/;
}

Serving static HTML files in Nginx without extension in url

root directory = /srv/myproject/xyz/main/
in the "main" folder I have few *.html files and I want all of them to point at a url say /test/ (which is quite different from the directory structure)
this is my very basic nginx configuration
server {
listen 80;
error_log /var/log/testc.error.log;
location /test/ {
root /srv/myproject/xyz/main/;
#alias /srv/myproject/xyz/main/;
default_type "text/html";
try_files $uri.html ;
}
}
If I use simple alias
location /test/ {
alias /srv/myproject/xyz/main/;
}
then its work perfectly, I mean I can access those html files by http://www.myurl.com/test/firstfile.html and so on
but I dont want that html extension.
I tried to follow these threads but no success
http://forum.nginx.org/read.php?11,201491,201494
How to remove both .php and .html extensions from url using NGINX?
how to serve html files in nginx without showing the extension in this alias setup
Try this
location ~ ^/test/(.*)$ {
alias /srv/myproject/xyz/main/;
try_files $1.html =404;
}

Resources