• 18 Nginx优化(一)


    nginx多server优先级

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

    禁止IP访问页面

    server {
    	listen 80 default_server;
    	server_name _;
    	#返回错误
    	return 500;
    	#跳转页面
    	return 302 https://www.mumusir.com;
    	#返回我指定的内容
    	default_type  text;
    	return 200 "页面错误...";
    }
    

    nginx的包含include

    vim nginx.conf
    
    #include /etc/nginx/conf.c/*.conf;
    http {
    	... ...
    	include /etc/nginx/mime.types;
    	... ...
    	include /etc/nginx/conf.d/*.conf;
    	... ...
    	server {
    		... ...
    		#当location很多很长的时候,也可以使用匹配
    		include /etc/nginx/conf.l/*.conf;
    		... ...
    		location ~* .php$ {
    			... ...
    			include fastcgi_params;
    		}
    	}
    }
    

    nginx 路径的alias和root

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

    1.配置

    [root@web01 ~]# vim /etc/nginx/conf.d/image.conf 
    server {
        listen 80;
        server_name image.com;
    
        location /picture {
            root /code/images/picture;
        }
    }
    
    [root@web01 ~]# vim /etc/nginx/conf.d/image.conf 
    server {
        listen 80;
        server_name image.com;
    
        location /picture {
            alias /code/images/picture;
        }
    }
    

    2.总结

    1.alias的处理结果是:使用alias定义的路径,而root的处理结果是:root路径+location路径
    2.alias只能用在location中,而root可以用在http、server、location中
    3.location如果加了/,那alias匹配的内容也要加/,否则会找不到文件
    

    nginx的try_files

    1.配置try_files

    #1.配置nginx
    [root@web01 conf.d]# vim try.conf 
    server {
        listen 80;
        server_name try.linux.com;
        root /code/try;
    
        location / {
            try_files $uri /404.html;
        }
    }
    
    #2. 创建实例目录与文件
    [root@web01 conf.d]# echo try11111 > /code/try/index.html 
    [root@web01 conf.d]# echo '404 404 404' > /code/try/404.html
    
    #3. 尝试访问try.linux.com
    [root@web01 conf.d]# curl try.linux.com
    404 404 404
    #由于访问的是try.drz.com,而$uri取得是域名后面我们写的内容,它找不到,所以返回后面的内容,即404.html
    
    #4. 尝试访问try.linux.com/index.html
    [root@web01 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.配置实例

    #1. 配置nginx
    [root@web01 conf.d]# cat try.conf 
    server {
        listen 80;
        server_name try.drz.com;
        root /code;
        index index.html;
    
        location / {
            try_files $uri $uri/ @java;             #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@,内部子请求
        }
    
        location @java {
        proxy_pass http://127.0.0.1:8080;          #配置后端tomcat
        }
    }
    

    显示指定错误页面

    跳转到其他网页

    server {
     	listen 80;
     	server_name error.linux.com;
     		
     	location / {
     		root /code;
     		index index.html;
     		error_page 404 http://www.baidu.com;
     }
    }
    

    跳转到本地文件

    server {
     	listen 80;
     	server_name error.linux.com;
     		
     	location / {
     		root /code;
     		index index.html;
     		error_page 404 404.jpg;
     }
    }
    
  • 相关阅读:
    统计学(第六版)14单元——学习总结
    统计学(第六版)13单元——学习总结(时间序列分析总结)
    统计学(第六版)11到12单元——学习总结
    Kubernetes: 微内核的分布式操作系统
    彻底搞懂JavaScript之原型
    手把手带你玩转k8s-一键部署vue项目
    新一代缓存Caffeine,速度确实比Guava的Cache快
    理解 Es6 中的 Symbol 类型
    一天一大 leet(用两个栈实现队列)难度:简单 DAY-30
    (Java 源码阅读) 春眠不觉晓,HashMap知多少
  • 原文地址:https://www.cnblogs.com/zhaokunhao/p/14757476.html
Copyright © 2020-2023  润新知