asked in Software by (8.9k points)
0 like 0 dislike
23 views

Me need redirect all subdomains to main domain in Nginx. How I can to make redirect all subdomains to main domain in Nginx webserver?


nginx redirect all subdomains to main domain

1 Answer

0 like 0 dislike
answered by (8.9k points)

If you have multiple sites on same server and one of your main sites would be www.site.loc: Main domain:

server {
    listen 80;
    server_name www.site.loc;
    ...
}

For all sub domains, bads or misspelled can be redirected to www:

server {
    listen 80;
    server_name *.site.loc;
    return 301 http://www.site.loc$fastcgi_script_name;
}

Nginx will check first www.site.loc and if not matched all what ever other subdomains will be redirected to "www" one.


You can simply create a default server, that redirects everything to "http://site.loc" like this:

server {
  listen [::]:80 default_server;
  server_name localhost;
  return 301 http://site.loc$request_uri;
}

All incoming requests, that don’t find a matching server will be served by the default server, which will redirect them to the correct domain. This solution is not very convenient if you have multiple sites on the same server.


You can simply do it:

server {
    listen 80;
	server_name site.loc *.site.loc;
}

It simply serves the subdomain from the same configuration. When a URL is redirected, the URL changes. In this solution does not URL.

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related questions

0 like 0 dislike
1 answer 24 views
0 like 0 dislike
1 answer 17 views
0 like 0 dislike
1 answer 50 views
0 like 0 dislike
1 answer 3 views
0 like 0 dislike
1 answer 48 views
0 like 0 dislike
1 answer 67 views
0 like 0 dislike
1 answer 36 views
0 like 0 dislike
1 answer 45 views
...