skip to Main Content

I have a some case to map in nginx

  1. for / it take from the /var/www/html/cont
  2. for /content/* it take form the var/www/html/cont
  3. if it is not / and not /content/*
    the it should take from /var/www/html/web

i am stucking in case 3

Here is my config

    location / {
                 root /var/www/html/cont;
                 if (!-e $request_filename){
                     rewrite ^(.*)$ /index.html break;
                 }
                 # try_files $uri $uri/ =404;
             }
   location /content {
                 root /var/www/html/cont;
                 if (!-e $request_filename){
                     rewrite ^(.*)$ /index.html break;
                 }
                 # try_files $uri $uri/ =404;
             }

Any help appreciated.
Thank you

2

Answers


  1. Chosen as BEST ANSWER

    Here is my solution which works perfectly in as per my use case.

        location / {
                     if ($request_uri = "/") {
                         set $custom_root /var/www/html/cont;
                     }
        
                     if ($request_uri != "/") {
                         set $custom_root /var/www/html/web;
                     }
        
                     root $custom_root;
        
                     if (!-e $request_filename) {
                         rewrite ^(.*)$ /index.html break;
                     }
                     # try_files $uri $uri/ =404;
                 }
    
    location /content {
                root /var/www/html/cont;
                if (!-e $request_filename) {
                     rewrite ^(.*)$ /index.html break;
                 }
        }
    

  2. Set root for server {} context as root /var/www/html/web;

    then

    location / {
        root /var/www/html/cont;
        # your other directives
    }
    
    location /content {
        root /var/www/html/cont;
        # your other directives
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search