nginx优化

Nginx常见问题

一、nginx多server优先级
在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。
1.准备多个配置文件
[root@web01 conf.d]# cat server1.conf 
server {
	listen 80;
	server_name localhost test1.com;
	
	location / {
		root /code/test1;
		index index.html;
	}
}
[root@web01 conf.d]# cat server2.conf 
server {
	listen 80;
	server_name localhost test2.com;
	
	location / {
		root /code/test2;
		index index.html;
	}
}
[root@web01 conf.d]# cat server3.conf 
server {
	listen 80;
	server_name localhost test3.com;
	
	location / {
		root /code/test3;
		index index.html;
	}
}
You have new mail in /var/spool/mail/root
[root@web01 conf.d]#
2.配置站点文件
[root@web01 conf.d]# mkdir /code/test{1..3}
You have new mail in /var/spool/mail/root
[root@web01 conf.d]# echo "test1" > /code/test1/index.html
[root@web01 conf.d]# echo "test2" > /code/test2/index.html
[root@web01 conf.d]# echo "test3" > /code/test3/index.html
[root@web01 conf.d]# chown -R www.www /code/
3.启动nginx
[root@web01 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 successfu
#讲稿提醒有同样的配置内容

[root@web01 conf.d]# systemctl restart nginx
4.配置hosts访问测试
10.0.0.7 test1.com test2.com test3.com
5.多server优先级总结
1.首先选择所有的字符串完全匹配的server_name。(完全匹配)
2.选择通配符在前面的server_name,如*.mumusir.com www.mumusir.com
3.选择通配符在后面的server_name,如mumusir.* mumusir.com mumusir.cn
4.最后选择使用正则表达式匹配的server_name,如~^www\.(.*)\.com$
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件
6.总结测试
[root@web01 conf.d]# cat server1.conf 
server {
	listen 80;
	server_name localhost www.test.com;
	
	location / {
		root /code/test1;
		index index.html;
	}
}
[root@web01 conf.d]# cat server2.conf 
server {
	listen 80;
	server_name localhost *.test.com;
	
	location / {
		root /code/test2;
		index index.html;
	}
}
[root@web01 conf.d]# cat server3.conf 
server {
	listen 80;
	server_name localhost www.test.*;
	
	location / {
		root /code/test3;
		index index.html;
	}
}
[root@web01 conf.d]# cat server4.conf 
server {
	listen 80;
	server_name localhost ~^www\.(.*)\.com$;
	
	location / {
		root /code/test3;
		index index.html;
	}
}
[root@web01 conf.d]#

二、nginx禁止IP访问网站

1.禁止ip访问直接返回错误
[root@web01 conf.d]# vim server4.conf 
server {
    listen 80 default_server;
    server_name localhost;
    return 500;
}
2.引流的方式,访问IP跳转到主页
server {
    listen 80 default_server;
    server_name localhost;
    return 302 https://www.baidu.com;
}
3.返回指定的内容
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包含的作用是为了简化主配置文件,便于人类可读。

3.两个目录
include /etc/nginx/online/*.conf  #线上使用
mv .cnof  /etc/nginx/offlie/	#下线的配置,临时撤站

四、nginx路径的 root与alias

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

root的处理结果是:root路径+location路径(uri路径)
alias的处理结果是:使用alias定义的路径
1.root和alias配置
#使用root的时候,访问http://image.com/picture/1.gif,实际上会到服务器的/code/picture/目录下面寻找1.gif文件
[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;

    location /picture {
        root /code;
    }
}


#如果使用的是alias,访问http://image.com/picture/1.gif,实际上是到服务器的/code/目录下寻找1.gif文件
[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;

    location /picture {
        alias /code;
    }
}

2.生产中配置方式
server {
    listen 80;
    server_name image.com;
    
    location / {
    	root /code;
    	index index.html 1.html test.html
    }

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

五、Nginx的 try_file路径匹配

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


try_files $uri $uri/ /demo.html;

$uri : 访问根目录下的/www文件
$uri/: 访问www目录
/demo.html : 访问的式根目录下demo.html

访问顺序:$uri---->$uri/---->/demo.html

try_files 一般使用在伪静态的场景中###### 1.配置try_files
#配置nginx
[root@web01 conf.d]# vim try.conf 
server {
    listen 80;
    server_name try.com;

    location / {
        root /code/try;
        try_files $uri /404.html;
    }
}

#创建目录与文件
[root@web01 conf.d]# mkdir /code/try
[root@web01 conf.d]# echo "try_file.index" > /code/try/index.html
[root@web01 conf.d]# echo 404040404 > /code/try/404.html

#访问try.com,匹配$uri,而域名后面为空,匹配不到内容,所以匹配/404.html,返回的内容是/code/try/404.html
[root@web02 ~]# curl try.com
404040404

#访问try.com/index.html,匹配$uri匹配到了/index.html,返回的内容就是/code/try/index.html
[root@web02 ~]# curl try.com/index.html
try_file.index

#修改nginx配置为
[root@web01 conf.d]# vim try.conf 
server {
    listen 80;
    server_name try.com;

    location / {
        root /code/try;
        try_files $uri $uri/ /404.html;
    }
}

#访问try.com,$uri匹配不到内容,交给$uri/匹配,匹配到的是 “空/”,所以访问的链接是 try.com/ ,则能访问到/code/try/index.html
2.实例配置
#1. 配置nginx
[root@lb01 conf.d]# cat try.conf 
server {
    listen 80;
    server_name try.com;
    root /code;

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

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

六、Nginx 调整上传文件大小

1.nginx 上传文件大小限制语法
Syntax:  client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
2.nginx上传文件大小限制配置
http {
	....
	client_max_body_size 200m;
	....
}

七、Nginx优雅显示错误页面

1.跳转到网上
server {
    listen 80;
    server_name error.linux.com;

    location / {
        root /code;
        index index.html;
        error_page 404 http://www.baidu.com;
    }
}
2.跳转到本地文件
server {
    listen 80;
    server_name error.linux.com;

    location / {
        root /code;
        index index.html;
        error_page 404 /404.jpg;
    }
}

[root@web01 code]# ll 404.jpg 
-rw-r--r-- 1 root root 9564 Mar 10 16:07 404.jpg
3.配置负载证明错误页面是由web端返回的
[root@lb01 conf.d]# vim err.conf 
upstream errweb {
    server 172.16.1.7:80;
}

server {
    listen 80;
    server_name error.linux.com;

    location / {
        proxy_pass http://errweb;
        include proxy_params;
        access_log /tmp/nginx.log;
    }
}
4.访问php错误页面跳转
server {
    listen 80;
    server_name blog.linux.com;

    location / {
        root /code/wordpress;
        index index.php;
        error_page 404 403 /404.jpg;
    }

    location ~* \.php$ {
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        if (!-e $request_filename) {
            rewrite (.*) /code/wordpress/404.jpg;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值