大网数据广告活动
服务器运维问答

Redirect from port 80 to 443 with nginx and multiple servers

· 52CE 我爱测

To begin, I'm a bit new with nginx and a bit lost… I sadly didn't found any solution to this, so here is my problem. If it's a duplicate of an already existing question I apologize and I would be very pleased to see the answer. I…

问题原文

原帖链接 · garfieldairlines · CC BY-SA 3.0

To begin, I'm a bit new with nginx and a bit lost… I sadly didn't found any solution to this, so here is my problem. If it's a duplicate of an already existing question I apologize and I would be very pleased to see the answer.

I'm building my very first nginx server and I have two domain names, one of these has plenty of subdomains, and I wish to put a SSL redirect on all of these. Problem is that it works only once with a IPv6 listen (here on the default config), and nginx dosen't start if I put that same config on the other subdomains (each subdomain has its own server config file with its own 80 → 443 server redirect).

server {
listen 443;
listen [::]:443;

## All the config and stuff

}

server {
listen      80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name www.mydomainna.me mydomainna.me;
rewrite     ^   https://$server_name$request_uri? permanent;
}

This is the default config file, and it works like a charm.

However and this is the thing that bugs me, is that on this exact same config (less the default_server) for every single subdomain and my second domain name I must remove IPv6 or nginx dosen't want to start.

server {
listen 443;
listen [::]:443;

## All the config and stuff

}

server {
listen      80;
server_name subdomain.mydomainna.me;
rewrite     ^   https://$server_name$request_uri? permanent;
}

So my question here is : how can I tell to listen on IPv6 for all of my other config files ?

I'm sorry if I didn't explained my question in a adequate manner and if anyone of you has a solution, thanks.

已采纳解答

原帖链接 · Tim · CC BY-SA 3.0

According to this page the line below tells Nginx to listen on port 80 for both IP4 and IP6.

listen [::]:80 default_server;

You have two directives telling Nginx to listen on port 443, which is why you're getting the duplicate listen message. Just remove the first line below and it should work.

listen 443;
listen [::]:443;

Note that I knew nothing about Nginx IP6 until I spent 2 minutes on Google. A very small amount of research uncovered this solution.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。