Home > Software engineering >  NGINX rewrite / redirect / alias wildcard for full path
NGINX rewrite / redirect / alias wildcard for full path

Time:05-03

I've found a few different ways to redirect a path in NGINX. The issue is I have a submodule that contains paths to /foo/ with subdirs like /foo/wisdom/script.js.

The closest I've come is: rewrite ^(/foo/)(.*)$ /bar/$2 permanent; Which throws 404 for js and css files.

Apparently the fix is to use and alias instead, I found this:

location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)$ {
    alias /bar/;
}

However the wildcard doesn't match properly and I'm not sure how to fix it.

EDIT: Adding nginx.conf for more info:

worker_processes    auto;
pid                 /run/nginx.pid;
error_log           /dev/stdout info;

events {
    worker_connections 1024;
}

http {
   include       mime.types;
   default_type  application/octet-stream;
   
   server {
        listen              80;
        server_name         localhost;
        keepalive_timeout   70;

        access_log          /dev/stdout;
        error_page          404    /404.php;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        server_tokens off;

        if ($request_method !~ ^(GET|POST|PUT|DELETE)$ ) {
            return 405; 
        }
        ## THIS WORKS BUT NESTED JS AND CSS RETURN 404
        #rewrite ^(/foo/)(.*)$ /bar/$2 permanent;
        ## THIS DOESN'T MATCH
        #location ~* ^(/foo/)(.*)$ {
        #    alias /bar/$2;
        #}
        ## THIS DOESN'T MATCH
        #location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)${
        #     alias /bar/$1;
        #}
        location /api/query1 {
            proxy_pass http://api:1234/api/query1;
        }
        location /api/query2 {
            proxy_pass http://api:1234/api/query2;
        }
        location /api/query3 {
            proxy_pass http://api:1234/api/query3;
        }
        location /api/query4 {
            proxy_pass http://api:1234/api/query4;
        }
        location /api/query5 {
            proxy_pass http://api:1234/api/query5;
        }
        location /api/query6 {
            proxy_pass http://api:1234/api/query6;
        }
        location /api/query7 {
            proxy_pass http://api:1234/api/query7;
        }
        location /api/query8 {
            proxy_pass http://api:1234/api/query8;
        }
        location /api/query9 {
            proxy_pass http://api:1234/api/query9;
        }
        location /api/query10 {
            proxy_pass http://api:1234/api/query10;
        }
        location /api/query11 {
            proxy_pass http://api:1234/api/query11;
        } 
        location /bar {
            proxy_pass http://api:5000/foo;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 43200000;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            expires       30d;
            tcp_nodelay   off;
            access_log    off;
        }

        location / {
            if ($request_uri ~ ^/(.*)\.php) {
                return 302 /$1;
            }
            autoindex   off;
            index       index.php;
            root        /var/www;
            try_files $uri $uri.html $uri.php $uri/ =404;
            #rewrite ^/assets/([a-z\-] )-([a-z0-9] ).(css|js|png|webp) /assets/$1.$3;
        }
        location ~* \.php$ {
                fastcgi_pass    127.0.0.1:9000;
                include         fastcgi_params;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
                root /var/www;
        }
        location ~ \.css {
            root        /var/www;
            add_header  Content-Type    text/css;
        }
        location ~ \.js {
            root        /var/www;
            add_header  Content-Type    application/x-javascript;
        }
        location = /404.php {
            root        /var/www;
        }
   }
}

EDIT 2: This also partially works:

location ~ ^/foo/(.*) {
    return 301 /bar/$1;
}

However when checking error logs I can see nginx is looking for css and js files in /var/www/bar/... instead of http://localhost/bar/... which is https://api:5000/foo/... proxied in my config above.

CodePudding user response:

When you are using an alias directive in regex locations, you need to specify the full physical path to the matched file, e.g.

location ~* ^/foo/(.*\.(?:jpg|png|css|js|appcache|xml|ogg|m4a))$ {
    alias /foo/bar/$1;
}

CodePudding user response:

So the rewrite was the correct solution. My proxy was incorrect. Solution as follows.

Rewrite:

rewrite ^(/foo/)(.*)$ /bar/$2 permanent;

Proxy:

location ^~ /bar {
            proxy_pass http://api:5000/foo;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 43200000;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
        }

Take note of the ^~ after the location tag.

  • Related