1. 程式人生 > >nginx rewrite only specific servername to https

nginx rewrite only specific servername to https

需求: 把某個域名的80埠服務  ----》 重定向轉到 這個域名的 443埠的服務。  
server {

    listen 80;

    server_name xxx.abcd.com.cn;

    if ($host = "xxx.abcd.com.cn") {
            rewrite ^ https://$server_name$request_uri? redirect;
    }
    return 403;

}

  重啟nginx 就可以了。

https://serverfault.com/questions/489801/nginx-rewrite-only-specific-servername-to-https

www 是指域名前帶 www的,以百度為例,就是 

www.baidu.com
@ 是指前面不帶任何主機名的,以百度為例,就是 baidu.com
* 是指泛解析,是指除已新增的解析記錄以外的所有主機都以此為準,以百度為例,就是 12343.baidu.com 但解析的時候並沒有針對 12343。

  --------------------------------------------------------------------------------------------- Ask Question up vote
2down votefavorite 1

It's about two subdomains. The first one (www) should be accessed via http. The second one (cloud) should be accessed via https.

These are the relevant parts of my entries:

server {
        listen 80;
        server_name cloud.example.de;
        rewrite ^ https://$server_name$request_uri? permanent;  # enforce https
}

server {
        listen 443 ssl;
        server_name cloud.example.de;
        root /home/user/web/cloud;
}

server {
        listen 80;
        server_name www.example.de;
        root /home/user/web/cms;

        #etc.
}

When I now call http://cloud.example.de I am redirected to https://cloud.example.de, fine. But when I call http://www.example.de I am also redirected, to https://www.example.de, which leads me to the content of cloud.example.com, because this is the only servername set as used by port 443. 

There is no entry in the access-log of the www-subdomain.
There is another subdomain pointing to a phpPgAdmin. This I can access as normal, it's not rewritten.

server {
        listen 80;
        server_name pgsql.example.de;
        root /home/user/web/phppgadmin;

        #etc
}

What is missing? The rewrite should be only done if the servername matches cloud.example.de
And is there a relevance in the order of the server entries or does it not matter? 


Using nginx 0.8.54 on Ubuntu 11.04.

      share improve this question asked  Mar 20 '13 at 19:29 32bitfloat 158228 add a comment

1 Answer

active oldest votes up vote 8down voteaccepted

The sample config that you provide looks about right, and I doubt it would work as you describe (you probably made too many changes when trying to simplify it).

Are you getting wrong redirects in something like curl, or only in the browser? I've dealt with cases where permanent is permanently cached in Mozilla (e.g. from a prior nginx.conf), without any way to invalidate a single 301 cache entry, so, are you sure it's not a cache issue?

In any case, you could also try using if to make the redirects conditional (perhaps the first server gets chosen as the default server):

    if ($host = "cloud.example.de") {
            rewrite ^ https://$server_name$request_uri? redirect;
    }
    return 403;

Or, another option,

server {
    listen 80;
    listen 443 ssl;
    server_name cloud.example.de;
    if ($scheme != "https") {
        rewrite ^ https://$server_name$request_uri? redirect;
    }
    root /home/user/web/cloud;
}

And try curl -v to make sure you're seeing what is there.