nginx rewrite only specific servername to https
1 2 3 4 5 6 7 8 9 10 11 12 | 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix