大网数据广告活动
服务器运维问答

nginx redirect based on domain name

· 52CE 我爱测

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 http…

问题原文

原帖链接 · Shiva Krishna Bavandla · CC BY-SA 4.0

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;
  }
}

已采纳解答

原帖链接 · Ashish Gupta · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。