• 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.

  • 相关阅读:
    美剧天堂前100最新
    自动登录github
    分布式爬虫
    爬虫框架:scrapy
    First day ~
    docker 使用的一些笔记
    POI 写出word文档实例
    POI 生成word各类问题(设置中文字体,重复图片插入等怪问题......)
    HTML 与 文本 的相互转义
    TDengine + Telegraf + Grafana 运维监测系统搭建
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10131593.html
Copyright © 2020-2023  润新知