0

I am trying to amend my nginx config to redirect from the web root ("/") to /dir-a and then if this returns a 404 redirect to /dir-b. I'm not entirely sure how or if this is possible though.

This is what I have so far...

server {
    listen   443 ssl;
    ssl on;
    ssl_certificate /xyz.crt;
    ssl_certificate_key /xyz.key;

    root /var/www/mysite;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    client_max_body_size 2G;
    client_body_buffer_size 128k;

error_page 404 =200 /dir-b;
    location = / {
            try_files $uri $uri/ /dir-a;
    }

    # Serve static assets
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {

            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 9;
            gzip_proxied any;
            gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
            gzip_buffers 16 8k;

            access_log off;
            expires max;
            add_header Cache-Control public;

            break;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            fastcgi_buffer_size 256k;
            fastcgi_buffers 8 256k;
            fastcgi_busy_buffers_size 256k;

            fastcgi_param HTTPS on;
            fastcgi_read_timeout 6000;
            fastcgi_param APPLICATION_ENV production;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
            deny all;
    }
}

But this returns the 404 page so I'm not sure where to go from here. I need it to try dir-b instead.

Tom
  • 12,776
  • 48
  • 145
  • 240

1 Answers1

0
error_page 404 /dir-b;    
location = / {
                rewrite "/" /dir-a permanent;
        }

but this will return 404 code, and you can change it error_page 404 =200 /dir-b;

location = / {
                rewrite "/" /dir-a permanent;
        }

this will return status code 200

You can read this topic for more info

Community
  • 1
  • 1
Alex Pol
  • 42
  • 5