Search code examples
nginxlocationserveroperator-precedence

NGINX server location precedence


I'm very new to NGINX server and I was wondering how to fix this location precedence because it doesn't work.

  • I want the server to look for /phpmyadmin/ if match then use its root else use the second location block.

    location ^/phpmyadmin/.*\.(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
    root /usr/share/phpmyadmin;
    }
    
    location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ {
    root /home/safeftp/www/public_html;
    expires 30d;
    }
    

Thank you very much in advance for your answers. Peter


Solution

  • You need the ~* (case-insensitive matching) syntax on the first location block. Also, it's best to get into the habit of wraping your regex in double or single-quotes just in case you have spaces, or other special characters.

    location ~* "^/phpmyadmin/.*\.(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$" {
        root /usr/share/phpmyadmin;
    }
    
    location ~* "\.(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$" {
        root /home/safeftp/www/public_html;
        expires 30d;
    }