zoukankan      html  css  js  c++  java
  • nginx 常见问题二

    Nginx常见问题

    一、nginx多server优先级

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

    1.多server优先级总结

    1.请求的内容完全匹配server_name
    2.选择通配符在前面的server_name     *.test.com
    3.选择通配符在后面的server_name	   www.test.*
    4.选择正则表达式匹配上的server_name     ~^linux.(.*).com$
    5.选择在listen后面加了default_server的server块
    6.如果没有配置文件可以匹配,则从上往下访问配置并返回第一个server的配置
    

    二、nginx禁止IP访问网站

    1.禁止IP访问直接返回错误

    [root@web01 /etc/nginx/conf.d]# vim a_ip.conf
    server {
        listen 80 default_server;
        server_name localhost;
        return 500;
    }
    

    2.引流的方式跳转页面

    [root@web01 /etc/nginx/conf.d]# vim a_ip.conf
    server {
        listen 80 default_server;
        server_name localhost;
        rewrite (.*) http://www.baidu.com$1;
        #return 302 http://www.baidu.com$request_uri;
    }
    

    3.返回指定内容

    [root@web01 /etc/nginx/conf.d]# vim a_ip.conf
    server {
        listen 80 default_server;
        server_name localhost;
        default_type text/plain;
        return 200 "请请求其他页面...或使用域名请求!";
    }
    

    三、nginx的include

    一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf
          主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 
    
    假设现在希望快速的关闭一个站点,该怎么办? 
    1.如果是写在nginx.conf中,则需要手动注释,比较麻烦 
    2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,
          便于人类可读。
    
    inlcude /etc/nginx/online/*.conf 	#线上使用的配置
    mv /etc/nginx/online/test.conf /etc/nginx/offline/ 	#保留配置,不启用(下次使用在移动到online中)
    

    四、nginx的root和alias

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

    1.root配置

    [root@web01 /etc/nginx/conf.d]# vim root.conf 
    server {
        listen 80;
        server_name linux.root.com;
    
        location /download {
            root /code;
        }
    }
    
    # 使用root时,当我请求 http://linux.root.com/download/1.jpg 时,
          实际上是去找服务器上 /code/download/1.jpg 文件
    

    2.alias配置

    [root@web01 ~]# vim /etc/nginx/conf.d/alias.conf 
    server {
        listen 80;
        server_name linux.alias.com;
    
        location /download {
            alias /code;
        }
    }
    
    #使用alias时,当我请求 http://linux.root.com/download/1.jpg 时,实际上是去找服务器上 /code/1.jpg 文件
    

    3.企业中的配置

    server {
        listen 80;
        server_name image.driverzeng.com;
    
        location / {
            root /code;
        }
    
        location ~* ^.*.(png|jpg|gif)$ {
            alias /code/images/;
        }
    }
    
    #注意:
    URL: http://linux.root.com/download/1.jpg
    URI: /download/1.jpg
    $request_filename: /code/download/1.jpg
    $request_uri: /download/1.jpg
    

    五、Nginx try_file路径匹配

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

    1.配置try_files

    [root@web01 /etc/nginx/conf.d]# vim t_file.conf
    server {
        listen 80;
        server_name linux.try.com;
    
        location / {
            root /code/try;
            #index index.html index.htm;
            try_files $uri /404.html;
        }
    }
    

    2.创建站点

    [root@web01 ~]# mkdir /code/try
    [root@web01 /code/try]# echo try_file_index.html > index.html
    [root@web01 /code/try]# echo '404 404 404' > 404.html
    

    3.访问测试

    1.当访问 linux.try.com ,结果返回的是404.html
    由于请求的是域名,后面没有URI,所以$uri匹配为空,匹配不到内容则返回404.html
    
    2.当访问 linux.try.com/index.html,返回的结果是 index.html
    请求的域名后面URI是/index.html ,所以$uri匹配到 /index.html,返回 /code/try/index.html
    

    4.修改nginx配置

    [root@web01 /etc/nginx/conf.d]# vim t_file.conf
    server {
        listen 80;
        server_name linux.try.com;
    
        location / {
            root /code/try;
            #index index.html index.htm;
            try_files $uri $uri/ /404.html;
        }
    }
    
    1.当访问 linux.try.com ,结果返回的是index.html
    	请求的是域名,后面没有URI,所以$uri匹配为空
    	然后 $uri/ 匹配到了 '空'/,相当于请求站点目录,默认返回站点目录下的 index.html
    	如果仍然匹配不到内容则返回 404.html
    

    5.try_files应用

    [root@web01 /code/try]# vim /etc/nginx/conf.d/t_file.conf 
    server {
        listen 80;
        server_name linux.try.com;
    
        location / {
            root /code/try;
            #index index.html index.htm;
            try_files $uri $uri/ @java;
        }
    
        location @java {
            proxy_pass http://172.16.1.8:8080;
    	}
    }
    

    六、Nginx调整上传文件大小

    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.跳转到网上

    #error_page配置的是http这种的网络地址
    [root@web01 conf.d]# cat error.conf 
    server {
        listen       80;
        server_name  linux.error.com;
    
        location / {
    		root /code/error;
            index index.html;
            error_page 404 http://www.baidu.com;
        }
    }
    

    2.跳转本地文件

    [root@web01 /code/error]# vim /etc/nginx/conf.d/error.conf
    server {
        listen 80;
        server_name  linux.error.com;
    
        location / {
            root /code/error;
            index index.html;
            error_page 404 403 /404.jpg;
        }
    }
    

    3.访问PHP的错误页面跳转

    [root@web01 /code/error]# vim /etc/nginx/conf.d/error.conf
    server {
        listen 80;
        server_name  linux.error.com;
        root /code/error;
        index index.php;
        error_page 404 403 /404.html;
    
        location ~* .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            if (!-e $request_filename) {
                rewrite (.*) http://linux.error.com/404.jpg;
            }
        }
    }
    
  • 相关阅读:
    剑指offer(14)链表中倒数第K个节点
    剑指offer(13)调整数组顺序使奇数位于偶数前面
    跨域资源共享CORS
    同源政策
    剑指offer(12)数值的整数次方
    剑指offer(11)二进制中1的个数
    面试金典——交点
    LeetCode——简化路径
    LeetCode——跳跃游戏 I-II
    LeetCode——最大矩形
  • 原文地址:https://www.cnblogs.com/xiaolang666/p/13645572.html
Copyright © 2011-2022 走看看