当前位置: 首页 > news >正文

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

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.

相关文章:

  • 闭包总结
  • 浅谈k8s cni 插件
  • Python爬虫--- 1.3 BS4库的解析器
  • Intellij IDEA 部署 Spring Boot / Spring Cloud 应用到阿里云
  • 线程之间调用问题
  • cdn转es5
  • Selenium 2自动化测试实战
  • css控制默认滚动条样式
  • MaxCompute表设计最佳实践
  • 一个JAVA程序员成长之路分享
  • 查看nginx服务器状态
  • SpringBoot整合Swagger2
  • 3年工作经验的Java程序员面试经过
  • Vue项目Webpack优化实践,构建效率提高50%
  • 关于tio 协议(Packet)中 消息头的长度(HEADER_LENGTH)的理解
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • AngularJS指令开发(1)——参数详解
  • C++入门教程(10):for 语句
  • CentOS6 编译安装 redis-3.2.3
  • ES6 学习笔记(一)let,const和解构赋值
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • Phpstorm怎样批量删除空行?
  • Redux 中间件分析
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 从零搭建Koa2 Server
  • 分享一份非常强势的Android面试题
  • 后端_MYSQL
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 如何使用 JavaScript 解析 URL
  • 深入浏览器事件循环的本质
  • 首页查询功能的一次实现过程
  • 学习笔记TF060:图像语音结合,看图说话
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 2017年360最后一道编程题
  • 从如何停掉 Promise 链说起
  • 组复制官方翻译九、Group Replication Technical Details
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #define 用法
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (AngularJS)Angular 控制器之间通信初探
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)
  • (转)编辑寄语:因为爱心,所以美丽
  • (转)创业家杂志:UCWEB天使第一步
  • (转)母版页和相对路径
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net 按比例显示图片的缩略图
  • .NET4.0并行计算技术基础(1)
  • .NET与java的MVC模式(2):struts2核心工作流程与原理
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • [AR Foundation] 人脸检测的流程
  • [ASP.NET 控件实作 Day7] 设定工具箱的控件图标