一、nginx多server优先级
[root@web01 conf.d]
server {
listen 80;
server_name localhost test1.linux.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]
server {
listen 80;
server_name localhost test2.linux.com;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]
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]
server {
listen 80 default_server;
server_name _;
return 500;
}
2.使用ip访问时跳转到指定页面
[root@web01 conf.d]
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
四、Nginx路径root与alias
root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。
root的处理结果是:root路径+location路径alias的处理结果是:使用alias定义的路径
1.使用root时
server {
listen 80;
server_name image.com;
location /picture {
root /code;
}
}
2.使用alias时
server {
listen 80;
server_name image.com;
location /picture {
alias /code;
}
}
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
[root@lb01 conf.d]
server {
listen 80;
server_name try.linux.com;
root /code;
index index.html;
location / {
try_files $uri /404.html;
}
}
[root@lb01 conf.d]
[root@lb01 conf.d]
[root@lb01 conf.d]
404 404 404
[root@lb01 conf.d]
try11111
location / {
try_files $uri $uri/ /404.html;
}
[root@lb01 conf.d]
try11111
2.Nginx try_file配置实例2
[root@lb01 conf.d]
server {
listen 80;
server_name try.linux.com;
root /code;
index index.html;
location / {
try_files $uri $uri/ @java;
}
location @java {
proxy_pass http://172.16.1.8:8080;
}
}
[root@web02 ~]
[root@web02 ROOT]
[root@web02 ROOT]
[root@lb01 code]
[root@lb01 code]
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长传文件大小限制配置示例
server {
listen 80;
server_name _;
client_max_body_size 200m;
}
七、Nginx优雅显示错误页面
1.配置跳转本地页面
[root@web01 conf.d]
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]
server {
listen 80;
server_name test1.linux.com;
root /code/test1;
index index.html;
error_page 404 http://www.baidu.com;
}