Score:0

Auto Virtual Host - Single Nginx config

cn flag

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;
  }
}
djdomi avatar
za flag
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
us flag
What is the output of `curl -v http://test.domain.com`?
Score:0
cz flag

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;
djdomi avatar
za flag
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
Michael Hampton avatar
cz flag
@djdomi That is the link in the answer!
djdomi avatar
za flag
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
WallyKaye avatar
cn flag
Thanks the solution @MichaelHampton worked. What does $host actually equate to in this scenario?
Michael Hampton avatar
cz flag
@WallyKaye That's explained in the linked post.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.