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

Why is nginx default_server masking a better match?

· 52CE 我爱测

I got my local nginx serve 3 sites with the respective configurations below. They worked well that way in my local LAN (see below also for the dnsmasq config) when requested with A or A.domain . Now I wanted one of those sites to…

问题原文

原帖链接 · Chaos_99 · CC BY-SA 4.0

I got my local nginx serve 3 sites with the respective configurations below. They worked well that way in my local LAN (see below also for the dnsmasq config) when requested with A or A.domain. Now I wanted one of those sites to be available from the internet via a dyndns domain like B.domain.ddns.net.

I configured my router to forwarded port 81 (to not accidentally mask the admin interface of the router) to port 80 of my local server and added the lines currently commented out in the examples below except the default_server line.

I entered http://B.domain.ddns.net:81 as URL. It didn't work.

I assumed that maybe the subdomain somehow doesn't work with the dyndns system, so I additionally set one site to default_server.

Now that made the site available from the internet, but suddenly one of the other sites is no longer available locally, neither with C nor C.domain, instead the default one is served.

Anybody know why this happens? Or why the dyndns domain does not match in the first place? Or how I can set the access log to show me the requested site WITH the domain string? Or maybe if there is a better way to serve just one site to the internet and the others locally?

Site A

server {
  listen 80;
  listen [::]:80;
  server_name A.domain, A; 
...

Site B

server {
   listen 80;
   # used this instead of previous line after specific domain below didn't work
   # listen 80 default_server;
   listen [::]:80;
   server_name B.domain, B;
   #used this instead of previous for external access
   # server_name B.domain, B, B.domain.ddns.net; 
...

Site C

server {
   listen 80;

   server_name C, C.domain;

dnsmasq-config

strict-order
local=/heimatsender/
expand-hosts
domain=heimatsender
log-queries
dhcp-range=192.168.1.100,192.168.1.200,255.255.255.0,168h
log-dhcp
dhcp-option=option:router,192.168.1.1
dhcp-hostsfile=/etc/dnsmasq-dhcphosts

dhcp-option=15,"domain"
dhcp-option=6,192.168.1.18

cname=A.domain,A
cname=A.domain.domain,A.domain
cname=Aalias,A
cname=Aalias.domain,A.domain
cname=C,A
cname=C.domain,A.domain
cname=B,A
cname=B.domain,A.domain

Update Removing the commas in the server_name field made the sites available from local LAN again. But from the internet, only domain.ddns.net:81 works to connect to the site configured as default server. B.domain.ddns.net:81 doesn't, although configured as a server_name. More help still welcome.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The server_name is an exact match, unless you use wildcards or regular expression syntax. See this document for details.

The domain B.domain.ddns.net does not match any of your existing server_name expressions, so the default server will be used instead.

If there is no listen directive with a default_server modifier, the first server block (with a matching IP:PORT) will be used. Without knowing the load order of your configuration, we don't know which one that was (possibly site C as it mysteriously stopped working). Use nginx -T to see the entire configuration as read by nginx.

There is no comma separator in the server_name directive. You site B statement should perhaps be:

server_name B.domain.ddns.net B.domain B;

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

资料来源与适用范围

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

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

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