CodeWeed
CodeWeed

Reputation: 1071

nginx redirection not working inside location directive

I have two location directives both containing conditional redirects.

server {
        listen 443 ssl;
        ssl_certificate /etc/ssl/cert.pem;
        ssl_certificate_key /etc/ssl/cert.key;
        server_name services.gixxx.de;
        
        location / {
            if (-f $document_root/maintenance.on) {
                return 503;
            }
            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri $uri/ /index.html =404;
            proxy_set_header Host $host;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
        }

        location /api {
            if (-f $document_root/maintenance.on) {
                return 503;
            }
            proxy_read_timeout 120;
            proxy_connect_timeout 120;
            proxy_send_timeout 120;
            proxy_pass http://serviceapp;
        }
        upstream serviceapp {
            server serviceapp:3000;
        }

When I create a document name maintenance.on on the route folder it works for the first location / { directive but not for location /api { part. What is going wrong here.

Upvotes: 1

Views: 49

Answers (1)

Richard Smith
Richard Smith

Reputation: 49792

You have no root defined for the second block. You should move the root statement into the outer block and allow it to be inherited by both location blocks.

Upvotes: 1

Related Questions