10

I have a django web app which run on server with IP address xx.xxx.105.49 and domain as www.example1.com

Below is my nginx configuration

server {
  listen 80;
  server_name  example1.com  www.example1.com ;
 
  location / { 
    return 301    https://www.example1.com$request_uri;
  }
}

server {
    listen 443 ssl;
    server_name  example1.com  www.example1.com;

    ssl_certificate      /etc/ssl/company/company.com.chained.crt;
    ssl_certificate_key  /etc/ssl/company/www.company.com.key;
    ssl_session_timeout  20m;
    ssl_session_cache    shared:SSL:10m;  # ~ 40,000 sessions
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2; # SSLv2
#    ssl_ciphers          ALL:!aNull:!eNull:!SSLv2:!kEDH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+EXP:@STRENGTH;
    ssl_ciphers          HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    client_max_body_size 20M;

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_connect_timeout 300s;
        proxy_read_timeout 300s;
    }

  location /static/ {
    alias /home/apps/webapp/company/new_media/;
  }

  location /media/ {
    alias  /home/apps/webapp/company/media/;
  }
}

when i type www.example1.com or example1.comfrom browser it take me to https://www.example1.com as expected but, now I have configured another domain(example2.company.com) to route to the same server (xx.xxx.105.49) and the actual problem is

when ever i type https://example2.company.com(secure), server was serving me the webapp with same domain example2.company.com

But when i use http://example2.company.com, my server was redirecting to www.example1.com which was not i want, so how can make some changes to the above nginx config file in such a way that if some one tries to access example2.company.com using http or https it should redirect to https://example2.company.com like below

server {
  listen 80;
  server_name  example1.com  www.example1.com ;

  location / { 
    return 301    https://www.example1.com$request_uri;
  }
}

server {
  listen 80;
  server_name  example2.company.com  www.example2.company.com ;

  location / { 
    return 301    https://www.example2.company.com$request_uri;
  }
}

1 Answers1

13

You need to setup a new vhost for example2.xyz.com. Nginx will read the domain name first then call respectively conf file otherwise default conf.

In nginx conf of vhost listen port 80 seperatly for both example1 and example2 or you can add listen 80 in default conf too for redirection to https.

Use map module for mapping multiple redirection like below example.

map $http_host $new {
  'exp1.xyz.com' '1';
  'exp2.xyz.com' '2';
}

server {
  listen 80;
  if ($new = '1') {
    rewrite ^(.*) https://exp1.xyz.com$1 redirect;
  }
  if ($new = '2') {
    rewrite ^(.*) https://exp2.xyz.com$1 redirect;
  }
}

For creating vhosts in nginx refer this link https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3

Ashish Gupta
  • 305
  • 2
  • 6
  • you advise this over `if ($host ~* "(www\.)?domain\.(com|nl)$") { rewrite ^/old/?$ domain.com/new permanent; }` – snh_nl May 20 '19 at 15:23
  • 1
    Rules based on if statements are not good in ngninx environment - please read https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ – lupo Aug 02 '19 at 09:35
  • Thanks @Ashish Gupta for contributing this. Your answer worked like a charm! :-) – Hussain7 Sep 11 '19 at 12:19
  • @lupo, the title of the article you linked is "If is Evil… when used in location context". Here, `if` is used in the `server` context. – ESV Dec 09 '22 at 13:00