NGINX 重写/重定向/别名通配符用于完整路径

NGINX rewrite / redirect / alias wildcard for full path

我发现了几种在 NGINX 中重定向路径的不同方法。问题是我有一个子模块,其中包含 /foo/ 的路径以及 /foo/wisdom/script.js.

等子目录

我最接近的是: rewrite ^(/foo/)(.*)$ /bar/ permanent; 对于 js 和 css 文件抛出 404。

显然解决方法是改用别名,我发现了这个:

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

但是通配符没有正确匹配,我不确定如何修复它。

编辑: 添加 nginx.conf 以获取更多信息:

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/ permanent;
        ## THIS DOESN'T MATCH
        #location ~* ^(/foo/)(.*)$ {
        #    alias /bar/;
        #}
        ## THIS DOESN'T MATCH
        #location ~* ^/foo/.*\.(jpg|png|css|js|appcache|xml|ogg|m4a)${
        #     alias /bar/;
        #}
        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 /;
            }
            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/.;
        }
        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;
        }
   }
}

编辑 2: 这也部分有效:

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

然而,当检查错误日志时,我可以看到 nginx 正在 /var/www/bar/... 中寻找 css 和 js 文件,而不是 http://localhost/bar/... 在我上面的配置中代理 https://api:5000/foo/... .

所以重写是正确的解决方案。我的代理不正确。解决方法如下

重写:

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

代理服务器:

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;
        }

记下位置标记后的 ^~