31 Nginx的server块和location块的简单说明

31 Nginx的server块和location块的简单说明

31.1 server与location

server 块 和 location 块都是重点学习的内容,要对Nginx的功能进行详细讲解,后面在静态资源部署章节详细说明

本节主要认识下Nginx默认的配置文件 nginx.conf 下的相关内容,以及 server 块与 location 块使用时的注意事项

一个配置文件对应一个 http 块、一个 http 块下可以对应多个 server 块、一个 server 块下可以对应多个 location 块

    server {    # 1. listen + server_name 组成访问的前半部分
        listen       80;         # listen 监听,默认监听 80端口,可以修改监听端口
        server_name  localhost;  # 指定服务名称,可以是 ip、localhost、域名

                # 2. location 组成访问的后半部分
        location / {
            root   html;                   # root 资源指定对应的目录 
            index  index.html index.htm;   # index 指定默认访问 / 时的首页,即访问 html/index.html
        }

        error_page   500 502 503 504   /50x.html;  # error_page 指定错误码 返回目录
        location = /50x.html {    # location 指定访问路径
            root   html;
        }

31.2 生效规则

当 http、server、location 块中,同时配置,生效规则为就近原则

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf

events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format myformat '=======> This is My format: $http_user_agent';
    access_log  logs/my.log myformat;
    sendfile        on;

    server {
        listen       8080;
        server_name  localhost;

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

    server {
        listen       80;
        server_name  localhost;
        access_log  logs/server.log myformat;    

        location / {
            root   /home/www;
            index  index.html index.htm;
        }
        location /get_text {
        access_log  logs/text.log myformat;
        return 200 "<h1>This is Nginx's Text</h1>";
        }
    location /get_text2 {
        default_type  text/plain;
        return 200 "<h1>This is Nginx's Text</h1>";
    }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
}
[root@nginx-100 /usr/local/nginx/conf]# ../sbin/nginx -t
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
[root@nginx-100 /usr/local/nginx/conf]# ../sbin/nginx -s reload

浏览器访问:http://10.0.0.100/get_text ,logs/text.log 出现日志

image

浏览器访问:http://10.0.0.100/get_text2 ,logs/server.log 出现日志

image

浏览器访问:http://10.0.0.100:8080 ,logs/my.log 出现日志

image

31.3 总结

image

1.http 配置,http 生效

2.server1 配置,server1 生效,server2、server3 未配置,即向上使用 http 生效的配置

3.location1 未配置,即向上使用 server1 生效的配置,location2 配置,location2 生效,location3、location4、location5 未配置,向上使用 server2、server3 配置,server2、server3 未配置,即再向上使用 http 生效的配置

 

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

                                                                                                                         无敌小马爱学习

posted on 2026-04-21 22:26  马俊南  阅读(5)  评论(0)    收藏  举报