• 13.Nginx使用常见问题


    1.Nginx多Server优先级

    在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中每个server的server_name进行匹配,以此决定到底由哪一个server来处理这个请求。但nginx如配置多个相同的server_name,会导致server_name出现优先级访问冲突。

    1.准备nginx对应的配置文件

    [root@web02 conf.d]# cat code1.conf
    server {
        listen 80;
        server_name localhost code1.bgx.com;
        location / {
            root /code1;
            index index.html;
        }
    }
    [root@web02 conf.d]# cat code2.conf
    server {
        listen 80;
        server_name localhost code2.bgx.com;
        location / {
            root /code2;
            index index.html;
        }
    }
    [root@web02 conf.d]# cat code3.conf
    server {
        listen 80;
        server_name localhost code3.bgx.com;
        location / {
            root /code3;
            index index.html;
        }
    }
    

    2.准备站点目录

    [root@web02 conf.d]# mkdir /code{1..3} -p
    [root@web02 conf.d]# for i in {1..3};do echo "Code$i" > /code$i/index.html;done
    [root@web02 conf.d]# cat /code1/index.html 
    Code1
    [root@web02 conf.d]# cat /code2/index.html 
    Code2
    [root@web02 conf.d]# cat /code3/index.html 
    Code3
    

    3.检查语法, 并重新加载Nginx

    [root@web02 conf.d]# nginx -t
    nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
    nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    # 重启Nginx
    [root@Nginx ~]# systemctl restart nginx
    

    4.测试访问效果

    #1.当用户第一次访问, 由code1.conf返回输出信息
    [root@Nginx ~]# curl localhost
    Code 1
    
    #2.此时将code1.conf修改为code5.conf后进行重载Nginx
    [root@Nginx ~]# mv code1.conf code5.conf
    [root@Nginx ~]# systemctl reload nginx
    
    #3.再次访问时, 由code2.conf返回输出信息
    [root@Nginx ~]# curl localhost
    Code 2
    

    5.多ServerName优先级总结, 在开始处理一个HTTP请求时,Nginx会读取 header(请求头)中的host,与每个server中的 server_name进行匹配,来决定用哪一个server标签来完成处理这个请求。有可能一个Host与多个server中的server_name都匹配,这个时候就会更具匹配优先级来选择实际处理的server块。优先级匹配结果如下:

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

    注意:当出现多个相同的Server_Name情况下,配置文件排序优先使用则会先被调用,所以建议配置相同端口, 不同域名,这样则不会出现域名访问冲突。

    2.Nginx禁止IP直接访问

    当用户通过访问IP或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都要求网站主关闭空主机头,防止未备案的域名指向过来造成麻烦

    server {
        listen 80;
        server_name www.xuliangwei.com   # 这里指定自己的域名
    }
    server{
        listen 80 default_server;       # 默认优先返回
        server_name _;                  # 空主机头或IP
        return 500;                     # 返回500错误
    }
    

    2.也可以将流量集中导入自己的网站,只要做以下跳转设置就可以

    server {
        listen 80 default_server;
        return 302 https://www.xuliangwei.com;
    }
    

    3.Nginx包含文件Include

    一台服务器配置多个server网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。
    假设现在希望快速的关闭一个站点,该怎么办?
    1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
    2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释

    Include包含的作用是为了简化主配置文件,便于人类可读。

    4.Nginx路径root与alias

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

    1.root路径配置实例: 用户访问www.xuliangwei.com/image/test.gif,实际上Nginx会上/code/image/目录下找去找test.gif文件

    server {
            listen 80;
            server_name www.xuliangwei.com;
            
            location /image/ {
                    root /code;
            }
    }
    

    2.alias配置实例: 用户访问www.xuliangwei.com/image/test.gif,实际上Nginx会上/code/目录下找去找test.gif文件。

    server {
            listen 80;
            server_name www.xuliangwei.com;
            location /image/ {
                    alias /code/image/;
            }
    }
    

    5.Nginx try_file路径匹配

    nginx的try_file路径匹配,按顺序检查文件是否存在

    [root@bgx ~]# cat /etc/nginx/conf.d/try_file.conf
    server {
        listen 80;
        server_name try.bgx.com;
        root /code;
    
        location / {
            try_files $uri /404.html /index.php;
        }
    }
    
    #1.检查用户请求的uri内容是否存在本地,存在则解析
    #2.如果请求的url不存在,则访问对应站点目录中的404.html文件 
    #3.最后交给index.php处理 
    

    1.演示环境准备

    [root@Nginx ~]# echo "Try-Page" > /soft/code/index.html
    [root@Nginx ~]# echo "Tomcat-Page" > /soft/app/apache-tomcat-9.0.7/webapps/ROOT/index.html
    
    #启动tomcat
    [root@Nginx ~]# sh /soft/app/apache-tomcat-9.0.7/bin/startup.sh
    #检查tomcat端口
    [root@Nginx ~]# netstat -lntp|grep 8080
    tcp6       0      0 :::8080                 :::*                    LISTEN      104952/java 
    

    2.配置Nginx的tryfiles

    [root@Nginx ~]# cat /etc/nginx/conf.d/try.conf 
    server {
            listen 80;
            server_name try.bgx.com;
            root /code;
            index index.html;
            location / {
                    try_files $uri @java_page;
            }
            location @java_page {
                    proxy_pass http://127.0.0.1:8080;
            }
    }
    
    #重启Nginx
    [root@Nginx ~]# nginx -s reload
    

    3.测试tryfiles

    [root@Nginx ~]# curl http://try.bgx.com/index.html
    Try-Page
    
    #将/code/index.html文件移走
    [root@Nginx ~]# mv /code/{index.html,index.html_bak}
    
    #发现由Tomcat吐回了请求
    [root@Nginx ~]# curl http://try.bgx.com/index.html    
    Tomcat-Page
    

    6.Nginx调整上传文件大小

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

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

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

    nginx上传文件大小限制配置示例

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

    7.Nginx优雅显示错误页面

    error_page错误日志

    [root@web01 conf.d]# cat code3.conf 
    server {
        listen 80;
        server_name code.oldboy.com;
        location / {
            root /code;
        }
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:900;
        }
        
        #如服务器返回如下错误状态码,则进行跳转,跳转至/404.html
        error_page 404 403 /40x.html;
        
        #如服务器返回如下错误状态码,则进行跳转,跳转至/50x.html
        error_page 500 502 503 504  /50x.html;
    
        #精准匹配访问
        location = /404.html {
           root /code;
        }
        location = /50x.html {
           root /code;
        }
    }
  • 相关阅读:
    系统设计的一些原则
    分层开发思想与小笼包
    工作与生活
    Microsoft .NET Pet Shop 4 架构与技术分析
    用人之道(二) 如何管理软件开发团队
    也谈很多开发人员的毛病
    《3S新闻周刊》第10期,本期策划:“超女”营销带来的启示
    浅析ArcIMS
    MapX的坐标问题
    应用ArcIMS构建GMap风格的地图应用
  • 原文地址:https://www.cnblogs.com/sseban/p/11536669.html
Copyright © 2020-2023  润新知