dylanh724
dylanh724

Reputation: 1018

Why does my Nginx alias redirect not work (Goes to 404)?

I have my main site at x.com (@ /var/www/x.com/index.html)

# MAIN LOCATION
location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    #autoindex on;
    proxy_set_header X-Real-IP $remote_addr;
}

I want /v2 to redirect to another local dir (@ /var/www/x.com/v2/public/index.html)

# Redirect beta site
location /v2 {
        alias    /var/www/x.com/v2/public;
}

This seems like it should work, but instead, it's going to my 404 site. Not sure if this matters, but here is my root above both of them:

# path
root    /var/www/throneoflies.com/html;

I tried ordering "/v2" both above "/" and below - didn't seem to make a difference. I read I shouldn't use 'root' instead of 'alias' because it's a different schema (/v2/public/ and not just /v2/).

EDIT: Seems like it should be working - I've read a lot since this post. Here is my full file:

server {
        # MAIN >>
        # SSL configuration
        listen                  443 default_server ssl;
        server_name             www.x.com;
        error_page 404          /error/404.html;
        server_tokens           off;

        # SSL
        ssl_certificate         /etc/ssl/x.com/cert.pem;
        ssl_certificate_key     /etc/ssl/x.com/key.pem;
        ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        #ssl_session_tickets    off;
        ssl_ciphers             EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        ssl_prefer_server_ciphers on;

        # path
        root                    /var/www/x.com/html;
        #root                   /var/www/x.com/v2/public; #works!

        # Add index.php to the list if you are using PHP
        #index index.php index.html index.htm index.nginx-debian.html;
        index                   index.html;

        # 404 handling - no cache
        location = /404.html {
                add_header      Cache-Control "no-cache" always;
        }

        # Redirect beta site : Why doesn't it work if the outer block root changed to the same path works? I can REPLACE my "/" with this v2 path np. However, alias at /v2 does not work.
        location = /v2 {
                alias           /var/www/x.com/v2/public;
                #root           /var/www/x.com/v2/public;
                #try_files      $uri $uri/ =404;
        }

        # MAIN LOCATION
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files       $uri $uri/ =404;
                #autoindex      on;
                proxy_set_header X-Real-IP $remote_addr;

        # DENY .HTACCESS ACCESS
        location ~/\.ht {
                deny all;
        }
}

Upvotes: 2

Views: 4179

Answers (1)

cnst
cnst

Reputation: 27258

This should obviously be caused by the = in your location = /v2, which will only match requests where the $uri is exactly /v2; the correct way would most likely be to remove =.

Additionally, note that if using location /v2 {alias /var/www/x.com/v2/public;}, then folks would also be able to access things like /var/www/x.com/v2/public.bak through /v2.bak; if that's not intentional, then you would be better off having trailing slashes, as appropriate.

Upvotes: 1

Related Questions