简体   繁体   中英

Nginx URL Rewrite Processing

I have a nginx container serving static assets and if it receives a request for

https://mysite:8090/static/2.1.1/test/mystyle.css (where 2.1.1 could be any semantic version)

Nginx should serve the static assets under /usr/share/nginx/html/test/mystyle.css

I tried the following config but it did not work:

http {
    include    mime.types;
    sendfile on;
    server {
        server_name localhost default";
        rewrite ^.*/static/[^/]+/(.*)$ $1;
        location / {
          root /usr/share/nginx/html/;
        }
    }
}

Looks like I was making this more complicated than need be, didn't realize the initial slash gets dropped.

This worked for me:

rewrite ^/static/[^/]+/(.*)$ /$1 break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM