-1

I was originally trying to do a complicated URL rewrite which I couldn't get working, so stripped it back to a simple URL redirect which should definitely work, instead it throws a 404.

Requests for [any_scheme]://www.mydomain.com/google should redirect to https://www.google.com/

nginx.conf (not posted as it contains no server blocks so can't conflict?)

mydomain.com.conf:

server {
    server_name www.mydomain.com;
    root /home/mydomain/public_html;
    index index.php index.html index.htm;

    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /home/mydomain/ssl.combined;
    ssl_certificate_key /home/mydomain/ssl.key;

    access_log /var/log/virtualmin/mydomain.com_access_log;
    error_log /var/log/virtualmin/mydomain.com_error_log;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location = /google {
        return 302 https://www.google.com/;
    }

    #rogue .htaccess files caught here
    location ~ /\.ht {
        deny all;
    }
}

#force non-www. to www.
server {
    server_name mydomain.com;
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    return 301 $scheme://www.mydomain.com$request_uri;
}
Jon
  • 452
  • 4
  • 23

2 Answers2

0

Based on your log entry: you are requesting /google/ (note the trailing slash), but this will not match up with /google location because there is no trailing slash there.

So you need two exact match locations:

location = /google {
    return 302 https://www.google.com/;
}

location = /google/ {
    return 302 https://www.google.com/;
}
Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
  • Sorry, my log file line was incorrect, I had been testing with and without slash and posted the wrong line. Even with the slash in the rule and testing slash in URL I get same response. Is there another way I can trace the problem? – Jon Mar 06 '19 at 11:44
0
  • Use curl -v to debug what's going on with your server.

  • Do you use sites-available / sites-enabled? Don't. See https://serverfault.com/a/870709/110020. The sites-available / sites-enabled is non-standard and is evil, don't use it, use conf.d instead.

  • Make sure you actually reload nginx. See https://stackoverflow.com/a/21297545/1122270. Try sudo nginx -s reload and sudo pkill -HUP nginx directly.

  • Check both error_log as well as access_log. Make sure the dates of events match the new requests that you're making. I'd recommend opening several terminal windows, and doing a tail -f in each, then doing a few return presses to separate new input from the old one, only then doing the requests.

  • Are you using any extra proxies on top of nginx? Those might be the cause of the problem.

cnst
  • 25,870
  • 6
  • 90
  • 122
  • Thanks, I *think* `sudo nginx -s reload` did the trick. I have been using `systemctl reload nginx` which I believe is basically the same thing, so not sure *why* it worked this time but it did. I will also take your advice on `sites-enabled`. For the record I'm using a Digital Ocean image of Ubuntu 16.04 LEMP. Thanks again. – Jon Mar 14 '19 at 16:02