skip to Main Content

Hello I am trying to upload static html files in Centos. So far I created subdomain and set nginx to open specific static html when It gets hit by browser. So far so good, problem is when I try to navigate from that static html to another html with relative paths say
Go elsewhere
it does not work. I get redirected to my whateveraddress/differentFolder/index.html but nginx says 404 Not Found. Is there a way to be able to redirect to another pages within the html files or I have to register every route in nginx?

nginx config:
server {
listen        80;
server_name   domain.tk www.domain.tk;
    root /home/apps/html/one;
    location / {
    index index.html;
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution after reading nginx documentation, it was to construct nginx.config like so:

    server {
    listen        80;
    server_name   mydomain.tk www.mydomain.tk;
        root /home/apps/html;
        location /one/ {index index.html;}
        location /two/ {index index.html;}
    }
    

    The thing is what if, I have 100 posible routes, this way I must specify all in the config?


  2. There are probably a few different ways to do this, but one option is to use the NGINX map directive, as the example below, adding additional URI locations to the map block and leaving the server block untouched.

    map $uri $index_uri {
      ~/one $1;
      ~/two $1;
    }
    
    server {
    listen        80;
    server_name   mydomain.tk www.mydomain.tk;
        root /home/apps/html;
        location /$index_uri {index index.html;}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search