nginx配置文件参考

1.禁止ip访问页面

# 直接返回500错误
[root@web01 conf.d]# vim server4.conf 
server {
    listen 80 default_server;
    server_name localhost;
    return 500;
}

# 访问ip跳转到主页
server {
    listen 80 default_server;
    server_name localhost;
    return 302 https://www.baidu.com;
}

# 返回指定内容
server {
    listen 80 default_server;
    server_name localhost;
    default_type text/plain;
    return 200 "页面错误.......";
}

2.多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块的配置文件

3.nginx路径的root和alias

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

root的处理结果是:root路径+location路径
alias的处理结果是:使用alias定义的路径

[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;

    location /picture {
        root /code;
    }
}
#使用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 {
        alias /code;
    }
}
#如果使用的是alias,访问http://image.com/picture/1.gif,实际上是到服务器的/code/目录下寻找1.gif文件

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

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

4.Nginx的 try_file路径匹配

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

#配置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



############## 实例配置 ########################################
#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
    }
}

5.优雅显示错误页面

# 遇到404错误跳转到百度
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;
    }
}


# 访问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;
        }
    }
}
原文地址:https://www.cnblogs.com/tcy1/p/13704827.html