如何使Nginx监听server_name:port [英] How to make nginx to listen to server_name:port
问题描述
在我的nginx conf文件中,我有:
In my nginx conf file, I have :
listen 80;
server_name $hostname;
但是如果我执行netstat,我会看到它正在监听0.0.0.0:80
however if I do netstat I see that it is listening on 0.0.0.0:80
我想发生什么,nginx是否可以监听$ hostname:80,有没有一种方法可以配置它呢?
what I want to happen, is the nginx to listen to $hostname:80 , is there a way to configure it to do that?
到目前为止,我尝试了其他设置,但均未成功.感谢你的帮助.
I tried different settings with no success so far. Appreciate your help.
推荐答案
The server_name
docs directive is used to identify virtual hosts, they're not used to set the binding.
netstat
告诉您nginx监听0.0.0.0:80
,这意味着它将接受来自任何IP的连接.
netstat
tells you that nginx listens on 0.0.0.0:80
which means that it will accept connections from any IP.
如果要更改绑定到nginx的IP,则必须更改listen
文档 规则.
因此,如果您想将nginx设置为绑定到localhost
,则可以将其更改为:
If you want to change the IP nginx binds on, you have to change the listen
docs rule.
So, if you want to set nginx to bind to localhost
, you'd change that to:
listen 127.0.0.1:80;
通过这种方式,将丢弃不是来自本地主机的请求(它们甚至不会访问nginx).
In this way, requests that are not coming from localhost are discarded (they don't even hit nginx).
这篇关于如何使Nginx监听server_name:port的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!