我有一个在 nginx 上运行的 Web 应用程序(使用 VueJS CLI 进行样板化)。
对此应用程序的所有请求(除 外/api
)均由 root 处理index.html
(因为它使用前端路由)。我想为该规则添加另一个例外,这样当我们转到时,/register
它会将我们重定向到其他静态公共 html 文件(与此 VueJS 应用程序无关,但位于同一项目/目录下)。
换句话说,我想重定向/register
到/public/xyz/index.html
但不更改浏览器地址栏中显示的 URL。
我尝试添加location = /register
规则,但似乎不起作用,/register
它仍然将我重定向到/app/dist/index.html
。
我的nginx.conf:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /app/dist;
index index.html;
server_name localhost;
location = /register {
root /public/xyz;
index index.html
}
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control no-cache;
expires 0;
}
location ~ ^/api/(.*) {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass_request_headers on;
proxy_pass <ENV_API_URL>/$1$is_args$args;
}
}
答案1
您应该使用alias
来直接替换您想要的路径。
location = /register {
alias /public/xyz/index.html;
}