Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the `listen` directive actually listen for in nginx?

Tags:

nginx

The syntax of listen directive is known as the following.

server {
    listen ADDRESS:PORT;
}

But what the ADDRESS and PORT mean here? ADDRESS of client IP address? or server IP address? or the requested IP address? So as the PORT part, who's port to listen for??

Suppose I have a situation like this enter image description here

Would the correct arguments of the listen directive be this or not ?

listen 46.137.123.236:80;

And, in which case should I specify an IP address instead of *:80?

like image 425
yaquawa Avatar asked Oct 19 '25 13:10

yaquawa


1 Answers

The listen directive specifies the nginx server ip and port. Requests are routed to nginx which then distributes them to the upstream pool. Based on your diagram, the host running nginx is at 46.137.123.236 and the 192.168.11.12 address is the pool member upstream which nginx is routing the requests to.

The ip is the ip of the host that the nginx application is running on. As for the port, you choose your own port. You will have to make sure the port is open and not restricted under any firewall or being used by another program. While you can use any port of your choosing, it is common practice to use specific ports for certain types of applications. For instance an http server would generally use port 80 and internet browsers use this port by default. If you chose a different port and were using a browser you would explicitly specify your chosen port in the request. In your example, assuming that the request ip and port is where nginx is, then you are correct by specifying listen 46.137.123.236:80; as the ip and port.

It is customary to use alternate ports when you forward https. For instance you could use 443 in such case. In a corporate environment, it is likely you will have only a few ports at your disposal and in such case there are standard ports for http and https.

like image 193
Mars Avatar answered Oct 21 '25 09:10

Mars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!