一、nginx多server优先级

[root@web01 conf.d]# cat server1.conf 
server {
        listen 80;
        server_name localhost test1.linux.com;

        location / {
                root /code/test1;
                index index.html;
        }
}

[root@web01 conf.d]# cat server2.conf 
server {
        listen 80;
        server_name localhost test2.linux.com;

        location / {
                root /code/test2;
                index index.html;
        }
}

[root@web01 conf.d]# cat server3.conf 
server {
        listen 80;
        server_name localhost test3.linux.com;

        location / {
                root /code/test3;
                index index.html;
        }
}
1.首先选择所有的字符串完全匹配的server_name。(完全匹配)
2.选择通配符在前面的server_name,如*.mumusir.com,然后匹配通配符在后面的,如 www.mumusir.*
4.最后选择使用正则表达式匹配的server_name
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件

当出现多个相同的server_name情况下,配置文件排序优先使用则会被调用,所以建议配置相同端口,不同域名,这样不会出现域名访问冲突。
#如果出现单台机器多配置文件,页面访问错误的问题
1.如过直接报错,查看hosts是否解析
2.如果访问页面没有对应上
    查看配置文件是否写错域名;
    检查nginx是否重启;

二、Nginx禁止IP直接访问

1.使用ip访问时直接拒绝

[root@web01 conf.d]# vim server3.conf 
server {
        listen 80 default_server;
        server_name _;
        return 500;
}

2.使用ip访问时跳转到指定页面

[root@web01 conf.d]# vim server3.conf 
server {
        listen 80 default_server;
        server_name _;
        return 302 http://www.baidu.com;
}

三、Nginx包含文件Include

一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 

假设现在希望快速的关闭一个站点,该怎么办? 
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦 
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于可读。

inlcude /etc/nginx/online/*.conf #线上使用的配置

/etc/nginx/offline #保留配置,不启用(下次使用在移动到online中)

四、Nginx路径root与alias

root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。

root的处理结果是:root路径+location路径alias的处理结果是:使用alias定义的路径

1.使用root时

#使用root时,用户访问http://image.com/picture/1.jpg时,实际上Nginx会到/code/picture/目录下找1.jpg文件
server {
    listen 80;
    server_name image.com;

    location /picture {
        root /code;
    }
}

2.使用alias时

#使用alias时,用户访问http://image.com/picture/1.jpg时,实际上Nginx会到/code/目录下找1.jpg文件
server {
    listen 80;
    server_name image.com;

    location /picture {
        alias /code;
    }

    #location /picture/ {
    #    alias /code/;
    #}
}
#alias注意 配置的时候 lication 有 ‘/’ alias也要有‘/’,要是没有就都没有

3.线上常用配置

server {
    listen 80;
    server_name image.driverzeng.com;

    location / {
        root /code;
    }

    location ~* ^.*\.(png|jpg|gif)$ {
        alias /code/images/;
    }
}

五、Nginx try_file路径匹配

nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。

1.Nginx try_file配置实例1

#1. 配置nginx
[root@lb01 conf.d]# vim try.conf 
server {
    listen 80;
    server_name try.linux.com;
    root /code;
    index index.html;

    location / {
        try_files $uri /404.html;
    }
}

#2. 创建实例目录与文件linux
[root@lb01 conf.d]# echo try11111 > /code/index.html 
[root@lb01 conf.d]# echo '404 404 404' > /code/404.html

#3. 尝试访问try.linux.com
[root@lb01 conf.d]# curl try.linux.com
404 404 404
#由于访问的是try.linux.com,而$uri取得是域名后面我们写的内容,它找不到,所以访问后面的内容,即404.html

#4. 尝试访问try.linux.com/index.html
[root@lb01 conf.d]# curl try.linux.com/index.html
try11111
#由于访问的是try.linux.com/index.html,而$uri取到了index.html所以返回/code/index.html的内容

#5. 修改配置为
location / {
    try_files $uri $uri/ /404.html;
}

#6. 再次尝试访问try.linux.com
[root@lb01 conf.d]# curl try.linux.com
try11111
#我们访问的是try.linux.com,而$uri我们没有写任何内容,于是他访问的便是“空/”,即匹配到/code/index.html

2.Nginx try_file配置实例2

#1. 配置nginx
[root@lb01 conf.d]# cat try.conf 
server {
    listen 80;
    server_name try.linux.com;
    root /code;
    index index.html;

    location / {
        try_files $uri $uri/ @java;             
        #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@
    }

    location @java {
    proxy_pass http://172.16.1.8:8080;          
    #配置后端tomcat
    }
}

#2. 配置后端tomcat
[root@web02 ~]# cd /usr/share/tomcat/webapps/ROOT
[root@web02 ROOT]# echo 'i am tomcat' > index.html
[root@web02 ROOT]# systemctl start tomcat

#3. 把文件都挪走
[root@lb01 code]# mv index.html index1.html /tmp/

#4. 测试访问
[root@lb01 code]# curl http://try.linux.com/index.html
i am tomcat

六、Nginx调整上传文件大小

在nginx使用上传文件的过程中,通常需要设置保温大小限制,避免出现413 Request Entity Too Large

1.nginx上传文件大小限制配置语法

Syntax:  client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location

2.nginx长传文件大小限制配置示例

#也可以放入http层,全局生效
server {
    listen 80;
    server_name _;
    client_max_body_size 200m;
}

七、Nginx优雅显示错误页面

1.配置跳转本地页面

[root@web01 conf.d]# vim server1.conf
server {
        listen 80;
        server_name test1.linux.com;
        root /code/test1;
        index index.html;

        error_page 404 /404.html;
        error_page 403 /403.html;
        error_page 502 /502.html;
        error_page 503 /503.html;

        location = /404.html {
            root /code/error;
        }
}

2.配置跳转域名

[root@web01 conf.d]# vim server1.conf
server {
        listen 80;
        server_name test1.linux.com;
        root /code/test1;
        index index.html;

        error_page  404  http://www.baidu.com;
}
Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:36

results matching ""

    No results matching ""