需求: 把某个域名的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.

asked Mar 20 '13 at 19:29
32bitfloat

158228

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.

nginx rewrite only specific servername to https的更多相关文章

  1. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  2. [转帖]Nginx rewrite 规则 与 proxy_pass 实现

    Nginx rewrite 规则 与 proxy_pass 实现 https://www.cnblogs.com/jicki/p/5546916.html Nginx rewrite 规则  与 pr ...

  3. Nginx rewrite(重读)

    Nginx Rewrite规则相关指令  Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Re ...

  4. nginx rewrite 实现URL跳转

    最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...

  5. nginx从http跳转到https

    场景 项目前期使用http,后期为了安全方面的考虑,启用了https. 项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡. 实际上只需要在nginx上启用https即可,使客户 ...

  6. nginx rewrite 指令

    ginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else. 该模块需要PCRE支持,应在编译nginx时指定PCRE源码目录, nginx安装方 ...

  7. nginx配置http强制跳转https

    nginx配置http强制跳转https 网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置. 一.采用nginx的rewrite方法 ...

  8. Nginx rewrite(重写)

    Nginx Rewrite规则相关指令  Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Re ...

  9. 【Web】Nginx Rewrite规则

    Rewrite介绍 Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Ngi ...

随机推荐

  1. python opencv3 基于ORB的特征检测和 BF暴力匹配 knn匹配 flann匹配

    git:https://github.com/linyi0604/Computer-Vision bf暴力匹配: # coding:utf-8 import cv2 """ ...

  2. python opencv3 向图像里写字

    git:https://github.com/linyi0604/Computer-Vision # coding:utf-8 import cv2 img = cv2.imread(".. ...

  3. wikioi 1080 线段树练习 树状数组

    1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...

  4. AbstractAction

    package cn.tz.action.abs; import java.io.File; import java.io.IOException; import java.text.SimpleDa ...

  5. Mac下使用ABTestingGateway快速搭建灰度网关

    ABTestingGateway简介 ABTestingGateway 是新浪开源的一个可以动态设置分流策略的灰度发布系统,工作在7层,基于nginx和ngx-lua开发,使用 redis 作为分流策 ...

  6. 你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)

    什么让你对C/C++如此恐惧? 本篇将继续上一篇来讨论段错误(Segmentation fault). 上一篇: 你的C/C++程序为什么无法运行?揭秘Segmentation fault(1) 追溯 ...

  7. Git_远程仓库

    到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了. 可是有用过集中式版本控制系统SVN的童鞋会站出来说,这些功能在SVN里早就有了,没看出Gi ...

  8. mysql数据库表迁移

    @ 把老数据库中的某个表倒出成sql文件 $mysqldump -uroot -p123456 my_db > my_db.sql (输入密码) @ 在新环境中导入 $sudo apt-get ...

  9. 转 UIAlertView 不显示、屏幕变灰

    UIAlertView 不显示.屏幕变灰 SvenFang 票 在 [[NSNotificationCenter defaultCenter] addObserver:self selector:@s ...

  10. HTML5中的Web Storage(sessionStorage||localStorage)理解与简单实例

    Web Storage是什么? Web Storage功能,顾名思义,就是在Web上针对client本地储存数据的功能,详细来说Web Storage分为两种: sessionStorage: 将数据 ...