微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nginx常见问题

Nginx常见问题

Nginx多server优先级

# 优先级匹配顺序
1.首先选择所有的字符完全匹配 (精确匹配) 的server_name。 (完全匹配)

2.选择通配符在前面的server_name

3.选择通配符在后面的server_name

4.正则表达式的server_name、

5.所有匹配规则相同时,哪个配置文件listen...后面加了default哪个优先级就最高

6.按照匹配文件的顺序访问第一个配置文件


禁止IP访问

# 禁止IP访问,并访问错误页面

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        default_type text/json;
        return 500 "页面500 ,给爷爬~";
}
server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.PHP index.html;

        location ~ \.PHP$ {
                fastcgi_pass unix:/dev/shm/PHP.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

# 禁止Ip访问并跳转到主页
server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://blog.zh.com$1 redirect;
}
server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.PHP index.html;

        location ~ \.PHP$ {
                fastcgi_pass unix:/dev/shm/PHP.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

Nginx包含其他子配置文件

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

站点目录路径root和alias区别

# root指定站点目录
server{
        listen 80;
        server_name img.zh.com;
        root /code/2;
        index index.html;

        incation /images{
                root /code/images;
        }
}

# 图片路径:/code/images/images/1.png
# alias指定站点目录
server{
        listen 80;
        server_name img.zh.com;
        root /code/2;
        index index.html;

        incation /images{
                alias /code/images;
        }
}
# 图片路径 :/code/images/1.png

Nginx try_file路径匹配

server {
        listen 80 default_server;
        server_name _;
        charset utf-8;
        rewrite (.*) http://www.baidu.com$1 redirect;
}

server{
        listen 80;
        server_name blog.zh.com;
        root /code/wordpress;
        index index.PHP index.html;

        location ~ \.PHP$ {
                fastcgi_pass unix:/dev/shm/PHP.sock;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_param HTTPS on;
                include fastcgi_params;
        }
# 路径匹配
        location / {
                try_files $uri $uri/  zh;
        }
        location zh {
                proxy_pass http://blog.zh.com;
        }
}

Nginx调整上传大小

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

Nginx优雅显示404错误页面

server {
        listen 80;
        server_name www.zh.com;

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

隐藏版本号

http{
	server_tokens off; 
	... 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐