nginx重写 /重定向 /别名通配符全卡

发布于 2025-01-25 08:23:52 字数 4369 浏览 3 评论 0原文

我找到了几种不同的方法来重定向nginx中的路径。问题是我有一个子模块,其中包含/foo/带有/foo/foo/wisdom/script.js.js之类的subdirs的路径。

我来的最接近的是: 重写 ^(/foo /)(.** fory/bar/$ 2永久; 为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/$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;
        }
   }
}

编辑2: 这也有效:

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

但是,在检查错误日志时,我可以看到Nginx在/var/var/www/bar /...而不是http:// http:// localhost // bar /... https:// api:5000/foo/...在我上面的配置中使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-02-01 08:23:52

因此,重写是正确的解决方案。我的代理不正确。解决方案如下。

重写:

rewrite ^(/foo/)(.*)$ /bar/$2 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;
        }

在位置标签后记下^〜

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文