0

I am trying to create a single Nginx config for multiple hosts based on a directory. I followed a guide which seems to work well with standard HTTP setup but when I add the HTTPS 301 redirect, I can an error "invalid redirect". Any ideas on this? Below is my config. Tx

server {
  listen x.x.x.x:80;

server_name ~^(?<sname>.+?).domain.com$;

return 301 https://$server_name$request_uri;

}

server { listen x.x.x.x:443 ssl default_server; server_name ~^(?<sname>.+?).domain.com$;

root /var/web/$sname;

index index.html index.htm index.php;

charset utf-8;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }


ssl_certificate /etc/letsencrypt/live/wildcard.domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/wildcard.domain.com/privkey.pem;

access_log /var/log/nginx/$sname-access.log; error_log /var/log/nginx/wildcard-error.log debug;

error_page 404 /index.php;

sendfile off;

    location ~ \.php {
            include fastcgi.conf;
            #fastcgi_index index.php;
            include cors_support;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    }
    location ~ /\.ht {
            deny all;
    }

location /.well-known { root /var/www/html; } }

  • 1
    In my Personal mind would it be more Sufficient and easier to manage, to not have a single file Installation, instead I wrote my pages in each file which only contains the same domain, and I mean I read already that is maybe nearly impossible what you are trying, but however i am unsure about this part but oyu dont tell any logs – djdomi Jun 28 '21 at 04:38
  • What is the output of curl -v http://test.domain.com? – Tero Kilkanen Jun 28 '21 at 07:33

1 Answers1

0

Your redirect is "invalid" because you are trying to redirect to https://~^(?<sname>.+?).domain.com$ which is pretty obviously invalid.

Why is this so?

You chose to write your redirect as follows:

  return 301 https://$server_name$request_uri;

This doesn't make sense. The server_name is not a valid hostname.

Instead, you should redirect to the same host that the user agent used. The correct variable to use there is $host, not $server_name.

  return 301 https://$host$request_uri;
Michael Hampton
  • 247,473
  • Michael, you are Stalking ME :-) However, and also a Good Solutin additonal might be: https://serverfault.com/questions/706438/what-is-the-difference-between-nginx-variables-host-http-host-and-server-na?noredirect=1&lq=1 – djdomi Jun 28 '21 at 14:42
  • 1
    @djdomi That is the link in the answer! – Michael Hampton Jun 28 '21 at 14:44
  • point to you didn't see that there was a link to the same site ;) and if I get an uovote on time each we meet us in the same topic I would very fast upto 500 points xD – djdomi Jun 28 '21 at 17:05
  • Thanks the solution @MichaelHampton worked. What does $host actually equate to in this scenario? – WallyKaye Jun 30 '21 at 06:46
  • @WallyKaye That's explained in the linked post. – Michael Hampton Jun 30 '21 at 10:03