2

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; } } 
6
  • 1
    The location /v2 block seems fine. Does /v2 redirect to /v2/ before the 404 response? Is there an index directive in the outer block? Commented Oct 4, 2017 at 13:13
  • I don't see a visible redirect, if that's what you mean. Index directive? Yea just index index.html; Commented Oct 4, 2017 at 14:32
  • (It's a CAUGHT 404, not the ugly nginx 404 from outer error_page 404 /error/404.html;). Also, if I swap the main root with the alias I'm trying to do, the /v2/public/ site loads FINE! So it's not a path issue Commented Oct 4, 2017 at 14:37
  • @RichardSmith I added my full file above Commented Oct 4, 2017 at 15:43
  • 2
    Your EDIT shows location = /v2 which is not the same as your original question (and will definitely not work) Commented Oct 4, 2017 at 15:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, this first line was already suggested by Richard Oct 4th - since the suggestion, I tried it and edited (+commented below Richard): It seemed to be 1 of the issues, but the issue is still not resolved. Looking into your 2nd suggestion now. Interesting, I didn't think trailing slashes were important! Does the root not having a trailing slash mean anything? Or the root doesn't matter at all since we're doing alias?
As an update, I tried trailing slash and same. Curious, I made an alias to the exact same location as my main site that works -- and that doesn't work, either! Something seems seriously wrong with alias. Perhaps something outside of this block? Another clue is that if I set the outside block root to the /v2/public/ it WORKS -- if I put the root inside, it doesn't work. No aliases seem to work, and root doesn't seem to update if I put it again on an inside block.
@DylanHunt can you show how you are testing this? curl -v will be really helpful also, if you could please paste some error.log lines

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.