1

I'm very new to NGINX and the whole configuration, so I managed to setup a server with the following config with the help from a friend:

    server {
        listen 80;
        listen [::]:80;
        server_name domain.com;
        rewrite         ^       https://$server_name$request_uri? permanent;
}

server {
        listen 80;
        listen [::]:80;
        server_name domain.de;
        rewrite         ^       https://$server_name$request_uri? permanent;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name domain.com domain.de;
        root /var/www/html;
        index index.php index.html index.htm;

there are multiple servers for my main page, some project pages, and some private pages. I have for every single TLD an own server because I don't want to have the TLD in the address bar changed on a redirect. Now my answer is, how can I simply get all these non-SSL Servers to one big Server(1/project) without redirecting to just one TLD?

Example:

Project.com, Project.de, Project.net and Project.org are redirecting over 5 of these non-SSL Servers to one SSL-enabled Server. When i'm going to http://project.net i'm beeing redirected to https://project.net. The TLD doesnt change at all, because of the single non-SSL Server setup.

What i want to achieve, that i can minify my config file to have one non-SSL Server per Project redirecting to https without changing the domain.

                                       :80 Server      :443 Server

http domain.net ---> https domain.net ([non-SSL 01] ---> [SSL 01]) http domain.com ---> https domain.com ([non-SSL 02] ---> [SSL 01]) http domain.de ---> https domain.de ([non-SSL 03] ---> [SSL 01])

http project.de ---> https project.de ([non-SSL 04] ---> [SSL 02]) http project.com ---> https project.com ([non-SSL 05] ---> [SSL 02]) and so on

CentrixDE
  • 113
  • 1
  • 1
  • 9

3 Answers3

3

What I understand is that you want to redirect multiple domains to one domain.

First, I suggest you use return 301 instead of rewrite. It is more efficient in this case.

This is the default server config. It will be served if the request does not match any of the virtual host.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://your-correct-domain.com$request_uri;
}

Also setup the a server to receive HTTP traffic for your-correct-domain.com and redirect it to HTTPS.

server {
    listen 80;
    listen [::]:80;
    server_name your-correct-domain.com;
    return 301 https://your-correct-domain.com$request_uri;
}

Then finally, the HTTPS server of your-correct-domain.com where you have all your config.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name your-correct-domain.com;
    root /var/www/html;
    index index.php index.html index.htm;
}

Note that this setup will serve the default server if someone accesses the IP directly. For example, your server IP is 192.168.100.100, if someone enters that in their browser, they will be redirected to https://your-correct-domain.com.

The default server will catch all request that does not have a server/virtual host prepared for it. So if someone requests for domain.de, my.domain.de, site2.domain-something.com, they will all be permanently redirected to https://your-correct-domain.com. You also have the option to redirect with 302 instead of 301.

Final note, if you have other virtual host that is not part of this setup you are making, it will not be a problem. The default server will not be served for that because Nginx will check first if a virtual host exist and serve that first.

jarvis
  • 2,006
  • 4
  • 18
  • 31
  • Oh I'm sorry, i don't want to redirect every domain just to one single domain. I have about 10 domains redirecting on one single server, it's redirecting every single domain to the main-ssl-enabled-server. I want to use every domain with different TLDs without changing the tld in the address bar. I currently have the problem, that i have for every tld a single server which is redirecting to the ssl-server but instead i want to know if there is any way to just have one server on port 80 and one on 443 where the domains doesn't change at all. – CentrixDE Apr 21 '17 at 00:34
  • If you have multiple websites and you do not want to change the domains at it appears in the address bar but do not want to make multiple virtual host, I suggest (1) you make use of the default server setup, just don't redirect, put all your config in the default server; or (2) indicate all the domains in one virtual host under server_name. You should have correct SSL if you want a catch all at 443. – jarvis Apr 21 '17 at 00:39
  • The thing is, that i have about 5 main servers for different projects. Every main server has about 10 sub-servers(each for a own tld as i mentioned in my question) I tried to use just a SSL enabled server but when i tried to connect with different browsers the most won't connect because it needs to be redirected(otherwise there will be an error for trying to use http on a SSL secured port) – CentrixDE Apr 21 '17 at 00:43
  • Good answer. Any domains explicitly specified in another server will work as expected. Any domains that aren't explicitly listed will be redirected. If you want to redirect SSL the easiest way is a single certificate with all the domains listed, then you can so similar for SSL. If you have certificates per domain then you'd need server blocks for each. – Tim Apr 21 '17 at 00:47
3

Updated answer corresponding to the clarified question.

This kind of redirect can be done as follows:

server {
    server_name _;
    listen 80 default_server;

    return 301 https://$server_name$request_uri;
}

This is a default server block, that matches to any domain name. We simply return a 301 rewrite response with the server name used in the request. This will do what your example shows.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • I don't want to redirect every TLDs to my .com domain, I want to let them unchanged. My setup works pretty well to this time, but I have a little concern when I'm going to setup new servers or changing some information, that I have to delete or edit 10+ server. I'm just searching for a simple way to combine al non-SSL Servers to one single non-SSL server where the redirect doesn't affect changing the domain in the adress bar. – CentrixDE Apr 21 '17 at 14:13
  • Your question is then quite unclear. Can you provide a set of example URLs, and what exactly should happen in each case? – Tero Kilkanen Apr 21 '17 at 14:15
  • yes, i gonna change the question a bit – CentrixDE Apr 21 '17 at 14:16
  • I've changed the question with a little explanation. – CentrixDE Apr 21 '17 at 14:23
  • I updated my answer. – Tero Kilkanen Apr 21 '17 at 15:00
1

Here's the correct answer!

server {
    server_name _;
    listen 80 default_server;
    return 301 https://$host$request_uri;
}

Don't use $server_name, use $host. You can choose to use 302 redirects if you'd like as well, because 301 are permanent and if you change something it may be harder to fix it because of caching across devices. Only use 301 if you don't already have a lot of http links that might change URL in the future to avoid multiple redirects.

Ryan Kopf
  • 111
  • 2
  • I'd argue this is indeed the correct response. If you have multiple server names `$server_name` will only refer to the first one (e.g., the redirect will potentially send the user to a different domain), where `$host` will preserve the host line in the http request through the redirect. – kalebo Feb 15 '23 at 16:49