2

I have a web app (boilerplated with VueJS CLI) running on nginx.

All requests to this app (except /api) are handled by root index.html (since it's using frontend-side routing). I want add another exception to this rule, so that when we go to /register it redirects us to some other static public html file (not related with this VueJS app, but sitting under the same project/directory).

In other words, I want to redirect /register to /public/xyz/index.html but without changing the URL displayed in the browser address bar.

I tried to add location = /register rule but it doesn't seem to work, on /register it still keeps redirecting me to /app/dist/index.html.

My 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;
        }
}

5

You should use an alias instead, to substitute directly for the path you want.

location = /register {
    alias /public/xyz/index.html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.