简体   繁体   English

将NGINX路径重新路由到另一个路径(无需http重定向)

[英]Reroute NGINX path to another path (without http redirect)

I want to be able to go to these URLs below 我希望能够转到下面的这些URL

http://assets.mydomain.com/static/style.css http://assets.mydomain.com/static/style.css

http://assets.mydomain.com/v999/static/style.css http://assets.mydomain.com/v999/static/style.css

And in NGINX, either urls above should instead point to 在NGINX中,上述任一网址应改为指向

/var/www/vhosts/mysite/public/static/style.css /var/www/vhosts/mysite/public/static/style.css

How can I do this? 我怎样才能做到这一点? I had thought it was simply a case of doing alias but it doesn't seem to be the case 我以为这只是做别名的情况,但事实并非如此

ie. 即。

location ~* ^/v(.*)/ {
    alias $rootPath;
}

But that doesn't seem to work. 但这似乎不起作用。 Below is my full nginx config. 以下是我完整的nginx配置。

server {
    set $rootPath /var/www/vhosts/mysite/public;

    listen 80;

    server_name assets.mydomain.com;

    root $rootPath;

    access_log /var/log/nginx/access_assets.log;
    error_log /var/log/nginx/error_assets.log debug;

    index index.html index.htm;

    # Any files matching these extensions are cached for a long time
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
        add_header Cache-Control public;
    }

    # Don't throw any errors for missing favicons and don't display them in the logs
    location /favicon.ico {
        log_not_found off;
        access_log off;
        error_log off;
    }

    # Deny these extensions
    location ~* \.(txt|php|less)$ {
        deny all;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~ /\. {
        deny all;
    }

    location ~* ^/v(.*)/ {
        alias $rootPath;
    }

    location / {
        try_files $uri $uri/;
    }
}

What you need is only: 您只需要:

server {
    set $rootPath /var/www/vhosts/mysite/public;

    listen 80;

    server_name assets.mydomain.com;

    root $rootPath;

    location /v999/ {
        root $rootPath;
    }
    ...

} 

See location on nginx docs 查看Nginx文档上的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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