I would like to nginx to serve a static file from website root ( : http://localhost:8080/ ) but it serves my proxy pass; it serves "/" rule instead of "= /".
Here is what my nginx config look like :
listen 0.0.0.0:8080;
server_name localhost;
set $static_dir /path/to/static/
location = / {
# got index.html in /path/to/static/html/index.html
root $static_dir/html/;
}
location / {
# ...
proxy_pass http://app_cluster_1/;
}
Did i miss something ?
Use this one:
location = / {
index index.html;
}
location = /index.html {
root /your/root/here;
}
Related
My website uses many subdomains. What I need is to root requests to each folder depending of subdomain:
src.mydomain.com to /public
api.mydomain.com to /public
Anyother subdomain xxx.mydomain.com to /dist
I tried this settings without success:
server {
listen 8080;
listen [::]:8080;
server_name ~^(?<subdomain>.+)\.mydomain\.com$;
set $folder "dist";
if ($subdomain = "src"){
set $folder "public";
}
if ($subdomain = "api"){
set $folder "public";
}
root "/home/site/wwwroot/$folder";
index index.php index.html;
location / {
index index.php index.html;
}
}
Try this:
map $http_host $webroot {
src.mydomain.com /home/site/wwwroot/public;
api.mydomain.com /home/site/wwwroot/public;
default /home/site/wwwroot/dist;
}
server {
server_name *.mydomain.com;
root $webroot;
...
}
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I would like to configure nginx for different locations on / and I have the following config.
server {
listen 80;
server_name bla.com;
location = / { // on exact route match such as 'bla.com'
root /usr/share/nginx/html/folder; // it has index.html
}
location / { // rest all url such as bla.com/* are captured here eg. bla.com/temp/12
root /usr/share/nginx/html/dist; // different folder
}
}
How can I do this?
I also tried the below config with no luck -
server {
listen 80;
server_name bla.com;
root /usr/share/nginx/html;
location = / {
try_files $uri /folder/index.html; // it has index.html
}
location / { // rest all url such as bla.com/* are captured here eg. bla.com/temp/12
try_files $uri /dist/index.html; // different folder
}
}
You can achieve this by creating something like this:
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /var/www/website1/public_html;
}
location /temp {
root /var/www/website2/public_html;
}
}
You can also do this with individual files like this:
location /robots.txt {
alias /var/www/website/public_html/robots.txt;
}
This is how my configuration looks like...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = / {
index index.html;
}
location / {
root /etc/nginx/html/app1;
}
}
In my folder app1 I have two files, index.html & home.html
If I browse http://localhost/ or http://localhost/index.html or http://localhost/home.html page it comes up well.
When I change the configuration like so...
server {
listen 80;
server_name 127.0.0.1 localhost;
location = / {
index home.html;
}
location / {
root /etc/nginx/html/app1;
}
}
http://localhost:8888/index.html > works
http://localhost:8888/home.html > works
http://localhost:8888/ > 403 forbidden!!!
Can someone please tell me what is wrong?
Because the priority of location = {} is higher location / {}.
So it first matches the location = {}.
In you case, there isn't root in the location = {}.