38 Nginx配置指令之listen

38 Nginx配置指令之listen

38.1 listen指令

listen:配置监听端口

语法

listen address[:port] [default_server]......;

listen port [default_server]......;

默认值 listen *:80 | *:8000
位置 server

 

 

 

 

 

位置:nginx官网 -> documentation -> ngx_http_core_module -> listen

文档地址:https://nginx.org/en/docs/http/ngx_http_core_module.html#listen

listen 127.0.0.1:8000;  // listen localhost:8000 监听指定的IP和端口
listen 127.0.0.1;       // 监听指定IP的所有端口
listen 8000;            // 监听指定端口上的连接
listen *:8000;          // 监听指定端口上的连接
listen localhost:8000;  // listen localhost:8000 监听指定的IP和端口

38.2 default_server

default_server属性是标识符,用来将此虚拟主机设置成默认主机,默认主机指如果没有匹配到对应的address:port,则会默认执行,如果不指定时,默认使用的是第一个server

[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
..........
   # include /home/www/conf.d/*.conf; 

    server {
        listen       8080;
        server_name  127.0.0.1;
..........
[root@nginx-100 ~]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

浏览器访问:http://10.0.0.100:8080/

image

浏览器访问:http://10.0.0.100:8080/

image

外部不能访问的原因是网络,内部可以访问

[root@nginx-100 ~]# curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<p><em>I am junnan666</em></p>
</body>
</html>

38.3 演示顺序

[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
..........
   # include /home/www/conf.d/*.conf; 

    server {
        listen 8080;
        server_name _;
        default_type text/plain;
        return 444 'not found server!!!';
        }

    server {
        listen       8080;
        server_name  127.0.0.1;
..........

浏览器访问:http://10.0.0.100:8080/

image

总结:默认使用的是第一个server

38.4 演示无default_server

[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
.........
   # include /home/www/conf.d/*.conf; 

    server {
        listen       8080;
        server_name  127.0.0.1;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen 8080;
        server_name _;
        default_type text/plain;
        return 444 'not found server!!!';
        }
}
[root@nginx-100 ~]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

浏览器访问:http://10.0.0.100:8080/

image

38.5 演示有default_server

[root@nginx-100 ~]# cat /usr/local/nginx/conf/nginx.conf
.........
   # include /home/www/conf.d/*.conf; 

    server {
        listen       8080;
        server_name  127.0.0.1;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
        return 444 'not found server!!!';
        }
}
[root@nginx-100 ~]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

浏览器访问:http://10.0.0.100:8080/

image

总结:default_server属性是标识符,用来将此虚拟主机设置成默认主机,默认主机指如果没有匹配到对应的address:port,则会默认执行

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-09 09:49  马俊南  阅读(14)  评论(0)    收藏  举报