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;
...
}
原创文章,作者:wure,如若转载,请注明出处:https://blog.ytso.com/270294.html