Nginx常见问题
nginx多server优先级
1.首先选择所有的字符完全匹配 (精确匹配) 的server_name。 (完全匹配)
2.选择通配符在前面的server_name
3.选择通配符在后面的server_name
4.正则表达式的server_name、
5.所有匹配规则相同时,哪个配置文件listen...后面加了default哪个优先级就最高
6.按照匹配文件的顺序访问第一个配置文件
禁止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;
include fastcgi_params;
}
}
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;
include fastcgi_params;
}
}
Nginx包含其他子配置文件
一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 假设现在希望快速的关闭一个站点,该怎么办?
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。
站点目录路径root和alias区别
server{
listen 80;
server_name img.zh.com;
root /code/2;
index index.html;
incation /images{
root /code/images;
}
}
server{
listen 80;
server_name img.zh.com;
root /code/2;
index index.html;
incation /images{
alias /code/images;
}
}
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;
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;
...
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!