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

Redirect multiple domains to SSL - NGINX

· 52CE 我爱测

I have the following server config: server { listen 80; server_name default; return 301 https://$host$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name default; root /home/forge/default/public; …

问题原文

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

I have the following server config:

server {
  listen 80;
  server_name default;
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name default;
    root /home/forge/default/public;

    ... and so it continues

I also have two domain names, test.com and example.com. They both point to the IP adress from the server. If I go to test.com, it gets redirected to https fine, but if I go to example.com, it gets redirected to the https version of test.com instead of the https version of example.com. Why does this happen? I'm very new to server configs. On other Server Fault answers I only found to use the $host variable but it doesn't seem to work.

已采纳解答

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

I just had the same issue and indeed the browser cache was the problem!

Just a note on that: one of the best solution (not cached) to check the redirects would be to curl the server from command line:

curl -I http://my-server.com

About the browser cache, clearing the whole cache - refreshyourcache.com - would work but it's slightly overkill! Another way to ensure the browser is not using the cache is to request a slightly different url, ie:

http://my-server.com?randomString

Another point I wanted to quote after reading the server_name nginx doc: it feels that the recommended way to accept multiple domain names is to use the empty server_name "" rather than default, ie for your config example:

server {
  listen 80;
  server_name "";
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name "";
    root /home/forge/default/public;

    ... and so it continues

But please someone, correct me if I'm wrong! :)

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

资料来源与适用范围

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

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

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