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

Nginx reverse-proxy redirects any subdomain while I only configured one redirection

· 52CE 我爱测

I want to use Nginx to redirect to specific applications based on the subdomain of the request. For now I only have one application which has its own server listening on port 3080. I would like the subdomain gitea.mydomain.com to…

问题原文

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

I want to use Nginx to redirect to specific applications based on the subdomain of the request.

For now I only have one application which has its own server listening on port 3080. I would like the subdomain gitea.mydomain.com to redirect to the application server which is listening on localhost:3080.

I created a file in /etc/nginx/sites-available with this content:

server {
    listen 80;
    server_name gitea.mydomain.com;
    location / {
        proxy_pass http://localhost:3080;
    }
}

Well it works too well and I can try anything as a subdomain, or no subdomain at all, and I'm redirected to the app.

For the DNS, I have two A records, one for redirecting my domain to my IP address, and the other one to redirect *.mydomain.com to my IP address.

Isn't that how I am supposed to do that subdomain-redirection?

Thanks for your help guys

EDIT Pothi Kalimuthu's answer solved my problem but I have a follow-up question. Why does Nginx redirected to the application when I told it to redirect only the subdomain? I expected other requests to fail.

已采纳解答

原帖链接 · Pothi Kalimuthu · CC BY-SA 4.0

You might want to add a catch-all server block to take care of every other subdomain and other domains such as example.net or example.org ...

server {
   listen 80 default_server;
   listen [::]:80 default_server;
   server_name _;

   return 444;
}

Note #1: Please be sure to reload or restart Nginx to apply changes.

Note #2: Please note that the above block basically returns nothing. The error code 444 is an Nginx specific error code that means "no response". Ref: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#nginx

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

资料来源与适用范围

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

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

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