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

Nginx Embedded Variables - How do you get the domain name used in a request?

· 52CE 我爱测

When using multiple domain names for one server, how do you get the domain name currently being used in a request? I'm using the embedded variables for Nginx in it's configuration file. I've set up OpenSSL and have a redirect tha…

问题原文

原帖链接 · Forrest Wilkins · CC BY-SA 3.0

When using multiple domain names for one server, how do you get the domain name currently being used in a request? I'm using the embedded variables for Nginx in it's configuration file.

I've set up OpenSSL and have a redirect that works fine for my first domain since, as you can see, I'm explicitly redirecting to it at the bottom of my config file. So an HTTP request on any of the other domains redirects to that first one for a secure connection. Is there an embedded variable I can use, similar to $request_uri, for example, but just returns the given domain name used by the client?

Here's the server block where I'm running the redirect.

server {
        listen 80;
        listen [::]:80;
        server_name example.com example1.com example2.com example3.com;
        location / {
                return 301 https://example.com$request_uri;
        }
}

And the rest of my config: https://pastebin.com/HgnZ0aBe

已采纳解答

原帖链接 · Ilham Sulaksono · CC BY-SA 3.0

Domain name that used in request is basically a host request header. In nginx, variable for host header is $host. So if you want to redirect based on domain name/host you should change the configuration to :

return 301 https://$host$request_uri;

Hope this help . Thanks

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

资料来源与适用范围

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

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

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