zoukankan      html  css  js  c++  java
  • 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。

     
    ---------------------------------------------------------------------------------------------

    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.

    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.

  • 相关阅读:
    清北学堂总结(未完待续。。。。。。。)
    洛谷p3372 线段树模版
    SPFA模版
    线段树 洛谷 p1531 I hate it(I hate it too)
    01 背包找装满方案数 洛谷 p1164 小a点菜
    01 找最大剩余体积 洛谷1049 装箱问题
    洛谷 p1880 石子合并 区间dp
    石子合并 最大值
    清北学堂入学测试d
    HTML 标记 3 —— 框架
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10131593.html
Copyright © 2011-2022 走看看