day15.5

nginx常见问题

nginx多server优先级

1.首先选择所有的字符串完全匹配(精确匹配)的server_name 
2.选择统配符在前面的server_neme
3.选择通配符在后面的server_name
4.正则表达式的server_name
5.所有匹配规则相同时,哪个配置文件listen...后面加了default_server哪个优先级最高
6.按照配置文件的顺序访问第一个配置文件

WordPress设置禁止IP访问

# 当用户通过访问IP或者未知域名访问你得网站的时候,你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都要求网站关闭空主机头,防止未备案的域名指向过来造成麻烦
# 禁止IP访问,并返回错误页面
[root@web02 conf.d]# vim blog.wc.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        default_type text/json;
        return 500 "页面500";
}
server {
        listen 80;
        server_name blog.wc.com;
        root /code/wordpress;
        index index.php index.html;
        error_log /var/log/nginx/blog.wc.com.error.log;
        #return 500 "页面500,滚";

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
}
}

# 禁止访问IP,并跳转到主站点
[root@web02 conf.d]# vim blog.wc.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        #default_type text/json;
        #return 500 "页面500";
        rewrite (.*) http://blog.wc.com$1 redirect;

}
server {
        listen 80;
        server_name blog.wc.com;
        root /code/wordpress;
        index index.php index.html;
        error_log /var/log/nginx/blog.wc.com.error.log;
        #return 500 "页面500,滚";

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
}
}

nginx包含其他子配置文件include

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

include /etc/nginx/fastcgi_params

# 自己创建的 代理优化参数
include /etc/nginx/proxy_parmas 

# 需要生效的配置文件
[root@web01 conf.d]# mkdir /etc/nginx/conf.d/online

# 不需要生效的配置文件
[root@web01 conf.d]# mkdir /etc/nginx/conf.d/offline

# 修改nginx主配置文件
include /etc/nginx/conf.d/online/*.conf;

站点目录路径root和alias的区别

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

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

使用root时,用户访问http://image.com/picture/1.jpg时,实际上Nginx会到/code/picture/目录下找1.jpg文件
[root@web02 conf.d]# vim blog.wc.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        #default_type text/json;
        #return 500 "页面500";
        rewrite (.*) http://blog.wc.com$1 redirect;

}
server {
        listen 80;
        server_name blog.wc.com;
        root /code/wordpress;
        index index.php index.html;
        error_log /var/log/nginx/blog.wc.com.error.log;
        #return 500 "页面500,滚";

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
}
}


# alias指定站点目录
vim /etc/nginx/conf.d/online/2.conf
server {
listen 80 default_server;
server_name _;
charset utf-8;
rewrite (.*) http://www.zls.com$1 redirect;
}
server {
listen 80;
server_name www.zls.com;
root /code/2;
index index.html;
location /images {
alias /images;
}
}
location /images {
alias /code/images;
}
## /code/images/1.png
location /images {
root /code/images;
}
## /code/images/images/1.png

nginx try_file路径匹配

# nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。
#1. 配置nginx
[root@lb01 conf.d]# vim try.conf 
server {
    listen 80;
    server_name try.drz.com;
    root /code;
    index index.html;
 
    location / {
        try_files $uri $uri/ /404.html;
    }
}
 
#2. 创建实例目录与文件drz
[root@lb01 conf.d]# echo try11111 > /code/index.html 
[root@lb01 conf.d]# echo '404 404 404' > /code/404.html
 
#3. 尝试访问try.drz.com
[root@lb01 conf.d]# curl try.drz.com
404 404 404
#由于访问的是try.drz.com,而$uri取得是域名后面我们写的内容,它找不到,所以返回后面的内容,即404.html
 
#4. 尝试访问try.drz.com/index.html
[root@lb01 conf.d]# curl try.drz.com/index.html
try11111
#由于访问的是try.drz.com/index.html,而$uri取到了index.html所以返回/code/index.html的内容
 
#5. 修改配置为
location / {
    try_files $uri $uri/ /404.html;
}
 
#6. 再次尝试访问try.drz.com
[root@lb01 conf.d]# curl try.drz.com
try11111
#我们访问的是try.drz.com,而$uri我们没有写任何内容,于是他访问的便是“空/”,即匹配到/code/index.html

nginx调整上传文件大小

# 在nginx使用上传文件的过程中,通常需要设置保温大小限制,避免出现413 Request Entity Too Large

# 语法
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location

nginx优雅的显示404错误页面

# 当用户输入的页面不存在时自动跳转到初始页面
# [root@web02 conf.d]# vim blog.wc.com.conf 

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        #default_type text/json;
        #return 500 "页面500";
        rewrite (.*) http://blog.wc.com$1 redirect;

}
server {
        listen 80;
        server_name blog.wc.com;

        root /code/wordpress;
        index index.php index.html;
        error_log /var/log/nginx/blog.wc.com.error.log;
        error_page 404 http://blog.wc.com;
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
}
}

posted @ 2022-06-24 16:33  Gabydawei  阅读(34)  评论(0编辑  收藏  举报