重新路由到另一条路径的NGINX路径(没有http重定向)

时间:2015-04-25 02:32:32

标签: nginx

我希望能够访问以下这些网址

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

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

在NGINX中,上面的网址应该指向

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

我该怎么做?我原以为这只是一个做别名的情况,但似乎并非如此

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

但这似乎不起作用。下面是我的完整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/;
    }
}

1 个答案:

答案 0 :(得分:1)

您所需要的只是:

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

    listen 80;

    server_name assets.mydomain.com;

    root $rootPath;

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

} 

请参阅location on nginx docs

相关问题