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/;
}
Related
creating a website for my self and need to host projects.
Basically, i hhave different projects with different framework. ie, Flask, Django, Node.JS and some html file projects. I would like to host them at projects.domain.com/<project name>
I tried to set server_name projects.domain.com/asdf but in error.log it says, server name "projects.domain.com/asdf" has suspicious symbols
Next up, i tried to nest location blocks (which i presume isn't how its supposed to be)
location /asdf {
location /static/ {
root blah blah;
}
location / {
..
proxy_pass http://localhost:3000 ;
}
}
But, this errors out saying location static is outside asdf
Some suggested to alias instead of root in the location /static/ block, but that doesnt work too.
Any help is appreciated :)
First of all a server_name can not contain URI segments. So a hostname or IP should be used as a value.
If you want to mix different local directories and proxy-locations a configuration could look like this.
Notice: Your location URI (/one, /two) will be appended to the root path.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not be part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
server {
server_name project.domain.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /one/ {
alias/var/www/html/project1/;
index index.html;
}
location /two/ {
alias/var/www/html/project2/;
index index.html;
}
}
I have a static site setup as follows:
location /cool {
alias /var/www/cool/build;
index index.html;
}
This serves the index.html file, but all the files that index.html references fail because they're relative to /. e.g., it's trying to load mydomain.com/styles.css rather than mydomain.com/cool/styles.css.
Rather than rewriting every link on the whole site to not have a preceding /, is there any way I can make nginx treat these links to be relative to /cool?
define root path before location block. Root path will be path of cool folder. By defining this, for every request it will check in cool directory by default.
root /var/www/cool;
location / {
alias /var/www/cool/build;
index index.html;
}
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...
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)
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.