将 NGINX 路径重新路由到另一个路径(没有 http 重定向)
Reroute NGINX path to another path (without http redirect)
我希望能够访问下面的这些 URL
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/;
}
}
您只需要:
server {
set $rootPath /var/www/vhosts/mysite/public;
listen 80;
server_name assets.mydomain.com;
root $rootPath;
location /v999/ {
root $rootPath;
}
...
}
我希望能够访问下面的这些 URL
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/;
}
}
您只需要:
server {
set $rootPath /var/www/vhosts/mysite/public;
listen 80;
server_name assets.mydomain.com;
root $rootPath;
location /v999/ {
root $rootPath;
}
...
}