Nginx:重定向服务器名称的条件和短URL的子域

时间:2013-12-05 19:33:11

标签: redirect nginx short-url

我想根据服务器名称有条件地重定向,但我重定向到的位置也取决于子域。例如,这是我的基本配置

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

server {
    listen 443;
    server_name company.com compa.ny;
    ssl on;
    ssl_client_certificate /etc/ssl/certs/godaddy_CA.crt;
    ssl_certificate /etc/ssl/certs/wildcard.company.com.crt;
    ssl_certificate_key /etc/ssl/private/wildcard.company.com.key;
    ssl_prefer_server_ciphers on;
    root /var/www/company;
    access_log /var/log/nginx/nginx.access.log;
    error_log /var/log/nginx/nginx.error.log;
    client_max_body_size 8M;
    location ^~ /application {
        proxy_set_header HOST $http_host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:8080;
    }
}

我想找一些寻找短网址主机“compa.ny”并重定向到“company.com/shortUrldRedirector”的内容,我也希望包含子域名,因此在dev或qa中这将正常工作:< / p>

https://compa.ny/abc123 -> https://company.com/shortUrldRedirector/abc123

and

https://dev.compa.ny/abc123 -> https://dev.company.com/shortUrldRedirector/abc123

我看到有一个$server_name配置变量,但是如何完成上述关于子域的重定向?

1 个答案:

答案 0 :(得分:0)

我会像这样使用地图构造:

map $http_host $long_domain {
    default      company.com;
    dev.compa.ny dev.company.com;
    compa.ny     company.com;
}

server {
    ...
    return 301 https://$long_domain/shortUrldRedirector$request_uri;
}