nginx rewrite only specific servername to https
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.
nginx rewrite only specific servername to https的更多相关文章
- [转帖]Nginx rewrite模块深入浅出详解
Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...
- [转帖]Nginx rewrite 规则 与 proxy_pass 实现
Nginx rewrite 规则 与 proxy_pass 实现 https://www.cnblogs.com/jicki/p/5546916.html Nginx rewrite 规则 与 pr ...
- Nginx rewrite(重读)
Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Re ...
- nginx rewrite 实现URL跳转
最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...
- nginx从http跳转到https
场景 项目前期使用http,后期为了安全方面的考虑,启用了https. 项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡. 实际上只需要在nginx上启用https即可,使客户 ...
- nginx rewrite 指令
ginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else. 该模块需要PCRE支持,应在编译nginx时指定PCRE源码目录, nginx安装方 ...
- nginx配置http强制跳转https
nginx配置http强制跳转https 网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置. 一.采用nginx的rewrite方法 ...
- Nginx rewrite(重写)
Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx Re ...
- 【Web】Nginx Rewrite规则
Rewrite介绍 Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Nginx的Rewrite功能,在编译Ngi ...
随机推荐
- UOJ.87.mx的仙人掌(圆方树 虚树)(未AC)
题目链接 本代码10分(感觉速度还行..). 建圆方树,预处理一些东西.对询问建虚树. 对于虚树上的圆点直接做:对于方点特判,枚举其所有儿子,如果子节点不在该方点代表的环中,跳到那个点并更新其val, ...
- GITC简单感触
GITC短暂的2天,去参加主要是想参与其中,了解其他家的技术,技术使用, 那些大牛,及大牛公司,大牛团队的一些事. 早上的主会场主要是介绍和宣传.半小时后就出去逛逛外面的分会场: 参与听了下 智能硬件 ...
- Android 打包出现jdk版本错误的问题
Android 打包出现 jdk 版本错误的问题,本质上是 SDK 的问题,与 JDK 无关.如果 SDK 的 API 是24或者更高,就要求 jdk 1.8,我这里指定的 API 是22,所以去勾选 ...
- pygame系列_第一个程序_图片代替鼠标移动
想想现在学校pygame有几个钟了,就写了一个小程序:图片代替鼠标移动 程序的运行效果: 当鼠标移动到窗口内,鼠标不见了,取而代之的是图片..... ========================= ...
- BZOJ 1192 鬼谷子的钱袋 数论
1192:鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1926 Solved: 1417 题目连接 http://www.lydsy ...
- tsinsen A1067. Fibonacci数列整除问题 dp
A1067. Fibonacci数列整除问题 时间限制:1.0s 内存限制:512.0MB 总提交次数:2796 AC次数:496 平均分:51.83 将本题分享到: 查看未格 ...
- 快速定位问题 Request无法获取参数
比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...
- [Visual Studio] 安装清单
VS安装位置要求路径必须是英文,且位于Program Files (x86)文件夹下. 下载工具vs_Professional.exe:https://pan.baidu.com/s/1jHRjiia ...
- private、protected、public
private protected public 本类内 Y Y Y 子类内 N Y Y 外部 N N N <?php class MyClass{ public $public = 'Pu ...
- Hibernate与MyBatis的对比总结
最近做了一个Hibernate与MyBatis的对比总结,希望大家指出不对之处. 第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出 ...